1def type_check(mode):
2 def decorator(*expected_types):
3 def checked(func):
4 def decorated(*args, **kwargs):
5 if mode == "in":
6 invalid_input = False
7 for arg in args:
8 if not isinstance(arg, expected_types):
9 invalid_input = True
10 break
11 for kwarg_value in kwargs.values():
12 if not isinstance(kwarg_value, expected_types):
13 invalid_input = True
14 break
15 if invalid_input:
16 print(f"Invalid input arguments, expected {', '.join(str(t) for t in expected_types)}!")
17
18 result = func(*args, **kwargs)
19 if mode == "out" and not isinstance(result, expected_types):
20 print(f"Invalid output value, expected {', '.join(str(t) for t in expected_types)}!")
21
22 return result
23 return decorated
24 return checked
25 return decorator
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK