1def type_check(*args_types):
2 def put_type_decorator(put_type):
3 def func_decorator(func):
4 def arguments_decorator(*args, **kwargs):
5 args_list = list(map(lambda x: ('', x), args)) + list(kwargs.items())
6 if put_type is 'in':
7 for key, value in args_list:
8 if type(value) not in args_types:
9 print('Invalid input value, expected {}!'.format(', '.join(str(arg) for arg in args_types)))
10 break
11 func_result = func(args, kwargs)
12 if put_type is 'out':
13 if type(func_result) not in args_types:
14 print('Invalid output value, expected {}!'.format(', '.join(str(arg) for arg in args_types)))
15 return func_result
16 return arguments_decorator
17 return func_decorator
18 return put_type_decorator
/tmp/solution.py:6: SyntaxWarning: "is" with 'str' literal. Did you mean "=="?
if put_type is 'in':
/tmp/solution.py:12: SyntaxWarning: "is" with 'str' literal. Did you mean "=="?
if put_type is 'out':
FEFE
======================================================================
ERROR: 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 108, in test_check_decorated_exception
decorated = type_check("in")(*types)(divide_by_zero)
^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: type_check.<locals>.put_type_decorator() takes 1 positional argument but 2 were given
======================================================================
ERROR: 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 71, in test_check_out
result = decorated()
^^^^^^^^^^^
File "/tmp/solution.py", line 11, in arguments_decorator
func_result = func(args, kwargs)
^^^^^^^^^^^^^^^^^^
TypeError: nothing() takes 0 positional arguments but 2 were given
======================================================================
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'>!")]
======================================================================
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: 0 != 1
----------------------------------------------------------------------
Ran 4 tests in 0.002s
FAILED (failures=2, errors=2)