-
-
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
Base class attribute type does not provide type context in subclass #4547
Comments
#6511 provides another use case for this. |
Minimal repro case: from typing import Iterable
class A:
a: Iterable[str] = ("a", )
class B(A):
a = ("a", "b")
class C(B):
a = ("a", "b", "c") And the error:
The error is reported on the assignment in I would have expected that B.a would have the same type as A.a since B.a doesn't have an explicit type. (This is the case from #6511 copied here in case it is useful) |
(Raising priority to high since this keeps coming.) |
This example from #6766 (comment), which is related to partial list type, doesn't give an error while it should: class A:
x = ['a', 'b']
class B(A):
x = []
x.append(2) |
It looks like the situation is even worse here. Here we have no partial types and we still infer wrong type (the one that violates Liskov): from typing import List, Optional
class B:
x: List[Optional[int]]
class C(B):
x = [1]
reveal_type(C().x) # Revealed type is "builtins.list[builtins.int]" The problem is that we use supertype as type context when checking compatibility. But then again when storing the variable type we use empty context. |
In this example there is an unnecessary message about needing a type annotation:
Here we should we able to use the base class type for
x
as type context for thex
initializer.This is closely related to #3208.
The text was updated successfully, but these errors were encountered: