1def check_for_invalid_types(arg_array, type_array, error_string):
2 for elem in arg_array:
3 if not list(filter(lambda current_type: isinstance(elem, current_type), type_array)):
4 print(error_string)
5 return
6
7
8def type_check(command):
9 def type_wrapper(*types):
10 def decorator(func):
11 def wrapper(*args, **kwargs):
12 result = None
13 if command == "in":
14 check_for_invalid_types([*args, *kwargs.values()],
15 types,
16 f"Invalid input arguments, expected {', '.join(map(str, types))}!",
17 )
18 result = func(*args, **kwargs)
19 elif command == "out":
20 result = func(*args, **kwargs)
21 check_for_invalid_types([result],
22 types,
23 f"Invalid output value, expected {', '.join(map(str, types))}!"
24 )
25 else:
26 result = func(*args, **kwargs) # ако тестваме за нещо извън (in out) не прави нищо
27 return result
28
29 return wrapper
30
31 return decorator
32
33 return type_wrapper
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK