Skip to content
Open
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
11 changes: 11 additions & 0 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,13 +541,24 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
return False

if isinstance(left, CallableType) and isinstance(right, CallableType):
# We run is_callable_compatible in both directions, similar to the logic
# in is_unsafe_overlapping_overload_signatures
# See comments in https://github.com/python/mypy/pull/5476
return is_callable_compatible(
left,
right,
is_compat=_is_overlapping_types,
is_proper_subtype=False,
ignore_pos_arg_names=not overlap_for_overloads,
allow_partial_overlap=True,
) or is_callable_compatible(
right,
left,
is_compat=_is_overlapping_types,
is_proper_subtype=False,
ignore_pos_arg_names=not overlap_for_overloads,
check_args_covariantly=True,
allow_partial_overlap=True,
)

call = None
Expand Down
16 changes: 15 additions & 1 deletion mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from mypy.erasetype import erase_type, remove_instance_last_known_values
from mypy.indirection import TypeIndirectionVisitor
from mypy.join import join_types
from mypy.meet import meet_types, narrow_declared_type
from mypy.meet import is_overlapping_types, meet_types, narrow_declared_type
from mypy.nodes import (
ARG_NAMED,
ARG_OPT,
Expand Down Expand Up @@ -645,6 +645,20 @@ def assert_simplified_union(self, original: list[Type], union: Type) -> None:
assert_equal(make_simplified_union(original), union)
assert_equal(make_simplified_union(list(reversed(original))), union)

def test_generic_callable_overlap_is_symmetric(self) -> None:
any_type = AnyType(TypeOfAny.from_omitted_generics)
outer_t = TypeVarType("T", "T", TypeVarId(1), [], self.fx.o, any_type)
outer_s = TypeVarType("S", "S", TypeVarId(2), [], self.fx.o, any_type)
generic_t = TypeVarType("T", "T", TypeVarId(-1), [], self.fx.o, any_type)

callable_type = CallableType([outer_t], [ARG_POS], [None], outer_s, self.fx.function)
generic_identity = CallableType(
[generic_t], [ARG_POS], [None], generic_t, self.fx.function, variables=[generic_t]
)

assert is_overlapping_types(callable_type, generic_identity)
assert is_overlapping_types(generic_identity, callable_type)

# Helpers

def tuple(self, *a: Type) -> TupleType:
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -3993,3 +3993,22 @@ def f2(func: Callable[..., T], arg: str) -> T:
return func(arg)
return func(arg)
[builtins fixtures/primitives.pyi]


[case testNarrowGenericCallableEquality]
# flags: --strict-equality --warn-unreachable
from typing import Callable, TypeVar

S = TypeVar("S")
T = TypeVar("T")

def identity(x: T) -> T:
return x

def msg(cmp_property: Callable[[T], S]) -> None:
if cmp_property == identity:
# TODO: the swapping of these reveal's is not ideal
reveal_type(cmp_property) # N: Revealed type is "def [T] (x: T`-1) -> T`-1"
reveal_type(identity) # N: Revealed type is "def (T`-1) -> S`-2"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although we fix the unreachable error, this "flipping" behavior in IMO surprising. Would it make sense for each expression to keep its own type if the intersection is not expressible?

Btw, in this case the intersection is expressible, it is Overloaded(def (T) -> S, def [U](x: U) -> U), but if we are going to do this we will need to do some (expensive) validation, plus freshening to avoid accidental TypeVar id clashes, and this would be way too big change for a point release.

return
[builtins fixtures/primitives.pyi]