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

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

2 точки общо

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

 1IN = "in"
 2OUT = "out"
 3SEP = ", "
 4INVALID_INPUT = "Invalid input arguments, expected "
 5INVALID_OUTPUT = "Invalid output value, expected "
 6
 7def type_check(io):
 8    """Check types of input and output values for the function."""
 9    def expected_decorator(*types):
10        def decorator(func):
11            def inner(*args, **kwargs):
12                if io == IN:
13                    unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))
14                    if unexpected_types:
15                        print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!")
16                
17                actual_result = func(*args, **kwargs)
18
19                if io == OUT:
20                    if not isinstance(actual_result, types):
21                        print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!")
22                return actual_result
23            return inner
24        return decorator
25    return expected_decorator

.E..
======================================================================
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 17, in inner
actual_result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 13, in inner
unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'set'

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

FAILED (errors=1)

Дискусия
История

f1IN = "in"f1IN = "in"
2OUT = "out"2OUT = "out"
3SEP = ", "3SEP = ", "
4INVALID_INPUT = "Invalid input arguments, expected "4INVALID_INPUT = "Invalid input arguments, expected "
5INVALID_OUTPUT = "Invalid output value, expected "5INVALID_OUTPUT = "Invalid output value, expected "
66
7def type_check(io):7def type_check(io):
8    """Check types of input and output values for the function."""8    """Check types of input and output values for the function."""
9    def expected_decorator(*types):9    def expected_decorator(*types):
10        def decorator(func):10        def decorator(func):
11            def inner(*args, **kwargs):11            def inner(*args, **kwargs):
12                if io == IN:12                if io == IN:
t13                    unexpected_types = list(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))t13                    unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))
14                    if unexpected_types:14                    if unexpected_types:
15                        print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!")15                        print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!")
16                16                
17                actual_result = func(*args, **kwargs)17                actual_result = func(*args, **kwargs)
1818
19                if io == OUT:19                if io == OUT:
20                    if not isinstance(actual_result, types):20                    if not isinstance(actual_result, types):
21                        print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!")21                        print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!")
22                return actual_result22                return actual_result
23            return inner23            return inner
24        return decorator24        return decorator
25    return expected_decorator25    return expected_decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op