Skip to content

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

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

Open
jamesbraza opened this issue Dec 16, 2020 · 0 comments
Labels
bug mypy got something wrong

Comments

@jamesbraza
Copy link
Contributor

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
@jamesbraza jamesbraza added the bug mypy got something wrong label Dec 16, 2020
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