class Wall(object): height: int = field(validators={'should be a positive number': x > 0, 'should be a multiple of 100': x % 100 == 0}, doc="Height of the wall in mm.") color: str = field(validators=is_in(colors), default='white', doc="Color of the wall.")
class Operate(object): att = field() # Required type_ = field(type_hint=str, check_type=True, validators=is_in(valid_types)) # Required value = field(default_factory=copy_value([])) # Optional mode = field(default=None) # Optional action = field(default=None) # Optional @init_fields def __init__(self): pass
def test_is_in(): """ Checks that is_in works """ is_in({'+', '-'})('+') with pytest.raises(ValidationFailure): is_in({'+', '-'})('*') # also test non-set iterable types is_in(('+', '-'))('+')
class Car(object): """ My class with many fields """ color = field(type_hint=str, check_type=True, validators=is_in(ALLOWED_COLORS)) name = field(type_hint=str, check_type=True, validators={'should be non-empty': lambda s: len(s) > 0}) wheels = field(type_hint=int, check_type=True, validators={'should be positive': lambda x: x > 0}) @init_fields def __init__(self, msg="hello world!"): print(msg)