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

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

1 точки общо

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

 1def type_check(command):
 2    def type_check2(*possible_types):
 3        def decorator(func):
 4            def decorator1(*args, **kwargs):
 5                if command == "in":
 6                    for arg in args:
 7                        arg_type = type(arg)
 8                        if arg_type not in possible_types:
 9                            print(f"Invalid input arguments, expected {str(possible_types)[1:-1]}!")
10                            break
11                    for key,value in kwargs.items():
12                        arg_type = type(value)
13                        if arg_type not in possible_types:
14                            print(f"Invalid input arguments, expected {str(possible_types)[1:-1]}!")
15                            break
16                elif command == "out":
17                    result = func(*args,**kwargs)
18                    result_type=type(result)
19                    if result_type not in possible_types:
20                        print(f"Invalid output value, expected {str(possible_types)[1:-1]}!")
21                else:
22                    print(\\_(ツ)_/¯")
23                return func(*args,**kwargs)
24            return decorator1
25        return decorator
26    return type_check2

F.FF
======================================================================
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 'float'>,!"),
call("Invalid output value, expected <class 'int'>,!"),
call("Invalid input arguments, expected <class 'float'>,!"),
call("Invalid input arguments, expected <class 'float'>,!")]

======================================================================
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 44, in test_check_in
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 'list'>!")]
Actual: [call("Invalid input arguments, expected <class 'list'>,!")]

======================================================================
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=3)

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