Skip to content

Instantly share code, notes, and snippets.

@JokerQyou
Last active August 29, 2015 14:12
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 JokerQyou/c2dd51f21b3f018166a6 to your computer and use it in GitHub Desktop.
Save JokerQyou/c2dd51f21b3f018166a6 to your computer and use it in GitHub Desktop.
Type check
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