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

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

0 точки общо

0 успешни теста
4 неуспешни теста
Код

 1def type_check(context):
 2    def decorator(*types):
 3        def wrapper(func):
 4            def inner(*args, **kwargs):
 5                if context == 'in':
 6                    all_types_ok = True
 7                    types_list = args + tuple(kwargs.value())
 8                    for arg in types_list:
 9                        if not isinstance(arg, types):
10                            all_types_ok = False
11                            break
12                    if not all_types_ok:
13                        string_with_types = ','.join(
14                            [f'<{str(kind)}>'for kind in types])
15                        print(
16                            f"Invalid input arguments, expected {string_with_types}!")
17
18                result_from_function = func(*args, **kwargs)
19
20                if context == 'out':
21                    if not isinstance(result_from_function, types):
22                        string_with_types = ','.join(
23                            [f'{str(kind)}'for kind in types])
24                        print(
25                            f"Invalid output arguments, expected {string_with_types}!")
26                return result_from_function
27            return inner
28        return wrapper
29    return decorator

EEEF
======================================================================
ERROR: 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 97, in test_check_both
result = decorated("Captain Jack Sparrow", "Bill", pirates=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 18, in inner
result_from_function = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in inner
types_list = args + tuple(kwargs.value())
^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'value'. Did you mean: 'values'?

======================================================================
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 111, in test_check_decorated_exception
decorated({1, 2, 3, 4, 5, 6})
File "/tmp/solution.py", line 18, in inner
result_from_function = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in inner
types_list = args + tuple(kwargs.value())
^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'value'. Did you mean: 'values'?

======================================================================
ERROR: 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 42, in test_check_in
result = decorated('asdf', 'movie')
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in inner
types_list = args + tuple(kwargs.value())
^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'value'. Did you mean: 'values'?

======================================================================
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 arguments, expected my type!')]

----------------------------------------------------------------------
Ran 4 tests in 0.003s

FAILED (failures=1, errors=3)

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