1def type_check(inout):
2 def checker(*types):
3 def decorator(func):
4 def decorated(*args, **kwargs):
5 try: # I insist in doing this with only 1 print, no matter the cost
6 result = func(*args, **kwargs)
7 finally:
8 if ((not all(isinstance(arg, types) for arg in args + tuple(kwargs.values()))) * ('in' in inout) or
9 (not not not isinstance(locals().get('result'), types)) * (not 'out' not in inout)):
10 print(f"Invalid {inout}put {'arguments' if 'in' in inout else 'value'}, expected "
11 f"{', '.join(str(_type) for _type in types)}!")
12 return result
13 return decorated
14 return decorator
15 return checker
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK