RussBaz / enforce

Python 3.5+ runtime type checking for integration testing and data validation
543 stars 21 forks source link

late decoration #73

Closed obeleh closed 3 years ago

obeleh commented 6 years ago

added late decorating for forward references that refer to a class that is being constructed

the corresponding issue is #72

obeleh commented 6 years ago

The build fails because the dev branch already fails with this stacktrace:

Error
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/Users/sjuul/workspace/enforce/tests/enforce_tests/test_iterable_types.py", line 27, in test_iterator
    foo(ai)
  File "/Users/sjuul/workspace/enforce/enforce/decorators.py", line 117, in universal
    _args, _kwargs, _ = enforcer.validate_inputs(parameters)
  File "/Users/sjuul/workspace/enforce/enforce/enforcers.py", line 97, in validate_inputs
    process_errors(self.settings, self.validator.errors, self.hints)
  File "/Users/sjuul/workspace/enforce/enforce/enforcers.py", line 288, in process_errors
    processor(parser, exception, errors, hints, is_return_type)
  File "/Users/sjuul/workspace/enforce/enforce/exceptions.py", line 32, in process_errors
    raise_errors(exception, message)
  File "/Users/sjuul/workspace/enforce/enforce/exceptions.py", line 27, in raise_errors
    raise exception(message)
enforce.exceptions.RuntimeTypeError: 
  The following runtime type errors were encountered:
       Argument 'i' was not of type typing.Iterator[int]. Actual type was range_iterator.

('(Assertion) Field Guard for: typing.Union[typing.Dict[str, typing.Union[int, '
 'typing.List[int], NoneType]], typing.List[typing.Tuple[str, int]]]')
typing.Callable[..., NoneType]
SubTest failure: Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 523, in subTest
    yield
  File "/Users/sjuul/workspace/enforce/tests/test_protocol.py", line 134, in test_parent
    self.assertEqual(str(fields[k]), v)
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/teamcity/diff_tools.py", line 39, in _patched_equals
    raise native_error
  File "/Applications/PyCharm.app/Contents/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals
    old(self, first, second, msg)
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 829, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 1203, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 670, in fail
    raise self.failureException(msg)
AssertionError: '(Assertion) Field Guard for: typing.Callable[[str], str]' != '(Assertion) Field Guard for: typing.Callable[[typing.Type[~T], str], str]'
- (Assertion) Field Guard for: typing.Callable[[str], str]
+ (Assertion) Field Guard for: typing.Callable[[typing.Type[~T], str], str]
?                                               +++++++++++++++++

One or more subtests failed
Failed subtests list: (k='bar')

Ran 115 tests in 14.262s

FAILED (failures=1, errors=1, skipped=1)
obeleh commented 6 years ago

I've found a few other cases that should be fixed as well. Hang on.

obeleh commented 6 years ago

Also implemented using forward refs in parameters. And noticed that I added an extra frame to the stack when doing the late decorating. So I made the stack depth available as a parameter to get_universal_decorator