1INPUT_TYPE = "in"
2OUTPUT_TYPE = "out"
3SEPARATOR = ", "
4
5def type_check(io_type=INPUT_TYPE):
6 """ Decorator for type checking on function arguments or return value."""
7 def argument_check(*expected_types):
8 def decorator(func):
9 def body_of_decorator(*args, **kwargs):
10 expected_types_for_print = SEPARATOR.join(map(str, expected_types))
11 if io_type == INPUT_TYPE:
12 for arg in args:
13 if not isinstance(arg, expected_types):
14 print(f"Invalid input arguments, expected {expected_types_for_print}!")
15 break
16 result = func(*args, **kwargs)
17 if io_type == OUTPUT_TYPE and not isinstance(result, expected_types):
18 print(f"Invalid output value, expected {expected_types_for_print}!")
19 return result
20 return body_of_decorator
21 return decorator
22 return argument_check
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK