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

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

2 точки общо

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

 1def type_check(input_output_mode):
 2    """Performs mode checking."""
 3    def var_types(*input_types):
 4        """Performs variables types checking."""
 5        def decorator(func):
 6            """Decorates the function."""
 7            def wrapper(*args, **kwargs):
 8                """Wraps the functioin."""
 9                if input_output_mode == "in":
10                    for arg in args:
11                        if not isinstance(arg, input_types):
12                            print(f"Invalid input arguments, expected {', '.join(str(curent_type) for curent_type in input_types)}!")
13                            break
14                    for kwarg_value in kwargs.values():
15                        if not isinstance(kwarg_value, input_types):
16                            print(f"Invalid input arguments, expected {', '.join(str(curent_type) for curent_type in input_types)}!")
17                            break
18
19                decorated = func(*args, **kwargs)
20
21                if input_output_mode == "out":
22                    if not isinstance(decorated, input_types):
23                        print(f"Invalid output value, expected {', '.join(str(curent_type) for curent_type in input_types)}!")
24                return decorated
25            return wrapper
26        return decorator
27    return var_types

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 101, in test_check_both
self.assertEqual(mock_print.call_count, 2)
AssertionError: 3 != 2

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

FAILED (failures=1)

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