1def type_check(in_or_out):
2 def expected_types(*types):
3 def decorator(func):
4 def wrapper(*args, **kwargs):
5 if in_or_out == "in":
6 for arg in args:
7 if not isinstance(arg, types):
8 # print(f"Баце, тия аргументи не ста'ат!")
9 types_expected = map(str, types)
10 print(f"Invalid input arguments, expected {", ".join(types_expected)}!")
11
12 result = func(*args, **kwargs)
13
14 if in_or_out == "out":
15 if not isinstance(result, types):
16 types_expected = map(str, types)
17 print(f"Invalid output value, expected {", ".join(types_expected)}!")
18
19 return result
20 return wrapper
21 return decorator
22 return expected_types
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 101, in test_check_both
self.assertEqual(mock_print.call_count, 2)
AssertionError: 3 != 2
======================================================================
FAIL: test_check_in (test.TestTypeCheck.test_check_in)
The decorator should report invalid "in" arguments.
----------------------------------------------------------------------
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 43, in test_check_in
self.assertEqual(mock_print.call_count, 1)
AssertionError: 2 != 1
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=2)