This module provides routines to verify type specifications.
A value is verified in one of four ways:
The last of these must inspect all values, so can be inefficient.
Hint
Don’t forget that type specifications also work with isinstance() and issubclass().
A decorator that adds runtime verification of type annotations to a function or method.
>>> @checked
... def str_len(s:str) -> int:
... return len(s)
>>> str_len('abc')
3
>>> str_len([1,2,3])
Traceback (most recent call last):
...
TypeError: Type str inconsistent with [1, 2, 3].
>>> @checked
... def bad_len(s:str) -> int:
... return 'wrong'
>>> bad_len('abc')
Traceback (most recent call last):
...
TypeError: Type int inconsistent with 'wrong'.