Skip to content

Instantly share code, notes, and snippets.

@JokerQyou
Last active Aug 29, 2015
Embed
What would you like to do?
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