1def is_valid_type(valid_types, *args, **kwargs):
2 arg_list = list(map(lambda x: ('', x), args)) + list(kwargs.items())
3 for key, value in arg_list:
4 if type(value) not in valid_types:
5 return False
6 return True
7
8
9def type_check(message):
10 def decorator1(*valid_types):
11 def decorator2(func):
12 def check_type_of_arguments(*args, **kwargs):
13 if message == "in":
14 if not is_valid_type(valid_types, *args, **kwargs):
15 print(
16 "Invalid input arguments, expected {}!".format(
17 str(valid_types)
18 )
19 )
20 result = func(*args, **kwargs)
21 if message == 'out':
22 if not type(result) in valid_types:
23 print(
24 "Invalid output value, expected {}!".format(
25 str(valid_types)
26 )
27 )
28 return result
29
30 return check_type_of_arguments
31
32 return decorator2
33
34 return decorator1
FFFF
======================================================================
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'>,)!"),
call("Invalid output value, expected (<class 'int'>,)!")]
======================================================================
FAIL: test_check_decorated_exception (test.TestTypeCheck.test_check_decorated_exception)
The decorator should not supress any exceptions raised.
----------------------------------------------------------------------
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 112, in test_check_decorated_exception
self.assert_in_permutations(mock_print.mock_calls[0], types, self.BASE_STRING_IN)
File "/tmp/test.py", line 35, in assert_in_permutations
self.assertIn(call, call_permutations)
AssertionError: call("Invalid input arguments, expected (<class 'list'>, <class 'tuple'>)!") not found in [call("Invalid input arguments, expected <class 'list'>, <class 'tuple'>!"), call("Invalid input arguments, expected <class 'tuple'>, <class 'list'>!")]
======================================================================
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 44, in test_check_in
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 'list'>!")]
Actual: [call("Invalid input arguments, expected (<class 'list'>,)!")]
======================================================================
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!')]
Actual: [call('Invalid output value, expected (my type,)!')]
----------------------------------------------------------------------
Ran 4 tests in 0.003s
FAILED (failures=4)