Package: decorator-validation
Validation DecoratorsSimple and fast type-checking and parameter validation.Install withpipinstalldecorator-validationWhy?Do you want to remove visual clutter in your python function?
Do you want to check types fast without a lot of boilerplate?Then this package is right for you.Not convinced? Check out the following:# classicdeffoo(bar:int,message:str,some_additional_info:dict):ifnotisinstance(bar,int):raiseTypeError(...)ifnotisinstance(message,str):raiseTypeError(...)ifnotisinstance(some_additional_info,dict):raiseTypeError(...)# now begin to code...# now@check_types()deffoo(bar:int,message:str,some_additional_info:dict):# begin to codeCheckout the codebase for more examples and built in decorators!NOTE:check_typeshas limitations for python versions lower than 3.10 due to lack of built in language support for type-checking. Use with caution with special types and None-Types!More ExamplesNote:check_typesis only available since release 2.2.0. for previous releases checkoutvalidate_types. For more details check out the repository and older releases :).If of course also supports multiple types:frompathlibimportPath@check_types()deffoo(path:str|Path,message:str):# begin to codeDo you want to convert your input-types fast and without clutter?deffrom_dict(dict_:dict):return(dict_.get('bar'),dict_.get('message'),dict_.get('some_additional_info')),{}@convert_with(from_dict)deffoo(bar:int,message:str,some_additional_info:dict):...Skip Type-checks by providing theSkipTypecheckclass as a type. This enables you
to skip the type check even if you have type-annotations.
This is usefull if you do not wont to check some stuff but want to use the type-hintsfromdecorator_validation.decoratorsimportvalidate_typesfromdecorator_validation.typesimportSkipTypeCheckclassFileReader:@check_types(self=(SkipTypeCheck,),file_path=(str,))def__init__(self,file_path:str):...Of course, sometimes you want to have a custom validation method for all your inputs.
Then, just use thevalidate_withfunction.fromdecorator_validation.decoratorsimportvalidate_withfrompathlibimportPathdefmy_validation_func(obj,file_path:str)->True:ifnotisinstance(file_path,str):raiseTypeError(...)ifnotPath(file_path).resolve().is_file():raiseValueError(...)returnTrueclassFileReader:@validate_with(my_validation_func)def__init__(self,file_path:str):...Map Multiple functions for subtypesYou can also directly map different validation functions to your arguments.
A Validation function is a function that returnsTrueofFalse(or raises an exception) if the input is correct / incorrect.fromdecorator_validation.decoratorsimportcheck_typesfromdecorator_validation.std_validatorsimportis_fileclassFileReader:@check_types(file_path=is_file)def__init__(self,file_path:str):...Validate Arbitrary ArgumentsYou can of course combine validation functions with type-check-skipping and the
signature_usage.fromdecorator_validation.decoratorsimportcheck_typesfromdecorator_validation.typesimportSkipTypeCheckfromdecorator_validation.std_validatorsimportis_fileclassLogger:@check_types(file_path=is_file,message=(SkipTypeCheck,))deflog(file_path:str,message:str,repeat:int=1):...log('some_file.txt','hello')# workslog('some_file.txt',2)# workslog('some_file.txt',2,'3')# does not work