does python 3.5 provide functions that allow to test whether a given argument would fit the type hints given in the function declaration?
if i have e.g. this function:
def f(name: List[str]):
pass
is there a python method that can check whether
name = ['a', 'b']
name = [0, 1]
name = []
name = None
...
fit the type hints?
i know that 'no type checking happens at runtime' but can i still check the validity of these arguments by hand in python?
or if python does not provide that functionality itself: what is the tool i'd need to use?
Python itself doesn't provide such functions, you can read more about it here:
I wrote a decorator for that. This is the code of my decorator:
You can use it like that:
Though it's not very pythonic to restrict your function to accept only one type. Though you can use abc (abstract base classes) like
number
(or custom abc) as type-hints and restrict your functions to accept not only one type, but whatever combination of types you want.Added a github repo for it, if anybody wants to use it.
This is an old question, but there is a tool I've written for doing run time type checking based on type hints: https://pypi.org/project/typeguard/