class NonePrimitive(Model): normal: bool = BoolField(primitive_name='ok', to_primitive_name='ordinary') none_both: int = IntField(primitive_name=None) none_input: float = FloatField(primitive_name=None, to_primitive_name='no_input') none_output: str = StrField(to_primitive_name=None)
class Book(Model): name: str authors: List[str] = ListField(min_length=1) reviews: Dict[str, float] = DictField( key_field=StrField(min_length=1), value_field=FloatField(min_value=0, max_value=5), default=dict, )
class Employee(Model): name: str department: str = 'Engineering' female: Optional[bool] = None salary: float = FloatField(min_value=42., default=42.) human = True def greeting(self) -> str: title = {True: "Ms. ", False: "Mr. "}.get(self.female, "") return f'Dear {title}{self.name}'
class FloatModel(Model): normal: Optional[float] = None min: float = FloatField(min_value=4, to_primitive_name='bottom') max: Optional[float] = FloatField(max_value=47.42, default=42., primitive_name='top', to_primitive_name='max') min_max: float = FloatField(min_value=1.5, max_value=4, default=lambda: 3.5, hide_none=True) def validate_min(self, value: int, _): # Would fail if executed with None if value % 2 == 0: raise ValueError('Must be an odd number') def validate_max(self, value: int, _): # Executed even with None because max is optional if value is None and self.normal is None: raise ValueError('Must be set if normal is omitted')
class Second(First): plain: float custom: float = FloatField(min_value=2)
class DriedLeaf(Leaf): age: float = FloatField(max_value=5, default=0.)
class Hidden(Model): none: Optional[float] = FloatField(hide_none=True, default=None) zero: Optional[float] = FloatField(hide_zero=True, default=0.0)
class Mismatch(Model): my_name: int = FloatField(min_value=1)