Skip to content

Commit e1bfb75

Browse files
committed
Fix incorrect join in the presence of Any fallback
Fixes #11925
1 parent 98cc165 commit e1bfb75

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ def check_context(self, proper_subtype: bool) -> None:
104104
# and different visitors. Check if flag values are such that we definitely support.
105105
if proper_subtype:
106106
assert (
107-
not self.ignore_type_params
108-
and not self.ignore_pos_arg_names
107+
not self.ignore_pos_arg_names
109108
and not self.ignore_declared_variance
110109
)
111110
else:

test-data/unit/check-inference.test

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

0 commit comments

Comments
 (0)