Skip to content

Commit

Permalink
Fix incorrect join in the presence of Any fallback
Browse files Browse the repository at this point in the history
Fixes #11925
  • Loading branch information
hauntsaninja committed Jan 6, 2023
1 parent 98cc165 commit c6b34ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -101,7 +102,7 @@ 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
Expand Down
1 change: 1 addition & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(
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.
return # TODO: investigate
if proper_subtype:
assert (
not self.ignore_type_params
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]"

0 comments on commit c6b34ae

Please sign in to comment.