-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
I have found what is a very "in the weeds" false positive error. Basically, if a parent class has:
- Base class inherits from package with missing imports ignored in mypy config file
- Base class uses a
ClassVarwith aTypeVarinside - 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 ofChild.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 at990b6a6) - 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
Labels
bugmypy got something wrongmypy got something wrong