n | def TypeCheck(types, result): | n | def TypeCheck(types, restype): |
| listed = list(types) | | ltypes = list(types) |
| llist = len(listed) | | lt = len(ltypes) |
| | | |
| def decorator(f): | | def decorator(f): |
n | def func(*args, **kwargs): | n | def newf(*args, **kwargs): |
| larg = len(args) | | la = len(args) |
| if(llist != larg + len(kwargs)): | | if(lt != la + len(kwargs)): |
| raise TypeError( | | raise TypeError( |
n | f"Function {f.__name__} must have {llist} arguments") | n | f"Function {f.__name__} must have {lt} arguments") |
| sort_k = sorted(kwargs.items(), key=lambda x: x[0]) | | kwas = sorted(kwargs.items(), key=lambda x: x[0]) |
| for i in range(larg): | | for i in range(la): |
| if (not isinstance(args[i], listed[i])): | | if (not isinstance(args[i], ltypes[i])): |
| raise TypeError( | | raise TypeError( |
n | f"Type of argument {i+1} is not {listed[i]}") | n | f"Type of argument {i+1} is not {ltypes[i]}") |
| for i in range(len(sort_k)): | | for i in range(len(kwas)): |
| if (not isinstance(sort_k[i][1], listed[i+larg])): | | if (not isinstance(kwas[i][1], ltypes[i+la])): |
| raise TypeError( | | raise TypeError( |
t | f"Type of argument '{sort_k[i][0]}' is not {listed[i]}") | t | f"Type of argument '{kwas[i][0]}' is not {ltypes[i]}") |
| r = f(*args, **kwargs) | | res = f(*args, **kwargs) |
| if(not isinstance(r, result)): | | if(not isinstance(res, restype)): |
| raise TypeError(f"Type of result is not {result}") | | raise TypeError(f"Type of result is not {restype}") |
| return r | | return res |
| return func | | return newf |
| return decorator | | return decorator |
| | | |