Skip to content

Commit 8f4da0e

Browse files
authored
Fix incorrect join in the presence of Any fallback (#14404)
Fixes #11925 This avoids mypy from guessing subtype relationships because of the any fallback over here: https://github.com/python/mypy/blob/e1bfb75ed2187db76d51ed875ce953da3ba4d02c/mypy/subtypes.py#L438
1 parent 9a8c171 commit 8f4da0e

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

mypy/join.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from mypy.nodes import CONTRAVARIANT, COVARIANT, INVARIANT
1010
from mypy.state import state
1111
from mypy.subtypes import (
12+
SubtypeContext,
1213
find_member,
1314
is_equivalent,
1415
is_proper_subtype,
@@ -101,7 +102,9 @@ def join_instances(self, t: Instance, s: Instance) -> ProperType:
101102
assert new_type is not None
102103
args.append(new_type)
103104
result: ProperType = Instance(t.type, args)
104-
elif t.type.bases and is_subtype(t, s, ignore_type_params=True):
105+
elif t.type.bases and is_proper_subtype(
106+
t, s, subtype_context=SubtypeContext(ignore_type_params=True)
107+
):
105108
result = self.join_instances_via_supertype(t, s)
106109
else:
107110
# Now t is not a subtype of s, and t != s. Now s could be a subtype

mypy/subtypes.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ def check_context(self, proper_subtype: bool) -> None:
103103
# Historically proper and non-proper subtypes were defined using different helpers
104104
# and different visitors. Check if flag values are such that we definitely support.
105105
if proper_subtype:
106-
assert (
107-
not self.ignore_type_params
108-
and not self.ignore_pos_arg_names
109-
and not self.ignore_declared_variance
110-
)
106+
assert not self.ignore_pos_arg_names and not self.ignore_declared_variance
111107
else:
112108
assert not self.erase_instances and not self.keep_erased_types
113109

test-data/unit/check-inference.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,3 +3380,15 @@ class A:
33803380
T = TypeVar("T")
33813381
def type_or_callable(value: T, tp: Union[Type[T], Callable[[int], T]]) -> T: ...
33823382
reveal_type(type_or_callable(A("test"), A)) # N: Revealed type is "__main__.A"
3383+
3384+
[case testJoinWithAnyFallback]
3385+
from unknown import X # type: ignore[import]
3386+
3387+
class A: ...
3388+
class B(X, A): ...
3389+
class C(B): ...
3390+
class D(C): ...
3391+
class E(D): ...
3392+
3393+
reveal_type([E(), D()]) # N: Revealed type is "builtins.list[__main__.D]"
3394+
reveal_type([D(), E()]) # N: Revealed type is "builtins.list[__main__.D]"

0 commit comments

Comments
 (0)