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

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

0 точки общо

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

 1def type_check(check_type):
 2    def expected_types(*type_expected):
 3        def decorator(func):
 4            def inner(*args, **kwargs):
 5                if check_type == 'in':
 6                    for arg in args:
 7                        if not isinstance(arg, type_expected):
 8                            expected = ""
 9                            for t in expected_types:
10                                expected =  " ,".join(str(t))
11                            print(f"Invalid input arguments, expected {expected}!")
12                            break
13                result = func(*args, **kwargs)
14                
15                if check_type == 'out':
16                    for arg in args:
17                        if not isinstance(result, type_expected):
18                            expected = ""
19                            for t in expected_types:
20                                expected = " ,".join(str(t))
21                            print(f"Invalid output value, expected {expected}!")
22                return result
23            return inner
24        return decorator
25    return expected_types
26                
27                            
28                                
29                            
30
31
32@type_check("in")(int, float)
33@type_check("out")(int, float)
34def power(num, power):
35    return num ** power
36
37@type_check("in")(str)
38@type_check("out")(str)
39def concatenate(*strings, separator=' beep boop '):
40    return separator.join(strings)

EEEF
======================================================================
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 13, in inner
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 9, in inner
for t in expected_types:
TypeError: 'function' object is not iterable

======================================================================
ERROR: test_check_decorated_exception (test.TestTypeCheck.test_check_decorated_exception)
The decorator should not supress any exceptions raised.
----------------------------------------------------------------------
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 111, in test_check_decorated_exception
decorated({1, 2, 3, 4, 5, 6})
File "/tmp/solution.py", line 13, in inner
result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 9, in inner
for t in expected_types:
TypeError: 'function' object is not iterable

======================================================================
ERROR: test_check_in (test.TestTypeCheck.test_check_in)
The decorator should report invalid "in" arguments.
----------------------------------------------------------------------
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 42, in test_check_in
result = decorated('asdf', 'movie')
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 9, in inner
for t in expected_types:
TypeError: 'function' object is not iterable

======================================================================
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!')]

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

FAILED (failures=1, errors=3)

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