Bug Report
mypy doesn't detect type of inherited class variables.
To Reproduce
from typing import List
class A:
a: List[int]
class B(A):
a = [] # error: Need type annotation for 'a' (hint: "a: List[<type>] = ...")
Expected Behavior
The type of B.a should ideally be inferred from the parent class A.
Actual Behavior
I get an error on the line where I initialize the attribute inside the class B with the message error: Need type annotation for 'a' (hint: "a: List[<type>] = ...").
This works if I define the attribute as an instance attribute inside the __init__ method.
from typing import List
class A:
def __init__(self) -> None:
self.a: List[int]
class B(A):
def __init__(self) -> None:
super().__init__()
self.a = []
Your Environment
- Mypy version used:
0.812
- Mypy command-line flags:
None
- Mypy configuration options from
mypy.ini (and other config files): None
- Python version used:
3.8.5
- Operating system and version:
Ubuntu 20.04.2 LTS
For what it's worth, I also tried using the mypy playground and the same issue can be reproduced there as well with python 3.9 and mypy latest. You can take a look at https://mypy-play.net/?mypy=latest&python=3.9&gist=ed3482447c995e3b1acd57706c066633
I will be happy to help if there are any clarifications required or anything else that can help resolve the issue.
Bug Report
mypydoesn't detect type of inherited class variables.To Reproduce
Expected Behavior
The type of
B.ashould ideally be inferred from the parent classA.Actual Behavior
I get an error on the line where I initialize the attribute inside the class
Bwith the messageerror: Need type annotation for 'a' (hint: "a: List[<type>] = ...").This works if I define the attribute as an instance attribute inside the
__init__method.Your Environment
0.812Nonemypy.ini(and other config files):None3.8.5Ubuntu 20.04.2 LTSFor what it's worth, I also tried using the
mypyplayground and the same issue can be reproduced there as well withpython 3.9andmypy latest. You can take a look at https://mypy-play.net/?mypy=latest&python=3.9&gist=ed3482447c995e3b1acd57706c066633I will be happy to help if there are any clarifications required or anything else that can help resolve the issue.