1def handle_input(valid_types, *args, **kwargs):
2 """Check if the types of the arguments are valid."""
3 all_args = args + tuple(kwargs.values())
4 for arg in all_args:
5 if not isinstance(arg, valid_types):
6 formatted_message = ', '.join([str(valid_type) for valid_type in valid_types])
7 print(f'Invalid input arguments, expected {formatted_message}!')
8 return
9
10
11def handle_output(result, valid_types):
12 """Check if the type of the returned value is valid."""
13 if not isinstance(result, valid_types):
14 formatted_message = ', '.join([str(valid_type) for valid_type in valid_types])
15 print(f'Invalid output value, expected {formatted_message}!')
16
17
18def type_check(io_operation):
19 """Verify input and output of a function."""
20 def valid_types_wrapper(*valid_types):
21 def decorator(func):
22 def function_arguments_wrapper(*args, **kwargs):
23 if io_operation == 'in':
24 handle_input(valid_types, *args, **kwargs)
25 result = func(*args, **kwargs)
26 if io_operation == 'out':
27 handle_output(result, valid_types)
28 return result
29 return function_arguments_wrapper
30 return decorator
31 return valid_types_wrapper
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK