Предизвикателства > Abomination decorator > Решения > Решението на Енисел Кунч

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

2 точки общо

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

 1def check_input(args, expected_types):
 2    for arg in args:
 3        if not isinstance(arg, expected_types):
 4            expected_str = ', '.join(str(t) for t in expected_types)
 5            print(f"Invalid input arguments, expected {expected_str}!")
 6            return
 7
 8
 9def check_output(result, expected_types):
10    valid = isinstance(result, expected_types)
11    if not valid:
12        expected_str = ', '.join(str(t) for t in expected_types)
13        print(f"Invalid output value, expected {expected_str}")
14
15
16def type_check(check_type):
17    def wrapper(*types):
18        def decorator(func):
19            def inner(*args, **kwargs):
20                if check_type == "in":
21                    check_input(args, types)
22
23                result = func(*args, **kwargs)
24
25                if check_type == "out":
26                    check_output(result, types)
27
28                return result
29            return inner
30        return decorator
31    return wrapper

F..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 98, in test_check_both
mock_print.assert_has_calls(
File "/usr/lib/python3.12/unittest/mock.py", line 981, in assert_has_calls
raise AssertionError(
AssertionError: Calls not found.
Expected: [call("Invalid input arguments, expected <class 'float'>!"),
call("Invalid output value, expected <class 'int'>!")]
Actual: [call("Invalid input arguments, expected <class 'float'>!"),
call("Invalid output value, expected <class 'int'>")]

======================================================================
FAIL: test_check_out (test.TestTypeCheck.test_check_out)
The decorator should report an invalid "out" value.
----------------------------------------------------------------------
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 72, in test_check_out
mock_print.assert_has_calls([call(self.BASE_STRING_OUT.format("my type"))])
File "/usr/lib/python3.12/unittest/mock.py", line 981, in assert_has_calls
raise AssertionError(
AssertionError: Calls not found.
Expected: [call('Invalid output value, expected my type!')]
Actual: [call('Invalid output value, expected my type')]

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

FAILED (failures=2)

Дискусия
Виктор Бечев
25.10.2024 11:46

Заради снощният разговор приемаме, че downtime-а те е ограбил с една точка, за в бъдеще проверявай внимателно стринговете и какво качваш _(решението с правилната удивителна не успяхме да го намерим)_. :stuck_out_tongue:
История
Това решение има само една версия.