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