Skip to content

Commit ca1a2fc

Browse files
committed
Prevent the crash on unpacking when tuple-returning overload is rejected by outer context
1 parent 354bea6 commit ca1a2fc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

mypy/checker.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4199,7 +4199,19 @@ def check_multi_assignment_from_tuple(
41994199
# inferred return type for an overloaded function
42004200
# to be ambiguous.
42014201
return
4202-
assert isinstance(reinferred_rvalue_type, TupleType)
4202+
if not isinstance(reinferred_rvalue_type, TupleType):
4203+
# Using outer context may transform a tuple type into something
4204+
# else when the callee is overloaded. Recheck with the new result
4205+
# and bail out.
4206+
self.check_multi_assignment(
4207+
lvalues,
4208+
rvalue,
4209+
context,
4210+
rv_type=reinferred_rvalue_type,
4211+
infer_lvalue_type=infer_lvalue_type,
4212+
undefined_rvalue=undefined_rvalue,
4213+
)
4214+
return
42034215
rvalue_type = reinferred_rvalue_type
42044216

42054217
left_rv_types, star_rv_types, right_rv_types = self.split_around_star(

test-data/unit/check-inference-context.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ class A(Generic[T]): pass
126126
class B: pass
127127
[out]
128128

129+
[case testInferUnpackedOverloadWithTupleAndOtherReturns]
130+
# https://github.com/python/mypy/issues/19920
131+
from typing import Tuple, TypeVar, overload
132+
133+
_T = TypeVar("_T")
134+
135+
@overload # type: ignore[no-overload-impl]
136+
def gather(f1: _T) -> Tuple[_T]: ...
137+
@overload
138+
def gather(*fns: object) -> int: ...
139+
140+
def crash() -> None:
141+
foo: str
142+
(foo,) = gather(0)
143+
[builtins fixtures/tuple.pyi]
144+
129145
[case testInferMultipleLocalVariableTypesWithArrayRvalueAndNesting]
130146
from typing import TypeVar, List, Generic
131147
T = TypeVar('T')

0 commit comments

Comments
 (0)