1""" Verify the input and output data types of a function. """
2
3
4def type_check(io_command):
5 """ Check if the command is in- or output."""
6 def type_requirement(*types):
7 """ Check if the input data matches the required types """
8 def decorator(func):
9 def operation(*args, **kwargs):
10 expected_types = ", ".join(str(every_type) for every_type in types)
11 if io_command == "in":
12 for arg in args:
13 if not any(isinstance(arg, every_type) for every_type in types):
14 print(f"Invalid input arguments, expected {expected_types}!")
15
16 result = func(*args, **kwargs)
17
18 if io_command == "out" and not any(isinstance(result, every_type) for every_type in types):
19 print(f"Invalid output value, expected {expected_types}!")
20
21 return result
22 return operation
23 return decorator
24 return type_requirement
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 101, in test_check_both
self.assertEqual(mock_print.call_count, 2)
AssertionError: 3 != 2
======================================================================
FAIL: 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 43, in test_check_in
self.assertEqual(mock_print.call_count, 1)
AssertionError: 2 != 1
----------------------------------------------------------------------
Ran 4 tests in 0.003s
FAILED (failures=2)
f | 1 | """ Verify the input and output data types of a function. """ | f | 1 | """ Verify the input and output data types of a function. """ |
2 | 2 | ||||
3 | 3 | ||||
4 | def type_check(io_command): | 4 | def type_check(io_command): | ||
5 | """ Check if the command is in- or output.""" | 5 | """ Check if the command is in- or output.""" | ||
6 | def type_requirement(*types): | 6 | def type_requirement(*types): | ||
7 | """ Check if the input data matches the required types """ | 7 | """ Check if the input data matches the required types """ | ||
8 | def decorator(func): | 8 | def decorator(func): | ||
9 | def operation(*args, **kwargs): | 9 | def operation(*args, **kwargs): | ||
10 | expected_types = ", ".join(str(every_type) for every_type in types) | 10 | expected_types = ", ".join(str(every_type) for every_type in types) | ||
11 | if io_command == "in": | 11 | if io_command == "in": | ||
12 | for arg in args: | 12 | for arg in args: | ||
13 | if not any(isinstance(arg, every_type) for every_type in types): | 13 | if not any(isinstance(arg, every_type) for every_type in types): | ||
14 | print(f"Invalid input arguments, expected {expected_types}!") | 14 | print(f"Invalid input arguments, expected {expected_types}!") | ||
15 | 15 | ||||
16 | result = func(*args, **kwargs) | 16 | result = func(*args, **kwargs) | ||
17 | 17 | ||||
18 | if io_command == "out" and not any(isinstance(result, every_type) for every_type in types): | 18 | if io_command == "out" and not any(isinstance(result, every_type) for every_type in types): | ||
t | 19 | print(f"Invalid output arguments, expected {expected_types}!") | t | 19 | print(f"Invalid output value, expected {expected_types}!") |
20 | 20 | ||||
21 | return result | 21 | return result | ||
22 | return operation | 22 | return operation | ||
23 | return decorator | 23 | return decorator | ||
24 | return type_requirement | 24 | return type_requirement |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | """ Verify the input and output data types of a function. """ | f | 1 | """ Verify the input and output data types of a function. """ |
2 | 2 | ||||
3 | 3 | ||||
4 | def type_check(io_command): | 4 | def type_check(io_command): | ||
5 | """ Check if the command is in- or output.""" | 5 | """ Check if the command is in- or output.""" | ||
6 | def type_requirement(*types): | 6 | def type_requirement(*types): | ||
7 | """ Check if the input data matches the required types """ | 7 | """ Check if the input data matches the required types """ | ||
8 | def decorator(func): | 8 | def decorator(func): | ||
9 | def operation(*args, **kwargs): | 9 | def operation(*args, **kwargs): | ||
10 | expected_types = ", ".join(str(every_type) for every_type in types) | 10 | expected_types = ", ".join(str(every_type) for every_type in types) | ||
11 | if io_command == "in": | 11 | if io_command == "in": | ||
12 | for arg in args: | 12 | for arg in args: | ||
13 | if not any(isinstance(arg, every_type) for every_type in types): | 13 | if not any(isinstance(arg, every_type) for every_type in types): | ||
14 | print(f"Invalid input arguments, expected {expected_types}!") | 14 | print(f"Invalid input arguments, expected {expected_types}!") | ||
15 | 15 | ||||
16 | result = func(*args, **kwargs) | 16 | result = func(*args, **kwargs) | ||
17 | 17 | ||||
18 | if io_command == "out" and not any(isinstance(result, every_type) for every_type in types): | 18 | if io_command == "out" and not any(isinstance(result, every_type) for every_type in types): | ||
t | 19 | print(f"Invalid output value, expected {expected_types}!") | t | 19 | print(f"Invalid output arguments, expected {expected_types}!") |
20 | 20 | ||||
21 | return result | 21 | return result | ||
22 | return operation | 22 | return operation | ||
23 | return decorator | 23 | return decorator | ||
24 | return type_requirement | 24 | return type_requirement |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|