1def type_check(in_or_out):
2 def types_handler(*types):
3 def decorator(func):
4 def wrapper(*args, **kwargs):
5 types_str = ", ".join(str(t) for t in types)
6 if in_or_out == "in":
7 for arg in list(args) + list(kwargs.values()):
8 if not isinstance(arg, types):
9 print(f"Invalid input arguments, expected {types_str}!")
10 break;
11
12 res = func(*args, **kwargs)
13 if in_or_out == "out" and isinstance(res, types):
14 print(f"Invalid output value, expected {types_str}!")
15 return res
16 return wrapper
17 return decorator
18 return types_handler
F..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 98, in test_check_both
mock_print.assert_has_calls(
File "/usr/lib/python3.12/unittest/mock.py", line 981, in assert_has_calls
raise AssertionError(
AssertionError: Calls not found.
Expected: [call("Invalid input arguments, expected <class 'float'>!"),
call("Invalid output value, expected <class 'int'>!")]
Actual: [call("Invalid input arguments, expected <class 'float'>!")]
======================================================================
FAIL: test_check_out (test.TestTypeCheck.test_check_out)
The decorator should report an invalid "out" value.
----------------------------------------------------------------------
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 72, in test_check_out
mock_print.assert_has_calls([call(self.BASE_STRING_OUT.format("my type"))])
File "/usr/lib/python3.12/unittest/mock.py", line 981, in assert_has_calls
raise AssertionError(
AssertionError: Calls not found.
Expected: [call('Invalid output value, expected my type!')]
----------------------------------------------------------------------
Ran 4 tests in 0.003s
FAILED (failures=2)