Description
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.