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

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

2 точки общо

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

 1def type_check(stream_type):
 2    def decorator(*types):
 3        def wrapper(func):
 4            def in_check(*args, **kwargs):
 5                args_kwargs_combined = [arg for arg in args] + [value for value in kwargs.values()]
 6                for elem in args_kwargs_combined: 
 7                    if type(elem) not in types:
 8                        str_from_types = ", ".join(str(t) for t in types)
 9                        print("Invalid input arguments, expected " + str_from_types + "!")
10                        break
11                return func(*args, **kwargs)
12            def out_check(*args, **kwargs):
13                output = func(*args, **kwargs)
14                if type(output) not in types:
15                    str_from_types = ", ".join(str(elem) for elem in types)
16                    print("Invalid output value, expected " + str_from_types + "!")
17                return func(*args, **kwargs)
18            if stream_type == "in":
19                return in_check
20            elif stream_type == "out":
21                return out_check
22        return wrapper
23    return decorator

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.002s

FAILED (failures=1)

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