mkorpela / overrides

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

`AttributeError` from `overrides` in conjunction with `typing_extensions.Literal` in subclass #122

Open vectro opened 9 months ago

vectro commented 9 months ago

A test case is worth a thousand words so let's just demo that.

This code:

import overrides
import typing_extensions

Lit = typing_extensions.Literal["foo", "bar"]

class MyBaseClass(overrides.EnforceOverrides):
    def my_method(self, lit : Lit) -> None:
        return

class MySubClass(MyBaseClass):
    @overrides.override
    def my_method(self, lit : Lit) -> None:
        return

MySubClass().my_method("foo")

breaks with the following stack trace:

  File "script.py", line 10, in <module>
    class MySubClass(MyBaseClass):
  File "script.py", line 12, in MySubClass
    def my_method(self, lit : Lit) -> None:
  File "<venv>/lib/python3.9/site-packages/overrides/overrides.py", line 143, in override
    return _overrides(method, check_signature, check_at_runtime)
  File "<venv>/lib/python3.9/site-packages/overrides/overrides.py", line 172, in _overrides
    _validate_method(method, super_class, check_signature)
  File "<venv>/lib/python3.9/site-packages/overrides/overrides.py", line 191, in _validate_method
    ensure_signature_is_compatible(super_method, method, is_static)
  File "<venv>/lib/python3.9/site-packages/overrides/signature.py", line 104, in ensure_signature_is_compatible
    ensure_all_kwargs_defined_in_sub(
  File "<venv>/lib/python3.9/site-packages/overrides/signature.py", line 163, in ensure_all_kwargs_defined_in_sub
    and not _issubtype(super_type_hints[name], sub_type_hints[name])
  File "<venv>/lib/python3.9/site-packages/overrides/signature.py", line 42, in _issubtype
    return issubtype(left, right)
  File "<venv>/lib/python3.9/site-packages/overrides/typing_utils.py", line 463, in issubtype
    return _is_normal_subtype(normalize(left), normalize(right), forward_refs)
  File "<venv>/lib/python3.9/site-packages/overrides/typing_utils.py", line 426, in _is_normal_subtype
    return _is_origin_subtype_args(left.args, right.args, forward_refs)
  File "<venv>/lib/python3.9/site-packages/overrides/typing_utils.py", line 341, in _is_origin_subtype_args
    and left[-1].origin is not Ellipsis
AttributeError: 'str' object has no attribute 'origin'

Tested with:

mkorpela commented 9 months ago

Thank you for the less than 1000 words and the test case!

mkorpela commented 9 months ago

249 to be more precise.