You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ClassVar with a TypeVar inside
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
fromtypingimportClassVar, Generic, TypeVarfromPhidget22.PhidgetimportPhidgetasPhidget22PhidgetAPI# Ignored in configTKeys=TypeVar("TKeys")
classBase(Phidget22PhidgetAPI, Generic[TKeys]):
foo: ClassVar[TKeys]
# If this is not commented out, the bug surfacesdef__init__(self):
super().__init__()
reveal_type(Base.foo)
classChild(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
The text was updated successfully, but these errors were encountered:
Bug Report
I have found what is a very "in the weeds" false positive error. Basically, if a parent class has:
ClassVar
with aTypeVar
insideTypeVar
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
Expected Behavior
With the
__init__
commented out inBase
, there is good behavior:It makes sense that mypy warns about ambiguous access here in line 15, per #2878.
Actual Behavior
With the
__init__
uncommented inBase
, the false positive bug surfaces (see 2nd to last line):Here we see:
Child
, mypy properly parses the type ofChild.foo
Child.foo
Your Environment
mypy==0.800+dev.990b6a6f03ad82c10cb8edbe70508b943336f7f3
(current master at990b6a6
)mypy.ini
(and other config files):setup.cfg
Python==3.8.6
The text was updated successfully, but these errors were encountered: