Skip to content

Instantly share code, notes, and snippets.

@agronholm
Last active September 17, 2015 22:43
Show Gist options
  • Select an option

  • Save agronholm/00311c169527442ddd5b to your computer and use it in GitHub Desktop.

Select an option

Save agronholm/00311c169527442ddd5b to your computer and use it in GitHub Desktop.
The typechecked() decorator
class DummyType:
pass
def typecheck1(fooarg: int=None):
return 1
def typecheck2(a, b: str, fooarg: int=None):
return 1
def typecheck3(a: Union[int, str]=None):
return 1
def typecheck4(a: str):
return 1
def typecheck5(a: str=None, c: bool=False):
return 1
def typecheck6(a: 'DummyType'):
return 1
def typecheck7(a: 'Nonexistent'):
return 1
def typecheck8(a: 'typecheck8'):
return 1
@pytest.mark.parametrize('func,args,kwargs', [
(typecheck1, (1,), {}),
(typecheck1, (), {'fooarg': 1}),
(typecheck1, (), {}),
(typecheck1, (None,), {}),
(typecheck1, (), {'fooarg': None}),
(typecheck2, (1,), {'b': 'foo'}),
(typecheck3, (None,), {}),
(typecheck3, (), {'a': None}),
(typecheck4, ('foo',), {}),
(typecheck5, (None,), {}),
(typecheck6, (DummyType(),), {}),
], ids=['basic', 'basic_kwarg', 'default_noarg', 'default_none', 'default_none_kwarg',
'multiarg', 'multitype_pos', 'multitype_kwarg', 'nodefaults', 'multiple_defaults',
'typelookup'])
def test_typechecked(func, args, kwargs):
assert typechecked(func)(*args, **kwargs) == 1
@pytest.mark.parametrize('func,args,kwargs,error_msg', [
(typecheck1, ('foo',), {},
'Type of argument "fooarg" should match Union[int, NoneType] -- got str instead'),
(typecheck1, (), {'fooarg': 'foo'},
'Type of argument "fooarg" should match Union[int, NoneType] -- got str instead'),
(typecheck2, (1,), {'b': 1}, 'Type of argument "b" should match str -- got int instead'),
(typecheck3, (2.0,), {'b': 1},
'Type of argument "a" should match Union[int, str, NoneType] -- got float instead'),
(typecheck7, (None,), {},
'Type "Nonexistent" not found for argument a of test_util.typecheck7'),
(typecheck8, (None,), {},
'Type name "typecheck8" for argument a of test_util.typecheck8 does not resolve to a type '
'object')
], ids=['basic_fail', 'basic_fail_kwarg', 'multiarg_fail', 'multitype_fail', 'typelookup_fail',
'typelookup_wrong_type'])
def test_typechecked_error(func, args, kwargs, error_msg):
exc = pytest.raises(TypeError, typechecked(func), *args, **kwargs)
assert exc.value.args[0] == error_msg
def test_typechecked_always():
# Not much of a test since __debug__ can't be set at run time
decorated = typechecked(always=True)(typecheck1)
assert decorated(4) == 1
exc = pytest.raises(TypeError, decorated, 'foo')
assert (exc.value.args[0] ==
'Type of argument "fooarg" should match Union[int, NoneType] -- got str instead')
def test_typechecked_no_annotations(recwarn):
def foo(a):
pass
typechecked(foo)
w = recwarn.pop(UserWarning)
assert (str(w.message) == 'No type annotations present -- not typechecking '
'test_util.test_typechecked_no_annotations.<locals>.foo')
def test_typechecked_incompatible_annotations(recwarn):
def foo(a: 1):
pass
typechecked(foo)
w = recwarn.pop(UserWarning)
assert str(w.message) == (
'The type annotation for parameter "a" in '
'test_util.test_typechecked_incompatible_annotations.<locals>.foo should either be a type '
'or a string reference to a type in the same module -- skipping type checking for this '
'parameter')
def typechecked(func: Callable[..., Any]=None, *, always: bool=False):
"""
Performs runtime type checking on the arguments that are passed to the wrapped function.
Each argument is checked against its annotated type using ``isinstance()``.
If a parameter has a default value, its type will be added to the list of allowed types.
Type checks are disabled if Python is being run in "production" mode
(ie. ``python -O`` or setting the ``PYTHONOPTIMIZE`` environment variable).
Example::
@typechecked
def foo(a: str, b: int, c: (str, int)):
pass
To enable type checks even in production mode::
@typechecked(always=True)
def foo(a: str, b: int, c: (str, int)):
pass
:param always: ``True`` to enable type checks even in production mode
"""
if func is None:
return partial(typechecked, always=always)
if not __debug__ and not always: # pragma: no cover
return func
argspec = inspect.getfullargspec(func)
if not argspec.annotations:
warn('No type annotations present -- not typechecking {}'.format(qualified_name(func)))
return func
argnames = argspec.args
allowed_types = {}
for name, argtype in argspec.annotations.items():
if isinstance(argtype, (type, str)):
allowed_types[name] = argtype
else:
warn('The type annotation for parameter "{}" in {} should either be a type or a '
'string reference to a type in the same module -- skipping type checking for '
'this parameter'.format(name, qualified_name(func)))
# Make sure the type of the default argument is within the accepted types (if it's not, add it)
for name, default_value in zip(reversed(argnames), reversed(argspec.defaults or ())):
if name in allowed_types:
allowed_type = allowed_types[name]
if not isinstance(default_value, allowed_type):
allowed_types[name] = Union[allowed_type, type(default_value)]
del argspec
@wraps(func)
def wrapper(*args, **kwargs):
for argname, value in chain(zip(argnames, args), kwargs.items()):
allowed = allowed_types.get(argname)
if allowed is not None:
if type(allowed) is str:
# Need to look up the type from the callable's globals
type_ = func.__globals__.get(allowed)
if not type_:
raise TypeError('Type "{}" not found for argument {} of {}'.
format(allowed, argname, qualified_name(func)))
elif not isinstance(type_, type):
raise TypeError('Type name "{}" for argument {} of {} does not resolve to '
'a type object'.format(allowed, argname,
qualified_name(func)))
allowed_types[argname] = allowed = type_
if not isinstance(value, allowed):
if allowed.__module__ == 'typing':
typename = str(allowed)[7:]
else:
typename = qualified_name(allowed)
raise TypeError('Type of argument "{}" should match {} -- got {} instead'.
format(argname, typename, qualified_name(value)))
return func(*args, **kwargs)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment