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

"Access to generic class variables is ambiguous" with CRTP #11063

Open
nuiva opened this issue Sep 6, 2021 · 0 comments
Open

"Access to generic class variables is ambiguous" with CRTP #11063

nuiva opened this issue Sep 6, 2021 · 0 comments
Labels
bug mypy got something wrong

Comments

@nuiva
Copy link

nuiva commented Sep 6, 2021

Bug Report

Mypy complains that "Access to generic class variables is ambiguous" when using the CRTP technique, even though the type of the class variable in question is actually known.

To Reproduce

Save the following Python program:

from typing import TypeVar, Type, Generic, ClassVar, Any

T = TypeVar("T", bound="Base[Any]")

class Base(Generic[T]):
    instance_list: ClassVar[list[T]]
    @classmethod
    def init(cls: Type[T]) -> None:
        cls.instance_list = []

class Derived(Base["Derived"]):
    pass

Derived.init()
print(Derived.instance_list)

Actual Behavior

Executing mypy test.py outputs:

test.py:15: error: Access to generic class variables is ambiguous
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

Mypy should not output errors, because Derived inherits the class Base[Derived] and therefore Derived has the member variable instance_list: ClassVar[list[Derived]]. Clearly the type is not generic or ambiguous at this point.

Your Environment

  • Mypy version used: mypy 0.910
  • Mypy command-line flags: none, see above
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.9.6
  • Operating system and version: Windows 10.0.19042
@nuiva nuiva added the bug mypy got something wrong label Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant