Skip to content

Commit dfdcd0e

Browse files
sobolevnflaeppe
authored andcommitted
Now overloads with ambiguous self are handled properly, refs python#11347
1 parent 35fbd2a commit dfdcd0e

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎mypy/checkexpr.py‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,7 @@ def infer_overload_return_type(
28202820
matches: list[CallableType] = []
28212821
return_types: list[Type] = []
28222822
inferred_types: list[Type] = []
2823+
self_contains_any = has_any_type(object_type) if object_type is not None else False
28232824
args_contain_any = any(map(has_any_type, arg_types))
28242825
type_maps: list[dict[Expression, Type]] = []
28252826

@@ -2840,7 +2841,7 @@ def infer_overload_return_type(
28402841
if is_match:
28412842
# Return early if possible; otherwise record info, so we can
28422843
# check for ambiguity due to 'Any' below.
2843-
if not args_contain_any:
2844+
if not args_contain_any and not self_contains_any:
28442845
self.chk.store_types(m)
28452846
return ret_type, infer_type
28462847
p_infer_type = get_proper_type(infer_type)
@@ -2856,7 +2857,14 @@ def infer_overload_return_type(
28562857

28572858
if not matches:
28582859
return None
2859-
elif any_causes_overload_ambiguity(matches, return_types, arg_types, arg_kinds, arg_names):
2860+
elif any_causes_overload_ambiguity(
2861+
matches,
2862+
return_types,
2863+
arg_types,
2864+
arg_kinds,
2865+
arg_names,
2866+
self_contains_any=self_contains_any,
2867+
):
28602868
# An argument of type or containing the type 'Any' caused ambiguity.
28612869
# We try returning a precise type if we can. If not, we give up and just return 'Any'.
28622870
if all_same_types(return_types):
@@ -6452,6 +6460,8 @@ def any_causes_overload_ambiguity(
64526460
arg_types: list[Type],
64536461
arg_kinds: list[ArgKind],
64546462
arg_names: Sequence[str | None] | None,
6463+
*,
6464+
self_contains_any: bool = False,
64556465
) -> bool:
64566466
"""May an argument containing 'Any' cause ambiguous result type on call to overloaded function?
64576467
@@ -6501,7 +6511,7 @@ def any_causes_overload_ambiguity(
65016511
if not all_same_types(matching_formals) and not all_same_types(matching_returns):
65026512
# Any maps to multiple different types, and the return types of these items differ.
65036513
return True
6504-
return False
6514+
return self_contains_any
65056515

65066516

65076517
def all_same_types(types: list[Type]) -> bool:

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6693,3 +6693,65 @@ class B:
66936693
def f(self, *args, **kwargs):
66946694
pass
66956695
[builtins fixtures/tuple.pyi]
6696+
6697+
[case testOverloadSelfArgWithMultipleMatches]
6698+
# https://github.com/python/mypy/issues/11347
6699+
from typing import Generic, TypeVar, overload, Any
6700+
6701+
T = TypeVar('T')
6702+
6703+
class Some(Generic[T]):
6704+
@overload
6705+
def method(self: Some[int]) -> str: ...
6706+
@overload
6707+
def method(self: Some[str]) -> float: ...
6708+
def method(self): ...
6709+
6710+
s1: Some[int]
6711+
s2: Some[str]
6712+
s3: Some[Any]
6713+
reveal_type(s1.method()) # N: Revealed type is "builtins.str"
6714+
reveal_type(s2.method()) # N: Revealed type is "builtins.float"
6715+
reveal_type(s3.method()) # N: Revealed type is "Any"
6716+
[builtins fixtures/dict.pyi]
6717+
6718+
[case testOverloadSelfArgWithOtherSameArgAndMultipleMatches]
6719+
from typing import Generic, TypeVar, overload, Any
6720+
6721+
T = TypeVar('T')
6722+
6723+
class Some(Generic[T]):
6724+
@overload
6725+
def method(self: Some[int], other: int) -> str: ...
6726+
@overload
6727+
def method(self: Some[str], other: int) -> float: ...
6728+
def method(self): ...
6729+
6730+
# was ok
6731+
s1: Some[int]
6732+
s2: Some[str]
6733+
s3: Some[Any]
6734+
reveal_type(s1.method(1)) # N: Revealed type is "builtins.str"
6735+
reveal_type(s2.method(1)) # N: Revealed type is "builtins.float"
6736+
reveal_type(s3.method(1)) # N: Revealed type is "Any"
6737+
[builtins fixtures/dict.pyi]
6738+
6739+
[case testOverloadSelfArgWithOtherDifferentArgAndMultipleMatches]
6740+
from typing import Generic, TypeVar, overload, Any
6741+
6742+
T = TypeVar('T')
6743+
6744+
class Some(Generic[T]):
6745+
@overload
6746+
def method(self: Some[int], other: int) -> str: ...
6747+
@overload
6748+
def method(self: Some[str], other: str) -> float: ...
6749+
def method(self): ...
6750+
6751+
s1: Some[int]
6752+
s2: Some[str]
6753+
s3: Some[Any]
6754+
reveal_type(s1.method(1)) # N: Revealed type is "builtins.str"
6755+
reveal_type(s2.method('a')) # N: Revealed type is "builtins.float"
6756+
reveal_type(s3.method(1)) # N: Revealed type is "builtins.str"
6757+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)