Skip to content

Instantly share code, notes, and snippets.

@minus7
Created May 8, 2016 14:47
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 minus7/01b8a84984d1b99241b80d3ff36a3e2c to your computer and use it in GitHub Desktop.
Save minus7/01b8a84984d1b99241b80d3ff36a3e2c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# vim: set noexpandtab:
from functools import wraps
from inspect import signature
def type_checked(func):
@wraps(func)
def wrapper(*args, **kwargs):
sig = signature(func)
bound = sig.bind(*args, **kwargs)
for k,v in bound.arguments.items():
ann = sig.parameters[k].annotation
if ann and not isinstance(v, ann):
raise ValueError("Argument {} should be of type {}, is {}"
.format(k, ann, type(v)))
return func(*bound.args, **bound.kwargs)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment