Skip to content

Commit 120a774

Browse files
committed
Preserve overload inference when nested Any does not cause ambiguity
Select the first matching overload when generic argument types are proper subtypes of every matching parameter. Preserve conservative handling of callable arguments and unpacked arguments. Fixes python#22174.
1 parent bc95983 commit 120a774

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
covers_at_runtime,
127127
find_member,
128128
has_any_type,
129+
is_proper_subtype,
129130
is_same_type,
130131
is_subtype,
131132
merge_typevars_in_callables_by_name,
@@ -6956,6 +6957,16 @@ def any_causes_overload_ambiguity(
69566957
for formal in formals:
69576958
matching_formals.append(matched_callable.arg_types[formal])
69586959
if not all_same_types(matching_formals) and not all_same_types(matching_returns):
6960+
if (
6961+
not arg_kinds[arg_idx].is_star()
6962+
and not isinstance(get_proper_type(arg_type), FunctionLike)
6963+
and all(is_proper_subtype(arg_type, formal) for formal in matching_formals)
6964+
):
6965+
# These matches do not depend on Any being compatible with arbitrary
6966+
# types. For example, SubBox[Any] matches both SubBox[Any] and Box[Any].
6967+
# Callable subtyping can still accept gradual parameter lists, so keep
6968+
# the conservative ambiguity check for callable arguments.
6969+
continue
69596970
# Any maps to multiple different types, and the return types of these items differ.
69606971
return True
69616972

‎test-data/unit/check-overloading.test‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,27 @@ reveal_type(f(d)) # N: Revealed type is "builtins.list[builtins.int]"
17931793

17941794
[builtins fixtures/list.pyi]
17951795

1796+
[case testOverloadWithNestedAnyNotCausingAmbiguity]
1797+
from typing import Any, Generic, TypeVar, overload
1798+
T = TypeVar('T')
1799+
1800+
class Box(Generic[T]): pass
1801+
class SubBox(Box[T]): pass
1802+
1803+
@overload
1804+
def choose(value: SubBox[T]) -> SubBox[T]: ...
1805+
@overload
1806+
def choose(value: Box[T]) -> Box[T]: ...
1807+
def choose(value): pass
1808+
1809+
value: SubBox[Any]
1810+
reveal_type(choose(value)) # N: Revealed type is "__main__.SubBox[Any]"
1811+
reveal_type(choose(value=value)) # N: Revealed type is "__main__.SubBox[Any]"
1812+
nested: SubBox[Box[Any]]
1813+
reveal_type(choose(nested)) # N: Revealed type is "__main__.SubBox[__main__.Box[Any]]"
1814+
any_value: Any
1815+
reveal_type(choose(any_value)) # N: Revealed type is "Any"
1816+
17961817
[case testOverloadWithOverlappingItemsAndAnyArgument10]
17971818
from typing import overload, Any
17981819

‎test-data/unit/pythoneval.test‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,20 @@ y = t"hello {1}"
22872287
[out]
22882288
m.pyi:3: error: T-strings: requires Python 3.14 or newer (current target: Python 3.13)
22892289

2290+
[case testReversedListWithNestedAny]
2291+
# flags: --python-version=3.12 --warn-return-any
2292+
from typing import Any, assert_type
2293+
2294+
def last_value(values: list[tuple[Any, int]]) -> int | None:
2295+
for key, value in reversed(values):
2296+
assert_type(value, int)
2297+
return value
2298+
return None
2299+
2300+
assert last_value([('first', 1), ('last', 2)]) == 2
2301+
assert last_value([]) is None
2302+
[out]
2303+
22902304
[case testNativeParserStarUnpackRequiresPython311]
22912305
# flags: --python-version=3.10 --native-parser
22922306
from typing_extensions import TypeVarTuple, Unpack

0 commit comments

Comments
 (0)