1IN = "in"
2OUT = "out"
3SEP = ", "
4INVALID_INPUT = "Invalid input arguments, expected "
5INVALID_OUTPUT = "Invalid output value, expected "
6
7def type_check(io):
8 """Check types of input and output values for the function."""
9 def expected_decorator(*types):
10 def decorator(func):
11 def inner(*args, **kwargs):
12 if io == IN:
13 unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))
14 if unexpected_types:
15 print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!")
16
17 actual_result = func(*args, **kwargs)
18
19 if io == OUT:
20 if not isinstance(actual_result, types):
21 print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!")
22 return actual_result
23 return inner
24 return decorator
25 return expected_decorator
.E..
======================================================================
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 17, in inner
actual_result = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 13, in inner
unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values())))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'set'
----------------------------------------------------------------------
Ran 4 tests in 0.003s
FAILED (errors=1)
f | 1 | IN = "in" | f | 1 | IN = "in" |
2 | OUT = "out" | 2 | OUT = "out" | ||
3 | SEP = ", " | 3 | SEP = ", " | ||
4 | INVALID_INPUT = "Invalid input arguments, expected " | 4 | INVALID_INPUT = "Invalid input arguments, expected " | ||
5 | INVALID_OUTPUT = "Invalid output value, expected " | 5 | INVALID_OUTPUT = "Invalid output value, expected " | ||
6 | 6 | ||||
7 | def type_check(io): | 7 | def type_check(io): | ||
8 | """Check types of input and output values for the function.""" | 8 | """Check types of input and output values for the function.""" | ||
9 | def expected_decorator(*types): | 9 | def expected_decorator(*types): | ||
10 | def decorator(func): | 10 | def decorator(func): | ||
11 | def inner(*args, **kwargs): | 11 | def inner(*args, **kwargs): | ||
12 | if io == IN: | 12 | if io == IN: | ||
t | 13 | unexpected_types = list(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values()))) | t | 13 | unexpected_types = set(filter(lambda x: not isinstance(x, types), args + tuple(kwargs.values()))) |
14 | if unexpected_types: | 14 | if unexpected_types: | ||
15 | print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!") | 15 | print(INVALID_INPUT + SEP.join([str(s) for s in types]) + "!") | ||
16 | 16 | ||||
17 | actual_result = func(*args, **kwargs) | 17 | actual_result = func(*args, **kwargs) | ||
18 | 18 | ||||
19 | if io == OUT: | 19 | if io == OUT: | ||
20 | if not isinstance(actual_result, types): | 20 | if not isinstance(actual_result, types): | ||
21 | print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!") | 21 | print(INVALID_OUTPUT + SEP.join([str(s) for s in types]) + "!") | ||
22 | return actual_result | 22 | return actual_result | ||
23 | return inner | 23 | return inner | ||
24 | return decorator | 24 | return decorator | ||
25 | return expected_decorator | 25 | return expected_decorator |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|