f | import types | f | import types |
| | | |
| | | |
n | def TypeCheck(argtypes, res): | n | def TypeCheck(arg, r): |
| if isinstance(argtypes, types.GeneratorType): | | if isinstance(arg, types.GeneratorType): |
| argtypes = list(argtypes) | | arg = list(arg) |
| | | |
n | def decorator(fun): | n | def de(f): |
| def newfun(*args, **kwargs): | | def n_f(*args, **kwargs): |
| if len(args) + len(kwargs) != len(argtypes): | | if len(kwargs)+len(args) != len(arg): |
| if not fun.__defaults__ or len( | | if not f.__defaults__ or len( |
| fun.__defaults__) + len(args) + len(kwargs) != len(argtypes): | | f.__defaults__) + len(kwargs) + len(args) != len(arg): |
| raise TypeError('Function ' + | | raise TypeError('Function ' + |
n | fun.__code__.co_name + | n | f.__code__.co_name + |
| ' must have ' + | | ' must have ' + |
n | str(len(argtypes)) + | n | str(len(arg)) + |
| ' arguments') | | ' arguments') |
n | for index, arg in enumerate(args): | n | for index, argss in enumerate(args): |
| if not isinstance(arg, argtypes[index]): | | if not isinstance(argss, arg[index]): |
| raise TypeError( | | raise TypeError('Type of argument ' + |
| 'Type of argument ' + str(index + 1) + ' is not ' + str(argtypes[index])) | | str(index + 1) + ' is not ' + str(arg[index])) |
| named = 0 | | n = 0 |
| for kw, val in kwargs.items(): | | for k, v in kwargs.items(): |
| if kw in fun.__code__.co_varnames and not isinstance( | | if k in f.__code__.co_varnames and not isinstance( |
| val, argtypes[fun.__code__.co_varnames.index(kw)]): | | v, arg[f.__code__.co_varnames.index(k)]): |
| named += 1 | | n += 1 |
| raise TypeError('Type of argument \'' + | | raise TypeError('Type of argument \'' + |
n | kw + | n | k + |
| '\' is not ' + | | '\' is not ' + |
t | str(argtypes[fun.__code__.co_varnames.index(kw)])) | t | str(arg[f.__code__.co_varnames.index(k)])) |
| result = fun(*args, **kwargs) | | otv = f(*args, **kwargs) |
| if not isinstance(result, res): | | if not isinstance(otv, r): |
| raise TypeError('Type of result is not ' + str(res)) | | raise TypeError('Type of result is not ' + str(r)) |
| return result | | return otv |
| return newfun | | return n_f |
| return decorator | | return de |
| | | |