Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vectro opened this issue Jan 23, 2024 · 2 comments

Comments

@vectro
Copy link

vectro commented Jan 23, 2024

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:

  • Python 3.9.6
  • Overrides 7.6.0
  • typing-extensions 4.9.0
@vectro vectro changed the title AttributeError from overrides in conjunction with `typing.Literal' in subclass AttributeError from overrides in conjunction with typing_extensions.Literal in subclass Jan 23, 2024
@mkorpela
Copy link
Owner

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

@mkorpela
Copy link
Owner

249 to be more precise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants