Skip to content

Fix bounded self types in override incompatibility checking #15045

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

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1954,11 +1954,15 @@ def bind_and_map_method(
# If we have an overload, filter to overloads that match the self type.
# This avoids false positives for concrete subclasses of generic classes,
# see testSelfTypeOverrideCompatibility for an example.
filtered_items = [
item
for item in mapped_typ.items
if not item.arg_types or is_subtype(active_self_type, item.arg_types[0])
]
filtered_items = []
for item in mapped_typ.items:
if not item.arg_types:
filtered_items.append(item)
item_arg = item.arg_types[0]
if isinstance(item_arg, TypeVarType):
item_arg = item_arg.upper_bound
if is_subtype(active_self_type, item_arg):
filtered_items.append(item)
# If we don't have any filtered_items, maybe it's always a valid override
# of the superclass? However if you get to that point you're in murky type
# territory anyway, so we just preserve the type and have the behaviour match
Expand Down
22 changes: 21 additions & 1 deletion test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class C(A[None]):
# N: def f(self, s: int) -> int
[builtins fixtures/tuple.pyi]

[case testSelfTypeOverrideCompatibilityTypeVar-xfail]
[case testSelfTypeOverrideCompatibilityTypeVar]
from typing import overload, TypeVar, Union

AT = TypeVar("AT", bound="A")
Expand Down Expand Up @@ -266,6 +266,26 @@ class B(A):
def f(*a, **kw): ...
[builtins fixtures/dict.pyi]

[case testSelfTypeOverrideCompatibilitySelfTypeVar]
from typing import Any, Generic, Self, TypeVar, overload

T_co = TypeVar('T_co', covariant=True)

class Config(Generic[T_co]):
@overload
def get(self, instance: None) -> Self: ...
@overload
def get(self, instance: Any) -> T_co: ...
def get(self, *a, **kw): ...

class MultiConfig(Config[T_co]):
@overload
def get(self, instance: None) -> Self: ...
@overload
def get(self, instance: Any) -> T_co: ...
def get(self, *a, **kw): ...
[builtins fixtures/dict.pyi]

[case testSelfTypeSuper]
from typing import TypeVar, cast

Expand Down