1INPUT_ERROR_MESSAGE = 'Invalid input arguments, expected'
2OUTPUT_ERROR_MESSAGE = 'Invalid output value, expected'
3
4
5def validate(error_message, outputs, valid_types):
6 if not all(isinstance(output, valid_types) for output in outputs):
7 expected_types = ', '.join(str(current_type) for current_type in valid_types)
8 print(f'{error_message} {expected_types}!')
9
10
11def type_check(criteria):
12 def type_check_by_criteria(*outer_args, **outer_kwargs):
13 valid_types = tuple(outer_args) + tuple(outer_kwargs.values())
14
15 def decorator(func):
16 def decorated(*args, **kwargs):
17 if criteria == 'in':
18 validate(INPUT_ERROR_MESSAGE, tuple(args) + tuple(kwargs.values()), valid_types)
19
20 result = func(*args, **kwargs)
21
22 if criteria == 'out':
23 validate(OUTPUT_ERROR_MESSAGE, tuple(args) + tuple(kwargs.values()), valid_types)
24
25 return result
26
27 return decorated
28
29 return decorator
30
31 return type_check_by_criteria
...F
======================================================================
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.002s
FAILED (failures=1)