-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Types of inheritanced class attributes aren't detected #10375
Comments
I looked into the code and it seems like it just isn't implemented. I had a quick go, in def check_lvalue(self, lvalue: Lvalue) -> Tuple[Optional[Type],
Optional[IndexExpr],
Optional[Var]]:
lvalue_type = None
index_lvalue = None
inferred = None
if self.is_definition(lvalue) and (
not isinstance(lvalue, NameExpr) or isinstance(lvalue.node, Var)
):
if isinstance(lvalue, NameExpr):
# If this is a class variable without a type annotation and
# there's a base class with the same name then set the type to
# that variable's type.
if lvalue.node.type is None:
for base in lvalue.node.info.mro[1:]:
tnode = base.names.get(lvalue.node.name)
if tnode is not None and tnode.type is not None:
lvalue_type = tnode.type
lvalue.node.type = lvalue_type
self.store_type(lvalue, lvalue_type)
break
if lvalue.node.type is None:
inferred = cast(Var, lvalue.node)
assert isinstance(inferred, Var)
else:
... Not sure about the Test case: class Base:
X: list[int] = []
class Derived0(Base):
X = []
class Derived1(Base):
X: list[str] = []
class Derived2(Base):
X = ["a"] Before:
After:
|
Ok I added my attempt in #12022 but it really needs feedback from an actual MyPy developer. I'd appreciate any help. |
Related python#10375 and python#10506. For an unhinted assignment to a class variable defined in a base class, this allows subderived classes to only match the type in the base class, rather than the one inferred from the assignment value.
Related python#10375 and python#10506. For an unhinted assignment to a class variable defined in a base class, this allows subderived classes to only match the type in the base class, rather than the one inferred from the assignment value.
Related python#10375 and python#10506. For an unhinted assignment to a class variable defined in a base class, this allows subderived classes to only match the type in the base class, rather than the one inferred from the assignment value.
Related python#10375 and python#10506. For an unhinted assignment to a class variable defined in a base class, this allows subderived classes to only match the type in the base class, rather than the one inferred from the assignment value.
Related python#10375 and python#10506. For an unhinted assignment to a class variable defined in a base class, this allows subderived classes to only match the type in the base class, rather than the one inferred from the assignment value.
This bug appears to have been fixed. I can no longer repro it with the latest version of mypy (1.5). |
Fixed in #13494 |
Bug Report
mypy
doesn't detect type of inherited class variables.To Reproduce
Expected Behavior
The type of
B.a
should ideally be inferred from the parent classA
.Actual Behavior
I get an error on the line where I initialize the attribute inside the class
B
with 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.812
None
mypy.ini
(and other config files):None
3.8.5
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 withpython 3.9
andmypy 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.
The text was updated successfully, but these errors were encountered: