atom icon indicating copy to clipboard operation
atom copied to clipboard

Enum items property is incorrect when a validate method is provided

Open frmdstryr opened this issue 1 year ago • 9 comments

class Foo(Atom):
    MAPPING = {"x": 1, "y": 2}
    x = Enum(*MAPPING.keys())
    

Foo.x.items
Out[21]: ('x', 'y')

class Foo(Atom):
    MAPPING = {"x": 1, "y": 2}
    x = Enum(*MAPPING.keys())
    def _validate_x(self, old, new):
        return new
        

Foo.x.items
Out[23]: '_validate_x'

Edit. From looking at the code I suppose this could just be considered incorrect usage. I'm trying to track which fields are invalid. Maybe there is a better way to do it? I just decided to subclass the Enum and override the items property.


class Foo(Atom):
    MAPPING = {"x": 1, "y": 2}
    errors = Dict()
    x = Enum(*MAPPING.keys())
    def _validate_x(self, old, new):
        if new not in self.MAPPING:
            self.errors['x'] = "x is invalid"
            return old     
        return new

frmdstryr avatar Feb 01 '23 01:02 frmdstryr