Package: attrs-strict
attrs runtime validationattrs-strictis a Python package which contains runtime validation forattrsdata classes based on the types existing in the typing module.RationaleThe purpose of the library is to provide runtime validation for attributes specified inattrsdata classes. The types supported are all the builtin types and most of the
ones defined in the typing library. For Python 2, the typing module is available through the backport foundhere.Quick StartType enforcement is based on thetypeattribute set on any field specified in anattrsdataclass. If the type
argument is not specified, no validation takes place.pip install attrs-strictfromtypingimportListimportattrfromattrs_strictimporttype_validator@attr.sclassSomeClass(object):list_of_numbers=attr.ib(validator=type_validator(),type=List[int])sc=SomeClass([1,2,3,4])print(sc)SomeClass(list_of_numbers=[1,2,3,4])try:SomeClass([1,2,3,"four"])exceptValueErrorasexception:print(repr(exception))SomeClass(list_of_numbers=[1, 2, 3, 4])<list_of_numbers must be typing.List[int] (got four that is a <class 'str'>) in [1, 2, 3, 'four']>Nested type exceptions are validated accordingly, and a backtrace to the initial container is maintained to ease with
debugging. This means that if an exception occurs because a nested element doesn't have the correct type, the
representation of the exception will contain the path to the specific element that caused the exception.fromtypingimportList,Tupleimportattrfromattrs_strictimporttype_validator@attr.sclassSomeClass(object):names=attr.ib(validator=type_validator(),type=List[Tuple[str,str]])try:SomeClass(names=[("Moo","Moo"),("Zoo",123)])exceptValueErrorasexception:print(exception)names must be typing.List[typing.Tuple[str, str]] (got 123 that is a <class 'int'>) in ('Zoo', 123) in [('Moo', 'Moo'), ('Zoo', 123)]What is currently supported ?Currently, there's support for simple types and types specified in thetypingmodule:List,Dict,DefaultDict,Set,Union,Tuple,NewTypeCallable,Literaland any combination of them. This means that you can specify
nested types likeList[List[Dict[int, str]]]and the validation would check if attribute has the specific type.Callablewill validate if the callable function's annotation matches the type definition. If type does not specify any
annotations then all callables will pass the validation against it. Support forCallableis not available forpython2.Literalonly allows using instances ofint,str,bool,Enumor validLiteraltypes. Type checkingLiteralwith any other type as argument raisesattrs_strict._error.UnsupportedLiteralError.deffully_annotated_function(self,a:int,b:int)->str:...defun_annonated_function(a,b):...@attr.sclassSomething(object):a=attr.ib(validator=type_validator(),type=typing.Callable)# Will work for any callableb=attr.ib(validator=type_validator(),type=typing.Callable[[int,int],str])Something(a=un_annonated_function,b=fully_annotated_function)TypeVarsorGenericsare not supported yet but there are plans to support this in the future.BuildingFor development, the project usestoxin order to install dependencies, run tests and
generate documentation. In order to be able to do this, you need toxpip install toxand after that invoketoxin
the root of the project.InstallationRunpip install attrs-strictto install the latest stable version fromPyPi.
Documentation is hosted onreadthedocs.For the latest version, on githubpip install git+https://github.com/bloomberg/attrs-strict.