Предизвикателства > Abomination decorator > Решения > Решението на Елица Павлова

Резултати
1 точки от тестове
0 точки от учител

1 точки общо

1 успешни теста
3 неуспешни теста
Код

 1def type_check(in_or_out):
 2    def allowed_types(*types):
 3        def check_this(func):
 4           def decorator(*args, **kwargs):
 5               arg_list = list(args) + list(kwargs)
 6               if in_or_out == 'in':
 7                   for value in arg_list:
 8                       if not isinstance(value, types):
 9                           types_to_print = ', '.join(str(type) for type in types)
10                           print(f"Invalid input arguments, expected {types_to_print}!")
11                           break
12               elif in_or_out == 'out':
13                   for value in arg_list:
14                       if not isinstance(value, types):
15                           types_to_print = ', '.join(str(type) for type in types)
16                           print(f"Invalid output value, expected {types_to_print}!")
17                           break
18               return func(*args, **kwargs)
19           return decorator
20        return check_this
21    return allowed_types

FF.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 output value, expected <class 'int'>!"),
call("Invalid input arguments, expected <class 'float'>!")]

======================================================================
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 output value, 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_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=3)

Дискусия
История
Това решение има само една версия.