Skip to content

Commit 0221c80

Browse files
authored
Fix crash when unpacking return value from overload (#21830)
Fixes #21824 Fixes #19920 Closes #19921 Note that ilevkivskyi has a suggestion to change overload inference here: #19920 (comment) While that is right, it is a little separate, and I think it is okay to remove the crash given that it now affects numpy and has been reported in other contexts too. We just keep the originally inferred tuple
1 parent 5bb72b7 commit 0221c80

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

‎mypy/checker.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,8 +4409,10 @@ def check_multi_assignment_from_tuple(
44094409
# inferred return type for an overloaded function
44104410
# to be ambiguous.
44114411
return
4412-
assert isinstance(reinferred_rvalue_type, TupleType)
4413-
rvalue_type = reinferred_rvalue_type
4412+
if isinstance(reinferred_rvalue_type, TupleType):
4413+
# This branch will usually be taken, but in some cases context can
4414+
# e.g. select a different overload
4415+
rvalue_type = reinferred_rvalue_type
44144416

44154417
left_rv_types, star_rv_types, right_rv_types = self.split_around_star(
44164418
rvalue_type.items, star_index, len(lvalues)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,42 @@ class A: pass
426426
class B: pass
427427
[builtins fixtures/tuple.pyi]
428428

429+
[case testMultipleAssignmentWithOverloadReinferredAsHomogeneousTuple]
430+
from typing import Any, TypeVar, overload
431+
432+
T = TypeVar("T")
433+
434+
@overload
435+
def f(value: T) -> tuple[T, int]: ...
436+
@overload
437+
def f(*values: object) -> tuple[Any, ...]: ...
438+
def f(*values: object, **kwargs: object) -> object: ...
439+
440+
def g() -> None:
441+
first: str
442+
first, second = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
443+
reveal_type(second) # N: Revealed type is "builtins.int"
444+
445+
last: str
446+
_, last = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
447+
[builtins fixtures/tuple.pyi]
448+
449+
[case testMultipleAssignmentWithOverloadReinferredAsNonTuple]
450+
from typing import TypeVar, overload
451+
452+
T = TypeVar("T")
453+
454+
@overload
455+
def f(value: T) -> tuple[T]: ...
456+
@overload
457+
def f(*values: object) -> int: ...
458+
def f(*values: object, **kwargs: object) -> object: ...
459+
460+
def g() -> None:
461+
value: str
462+
(value,) = f(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
463+
[builtins fixtures/tuple.pyi]
464+
429465
[case testMultipleAssignmentWithSquareBracketTuples]
430466
# flags: --no-strict-optional
431467
from typing import Tuple

0 commit comments

Comments
 (0)