1def type_check(type_of_check):
2 """Validate input/output of decorated function."""
3 def check_types(*args):
4 types_as_strings = set(map(str, args))
5 def decorator(func):
6 def evaluate(*args, **kwargs):
7 if isinstance(type_of_check, str):
8 if type_of_check == "in":
9 parameters_types = list(map(str, map(type, args)))
10 parameters_types.extend(list(map(str, map(type, kwargs.values()))))
11 parameters_types = set(parameters_types)
12 for param in parameters_types:
13 if param not in types_as_strings:
14 print(f"Invalid input arguments, expected {", ".join(types_as_strings)}!")
15 return func(*args, **kwargs)
16 elif (type_of_check == "out"
17 and str(type(func(*args, **kwargs))) not in types_as_strings):
18 print(f"Invalid output value, expected {", ".join(types_as_strings)}!")
19 return func(*args, **kwargs)
20 return evaluate
21 return decorator
22 return check_types
F...
======================================================================
FAIL: test_check_both (test.TestTypeCheck.test_check_both)
The decorator should report invalid "in" and "out" together.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python3.12/unittest/mock.py", line 1390, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/test.py", line 101, in test_check_both
self.assertEqual(mock_print.call_count, 2)
AssertionError: 3 != 2
----------------------------------------------------------------------
Ran 4 tests in 0.003s
FAILED (failures=1)