Runtime type-checking for Python 2.7. Add decorators to your functions and properties to support runtime type safety of arguments and return types.
@types(x=str, returns=str)
def greet(x):
return 'Hello ' + x
greet('user!') # >>> Hello user!
greet(42) # >>> x (42) is not type <type 'str'>
# Types
Any # represents any type; ignored by checker
Class # represents any type; analogous to types.TypeType
Optional # optional type; allows for given type or None value
opt(str) # optional type alias with a type value of str
# Properties
class TypesafeClass(object):
def __init__(self):
self._x = ''
add_property(self, 'x', str)
instance = TypesafeClass()
instance.x # >>> ''
def greet(x):
return 'Hello ' + x
$ flake8 greet.py
greet.py:1:1: T000 function 'greet' does not have a type declaration
$ pip install git+git://github.com/mirainc/python-typesafe.git@v0.3
# in requirements.txt
-e git://github.com/mirainc/python-typesafe.git@v0.3#egg=typesafe