Skip to content

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

Closed as not planned
@nuiva

Description

@nuiva

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions