mkorpela / overrides

A decorator to automatically detect mismatch when overriding a method
Apache License 2.0
261 stars 32 forks source link

Inner classes are recognized as methods by EnforceOverrides #107

Closed apirogov closed 2 years ago

apirogov commented 2 years ago
from overrides import EnforceOverrides, overrides

class SuperClass(EnforceOverrides):

    class Inner:
        test = 123

class Child(SuperClass):

    class Inner:
        field = "hello world"

results in:

Traceback (most recent call last):
  File "scratch3.py", line 9, in <module>
    class Child(SuperClass):
  File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/metador-core-AXME58BY-py3.8/lib/python3.8/site-packages/overrides/enforce.py", line 29, in __new__
    assert (
AssertionError: Method Inner overrides but does not have @overrides decorator

Adding the @overrides decorator to Inner then leads to

Traceback (most recent call last):
  File "scratch3.py", line 9, in <module>
    class Child(SuperClass):
  File "scratch3.py", line 12, in Child
    class Inner:
  File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/metador-core-AXME58BY-py3.8/lib/python3.8/site-packages/overrides/overrides.py", line 88, in overrides
    return _overrides(method, check_signature, check_at_runtime)
  File "/local/home/a.pirogov/.cache/pypoetry/virtualenvs/metador-core-AXME58BY-py3.8/lib/python3.8/site-packages/overrides/overrides.py", line 103, in _overrides
    for super_class in _get_base_classes(sys._getframe(3), method.__globals__):
AttributeError: type object 'Inner' has no attribute '__globals__'

In my opinion, inner classes and other "complicated entities" that are not normal methods etc. should probably be simply ignored as a fix for this issue.

There is not much that could be easily checked (apart from maybe that the inner class inherits from certain base classes / metaclass that can do further checks), but in any case that would be probably be something for another issue / feature request.

mkorpela commented 2 years ago

Thank you for the report!

mkorpela commented 2 years ago

@apirogov tested with overrides 7.0.0. This works.

from overrides import EnforceOverrides, overrides

class SuperClass(EnforceOverrides):
    class Inner:
        test = 123

class Child(SuperClass):
    @overrides
    class Inner:
        field = "hello world"
mkorpela commented 2 years ago

Seems that last version that did not work was 6.2.0. Based on this I'm closing this bug.

apirogov commented 2 years ago

Ah, my bad, I will upgrade then :) Thanks!