-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Class variable type is reset in inheritance chain #10506
Comments
It seems to be that way for any complex types, I can see the same issue here for Dict ClassVars: from typing import Dict, Tuple, ClassVar
class A:
a: ClassVar[int] = 1
b: ClassVar[Dict[str, Tuple[int, str]]] = {}
class B:
a = 2
b = {} # fails with error: Need type annotation for 'b' (hint: "b: Dict[<type>, <type>] = ...") |
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.
Here's a simplified example where I ran into this: class P:
x: ClassVar[Union[int, Literal["X"]]] = 0
class Q(P):
x = 0
class R(Q):
x = 1 https://mypy-play.net/?mypy=latest&python=3.11&gist=1f83185d54fbb490447484a5a2fd7677&flags=strict |
I found that this bug doesn't require any complex types. The bug happens with any type that isn't what would be inferred. from typing import *
class P:
x: ClassVar[Any] = 0
class Q(P):
x = "random"
class R(Q):
x = 3.14
https://mypy-play.net/?mypy=latest&python=3.11&gist=ef5fea9cb8e11b10cb1568ce5dc12fdc&flags=strict Because we explicitly annotated the type of I think it would be good to remove "Class variable type is reset in inheritance chain" |
List
type is reset in inheritance chain
Bug Report
When a
List
class variable is declared in a base class, subclasses that assign new values to the class variable will limit their children to the type of that assignment.To Reproduce
Expected Behavior
ManufacturerCar.seats
should not have been marked as an error since the list can containSeat
s. The class isn't a generic and (to my knowledge) there isn't a way to sayList[? inherits Seat]
like in Java with wildcards.If that's what the problem is, then warning should come already at
ElectricCar.seats = [LeatherSeat()]
and the type shouldn't be erased or overwritten to then fail in the next class.Actual Behavior
.seats
type is overwritten (erased?) in one subclass and the children of that are then limited to that type.Your Environment
mypy.ini
(and other config files):python:3.8.7-slim
The text was updated successfully, but these errors were encountered: