Created
October 29, 2014 11:16
-
-
Save ryanwang520/dea41945dc6f8dffcd4a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def typeassert(*ty_args, **ty_kwargs): | |
| def decorate(func): | |
| if not __debug__: | |
| return func | |
| sig = signature(func) | |
| bound_types = sig.bind_partial(*ty_args, **ty_kwargs).arguments | |
| @wraps(func) | |
| def wrapper(*args, **kwargs): | |
| bound_values = sig.bind(*args, **kwargs) | |
| for name, value in bound_values.arguments.items(): | |
| if name in bound_types: | |
| if not isinstance(value, bound_types[name]): | |
| raise TypeError( | |
| 'Argument {} must be {}'.format( | |
| name, bound_types[name])) | |
| return func(*args, **kwargs) | |
| return wrapper | |
| return decorate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment