KEMBAR78
Presence of __init__ in parent class triggering false positive error: Access to generic class variables is ambiguous · Issue #9815 · python/mypy · GitHub
Skip to content

Presence of __init__ in parent class triggering false positive error: Access to generic class variables is ambiguous #9815

@jamesbraza

Description

@jamesbraza

Bug Report

I have found what is a very "in the weeds" false positive error. Basically, if a parent class has:

  1. Base class inherits from package with missing imports ignored in mypy config file
  2. Base class uses a ClassVar with a TypeVar inside
  3. Child class populates the TypeVar

If this happens, everything works as expected. However, if you override the __init__ method in the base class, it exposes a false positive error "Access to generic class variables is ambiguous" in mypy.

To Reproduce

from typing import ClassVar, Generic, TypeVar

from Phidget22.Phidget import Phidget as Phidget22PhidgetAPI  # Ignored in config

TKeys = TypeVar("TKeys")

class Base(Phidget22PhidgetAPI, Generic[TKeys]):

    foo: ClassVar[TKeys]

    # If this is not commented out, the bug surfaces
    def __init__(self):
        super().__init__()

reveal_type(Base.foo)

class Child(Base[str]):

    foo: ClassVar[str] = "bar"
    reveal_type(foo)

reveal_type(Child.foo)

Expected Behavior

With the __init__ commented out in Base, there is good behavior:

path/to/classvar_ambiguous_access.py:15:13: error: Access to generic class variables is ambiguous  [misc]
path/to/classvar_ambiguous_access.py:15:13: note: Revealed type is 'Any'
path/to/classvar_ambiguous_access.py: note: In class "Child":
path/to/classvar_ambiguous_access.py:20:17: note: Revealed type is 'builtins.str'
path/to/classvar_ambiguous_access.py: note: At top level:
path/to/classvar_ambiguous_access.py:22:13: note: Revealed type is 'builtins.str'

It makes sense that mypy warns about ambiguous access here in line 15, per #2878.

Actual Behavior

With the __init__ uncommented in Base, the false positive bug surfaces (see 2nd to last line):

path/to/classvar_ambiguous_access.py:15:13: error: Access to generic class variables is ambiguous  [misc]
path/to/classvar_ambiguous_access.py:15:13: note: Revealed type is 'Any'
path/to/classvar_ambiguous_access.py: note: In class "Child":
path/to/classvar_ambiguous_access.py:20:17: note: Revealed type is 'builtins.str'
path/to/classvar_ambiguous_access.py: note: At top level:
path/to/classvar_ambiguous_access.py:22:13: error: Access to generic class variables is ambiguous  [misc]
path/to/classvar_ambiguous_access.py:22:13: note: Revealed type is 'Any'

Here we see:

  • In the body of Child, mypy properly parses the type of Child.foo
  • Outside of the body at the module-level:
    • mypy now has a false positive error "Access to generic class variables is ambiguous"
    • mypy also now doesn't know the type of Child.foo

Your Environment

  • Mypy version used: mypy==0.800+dev.990b6a6f03ad82c10cb8edbe70508b943336f7f3 (current master at 990b6a6)
  • Mypy command-line flags: none notable
  • Mypy configuration options from mypy.ini (and other config files): setup.cfg
[mypy-Phidget22.*]
ignore_missing_imports = True
  • Python version used: Python==3.8.6
  • Operating system and version: macOS Catalina Version 10.15.7

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