Created
May 8, 2016 14:47
-
-
Save minus7/01b8a84984d1b99241b80d3ff36a3e2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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