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