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

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

2 точки общо

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

 1def type_check(mode):
 2    def condition(*valid_types):
 3        def decorator(func):
 4            def validation(*args, **kwargs):
 5                all_args = list(args) + list(kwargs.values())
 6                if mode == "in":
 7                    for arg in all_args:
 8                        if not isinstance(arg, valid_types):
 9                            print(f"Invalid input arguments, expected {", ".join(str(t) for t in valid_types)}!")
10                            break
11                    return func(*args, **kwargs)
12                if mode == "out":
13                    if type(func(*args, **kwargs)) not in valid_types:
14                        print(f"Invalid output value, expected {", ".join(str(t) for t in valid_types)}!")
15                    return func(*args, *kwargs)
16            return validation
17        return decorator
18    return condition

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

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

FAILED (errors=1)

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