Skip to content

Instantly share code, notes, and snippets.

@tweekmonster
Created January 4, 2020 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tweekmonster/6e2e528faa7f2583eb88c9ebb3aac786 to your computer and use it in GitHub Desktop.
Save tweekmonster/6e2e528faa7f2583eb88c9ebb3aac786 to your computer and use it in GitHub Desktop.
Decorator for enforcing strict type annotations on functions
import typing
import inspect
import functools
def strict_types(func: typing.Callable):
"""Decorator for enforcing strict type annotations"""
argspec = inspect.getfullargspec(func)
ret_type = argspec.annotations.pop('return', None)
@functools.wraps(func)
def wrapper(*args, **kwargs):
for arg, cls in argspec.annotations.items():
if arg in kwargs:
val = kwargs.get(arg)
else:
try:
i = argspec.args.index(arg)
val = args[i]
except ValueError:
continue
if val is not None and not isinstance(val, cls):
raise ValueError('Expected %r to be of type %r, '
'but got %r instead' % (arg, cls.__name__,
val.__class__.__name__))
ret_val = func(*args, **kwargs)
if ret_type and ret_val is not None and not isinstance(ret_val, ret_type):
raise ValueError('Expected return value to be of type %r, '
'but got %r instead' % (cls.__name__,
ret_val.__class__.__name__))
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment