Skip to content
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

Closed
JukkaL opened this issue Feb 7, 2018 · 5 comments · Fixed by #13494
Closed

Base class attribute type does not provide type context in subclass #4547

JukkaL opened this issue Feb 7, 2018 · 5 comments · Fixed by #13494
Labels
bug mypy got something wrong priority-0-high topic-inheritance Inheritance and incompatible overrides topic-type-context Type context / bidirectional inference topic-usability

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 7, 2018

In this example there is an unnecessary message about needing a type annotation:

from typing import List

class A:
    x: List[str]

class B(A):
    x = []  # Need type annotation

Here we should we able to use the base class type for x as type context for the x initializer.

This is closely related to #3208.

@ilevkivskyi
Copy link
Member

#6511 provides another use case for this.

@ashb
Copy link

ashb commented Mar 6, 2019

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:

a.py:10: error: Incompatible types in assignment (expression has type "Tuple[str, str, str]", base class "B" defined the type as "Tuple[str, str]")

The error is reported on the assignment in C.

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)

@ilevkivskyi
Copy link
Member

(Raising priority to high since this keeps coming.)

@onlined
Copy link
Contributor

onlined commented May 13, 2019

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)

@ilevkivskyi
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-0-high topic-inheritance Inheritance and incompatible overrides topic-type-context Type context / bidirectional inference topic-usability
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants