This is a python module that type checks for Python function arguments and return values.
pip install type_spec
A function that takes built-in types:
from type_spec import typeSpec
@typeSpec(int, int, int)
def add(a, b):
return a + bA function that takes any of a number of built in types with certain constraints:
from type_spec import typeSpec, AllOf, AnyOf, type_check
def Number(v):
return type_check(v, AnyOf(int, float))
def GreaterThanZero(v):
if v <= 0:
return 'must be greater than zero'
@typeSpec(Number, AllOf(Number, GreaterThanZero), Number)
def div(a, b):
return a / bTo specify a pre/post condition for your function you use the typeSpec
decorator. The signature is:
typeSpec(arg_1_type, ..., arg_n_type, return_type)
The arguments/types provided to typeSpec can be any of the following:
- A built-in type such as
int,float,str,bool,list,dict,setetc. - A class, i.e.
MyClass - A function/callable that takes a value as argument and returns
Noneif the value has a valid type and an error message (string) otherwise. - A
dictinstance that specifies that a valid value must be a dictionary with a certain structure, i.e.{'name': str, 'enabled': bool} - A
listinstance that specifies that a valid value must be a list with items of a certain type, i.e.[str]. - A predicate function that returns True or False, i.e.
lambda n: n > 0. Note that you can use thecallablebuilt-in predicate function to type check functions. - An example value/instance, i.e.
None,"2017-01-01". This means the value needs to be an instance of the same type as the example value.
- typeannotations - uses the elegant approach of overriding
isinstancefor its type checks (through metaclasses) along with the built-in Python 3 type annotation syntax. - PEP 362 - Function Signature Object - annotation checker