1"""This module checks the types of input arguments and output values of decorated functions."""
2def type_check(mode):
3 """Check the types of input arguments and output values."""
4 def expected_types(*types):
5 def decorator(func):
6 def wrapper(*args, **kwargs):
7 str_represent_type = ", ".join(str(exp_type) for exp_type in types)
8 if mode == "in":
9 if not all(isinstance(argument, types) for argument in args):
10 print(f"Invalid input arguments, expected {str_represent_type}!")
11 result = func(*args, **kwargs)
12 if mode == "out":
13 if not isinstance(result, types):
14 print(f"Invalid output value, expected {str_represent_type}!")
15 return result
16 return wrapper
17 return decorator
18 return expected_types
....
----------------------------------------------------------------------
Ran 4 tests in 0.002s
OK