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

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

1 точки общо

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

 1def print_types(*allowed_types):
 2    str_of_types = ', '.join(str(el) for el in allowed_types)
 3    print(f'Invalid input arguments, expected {str_of_types}!')
 4
 5def validate_in(args, allowed_types):
 6    return all(type(arg) in allowed_types for arg in args)
 7
 8def validate_out(args, allowed_types):
 9    return type(args) in allowed_types
10
11def type_check(direction):
12    def decorator(*allowed_types):
13        def check(func):
14            def excecute_func(*args, **kwargs):
15                if direction == 'in':
16                    if not validate_in(args, allowed_types) or not validate_in(kwargs.items(), allowed_types):
17                        print_types(*allowed_types)
18                returned_values = func(*args, **kwargs)
19                if direction == 'out' and not validate_out(returned_values, allowed_types):
20                    print_types(*allowed_types)
21                return returned_values
22            return excecute_func
23        return check
24    return decorator

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 input arguments, 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 input arguments, expected my type!')]

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

FAILED (failures=2)

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