Package: dataclass-type-validator
dataclass-type-validatorThedataclass-type-validatoris a type validation library for the properties ofdataclasses.dataclassusing Python type hint information.Installationpip install dataclass-type-validatoror adddataclass-type-validatorline torequirements.txtA Simple ExampleExplicitly calling dataclass_type_validator from within your dataclassfromdataclassesimportdataclassfromtypingimportListfromdataclass_type_validatorimportdataclass_type_validatorfromdataclass_type_validatorimportTypeValidationError@dataclass()classUser:id:intname:strfriend_ids:List[int]def__post_init__(self):dataclass_type_validator(self)# Valid UserUser(id=10,name='John Smith',friend_ids=[1,2])# => User(id=10, name='John Smith', friend_ids=[1, 2])# Invalid Usertry:User(id='a',name=['John','Smith'],friend_ids=['a'])exceptTypeValidationErrorase:print(e)# => TypeValidationError: Dataclass Type Validation (errors = {#   'id': "must be an instance of <class 'int'>, but received <class 'str'>",#   'name': "must be an instance of <class 'str'>, but received <class 'list'>",#   'friend_ids': 'must be an instance of typing.List[int], but there are some errors:#       ["must be an instance of <class \'int\'>, but received <class \'str\'>"]'})The same, but using the class decorator insteadfromdataclassesimportdataclassfromtypingimportListfromdataclass_type_validatorimportdataclass_validatefromdataclass_type_validatorimportTypeValidationError@dataclass_validate@dataclass()classUser:id:intname:strfriend_ids:List[int]# Valid UserUser(id=10,name='John Smith',friend_ids=[1,2])# => User(id=10, name='John Smith', friend_ids=[1, 2])# Invalid Usertry:User(id='a',name=['John','Smith'],friend_ids=['a'])exceptTypeValidationErrorase:print(e)# => TypeValidationError: Dataclass Type Validation (errors = {#   'id': "must be an instance of <class 'int'>, but received <class 'str'>",#   'name': "must be an instance of <class 'str'>, but received <class 'list'>",#   'friend_ids': 'must be an instance of typing.List[int], but there are some errors:#       ["must be an instance of <class \'int\'>, but received <class \'str\'>"]'})You can also pass thestrictparam (which defaults to False) to the decorator:@dataclass_validate(strict=True)@dataclass(frozen=True)classSomeList:values:List[str]# Invalid item contained in typed Listtry:SomeList(values=["one","two",3])exceptTypeValidationErrorase:print(e)# => TypeValidationError: Dataclass Type Validation Error (errors = {#   'x': 'must be an instance of typing.List[str], but there are some errors:#       ["must be an instance of <class \'str\'>, but received <class \'int\'>"]'})You can also pass thebefore_post_initparam (which defaults to False) to the decorator,
to force the type validation to occur before__post_init__()is called.  This can be used
to ensure the types of the field values have been validated before your higher-level semantic
validation is performed in__post_init__().@dataclass_validate(before_post_init=True)@dataclassclassUser:id:intname:strdef__post_init__(self):# types of id and name have already been checked before this is called.# Otherwise, the following check will throw a TypeError if user passed# `id` as a string or other type that cannot be compared to int.ifid<1:raiseValueError("superuser not allowed")