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

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

2 точки общо

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

 1def type_check(mode):
 2    def types_func(*types):
 3        types_as_str = ', '.join(map(str, types))
 4        invalid_in_result = f"Invalid input arguments, expected {types_as_str}!"
 5        invalid_out_result = f"Invalid output value, expected {types_as_str}!"
 6        def decorator(func):
 7            def wrapper(*args, **kwargs):
 8                if mode == "in":
 9                    arg_list = list(map(lambda x: ('', x), args)) + list(kwargs.items())
10                    for key, value in arg_list:
11                        if not isinstance(value, types):
12                            print(invalid_in_result)
13                            break
14                decorated = func(*args, *kwargs) 
15                if mode == "out" and not isinstance(decorated, types):
16                    print(invalid_out_result)
17                return decorated
18            return wrapper
19        return decorator
20    return types_func

E...
======================================================================
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 14, in wrapper
decorated = func(*args, *kwargs)
^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 14, in wrapper
decorated = func(*args, *kwargs)
^^^^^^^^^^^^^^^^^^^^
TypeError: lonely_island() takes 2 positional arguments but 3 were given

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

FAILED (errors=1)

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