Last active
August 29, 2015 14:12
-
-
Save JokerQyou/c2dd51f21b3f018166a6 to your computer and use it in GitHub Desktop.
Type check
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
import functools, inspect | |
def _strict_type(type_map): | |
''' | |
Check func parameter type | |
:param f: func | |
:param type_map: mapping of parameter types | |
:return: | |
''' | |
def _wrapped(f): | |
@functools.wraps(f) | |
def _check_types(f, *posargs, **kwargs): | |
args = inspect.getcallargs(f, *posargs, **kwargs) | |
for k, v in args.iteritems(): | |
for _k, _v in type_map.iteritems(): | |
if k == _k and type(v) != v: | |
raise RuntimeError, u'func `%s` expected `%s` to be `%s` type but got `%s` type' % (f.func_name, _k, str(_v), str(type(v)), ) | |
return f(*posargs, **kwargs) | |
return _check_types | |
return _wrapped | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment