From c44bebbf1594fa19c9e66070b51355d6ee59b24c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 5 Jan 2023 21:25:13 -0800 Subject: [PATCH] Fix incorrect join in the presence of Any fallback Fixes #11925 --- mypy/join.py | 5 ++++- mypy/subtypes.py | 6 +----- test-data/unit/check-inference.test | 12 ++++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/mypy/join.py b/mypy/join.py index 84aa03f8eeba..62d256f4440f 100644 --- a/mypy/join.py +++ b/mypy/join.py @@ -9,6 +9,7 @@ from mypy.nodes import CONTRAVARIANT, COVARIANT, INVARIANT from mypy.state import state from mypy.subtypes import ( + SubtypeContext, find_member, is_equivalent, is_proper_subtype, @@ -101,7 +102,9 @@ def join_instances(self, t: Instance, s: Instance) -> ProperType: assert new_type is not None args.append(new_type) result: ProperType = Instance(t.type, args) - elif t.type.bases and is_subtype(t, s, ignore_type_params=True): + elif t.type.bases and is_proper_subtype( + t, s, subtype_context=SubtypeContext(ignore_type_params=True) + ): result = self.join_instances_via_supertype(t, s) else: # Now t is not a subtype of s, and t != s. Now s could be a subtype diff --git a/mypy/subtypes.py b/mypy/subtypes.py index 61ba7af5147f..d76b45779a6d 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -103,11 +103,7 @@ def check_context(self, proper_subtype: bool) -> None: # Historically proper and non-proper subtypes were defined using different helpers # and different visitors. Check if flag values are such that we definitely support. if proper_subtype: - assert ( - not self.ignore_type_params - and not self.ignore_pos_arg_names - and not self.ignore_declared_variance - ) + assert not self.ignore_pos_arg_names and not self.ignore_declared_variance else: assert not self.erase_instances and not self.keep_erased_types diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 45a833e5210c..dfd526b90747 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -3382,3 +3382,15 @@ class A: T = TypeVar("T") def type_or_callable(value: T, tp: Union[Type[T], Callable[[int], T]]) -> T: ... reveal_type(type_or_callable(A("test"), A)) # N: Revealed type is "__main__.A" + +[case testJoinWithAnyFallback] +from unknown import X # type: ignore[import] + +class A: ... +class B(X, A): ... +class C(B): ... +class D(C): ... +class E(D): ... + +reveal_type([E(), D()]) # N: Revealed type is "builtins.list[__main__.D]" +reveal_type([D(), E()]) # N: Revealed type is "builtins.list[__main__.D]"