From 1bbdc0a92fc6e40f5173f1bed7a7b29a13d01e3e Mon Sep 17 00:00:00 2001 From: STerliakov Date: Wed, 24 Sep 2025 18:41:10 +0200 Subject: [PATCH] Prevent the crash on unpacking when tuple-returning overload is rejected by outer context --- mypy/checker.py | 14 +++++++++++++- test-data/unit/check-inference-context.test | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 96b55f321a73..36036b7b81c1 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -4199,7 +4199,19 @@ def check_multi_assignment_from_tuple( # inferred return type for an overloaded function # to be ambiguous. return - assert isinstance(reinferred_rvalue_type, TupleType) + if not isinstance(reinferred_rvalue_type, TupleType): + # Using outer context may transform a tuple type into something + # else when the callee is overloaded. Recheck with the new result + # and bail out. + self.check_multi_assignment( + lvalues, + rvalue, + context, + rv_type=reinferred_rvalue_type, + infer_lvalue_type=infer_lvalue_type, + undefined_rvalue=undefined_rvalue, + ) + return rvalue_type = reinferred_rvalue_type left_rv_types, star_rv_types, right_rv_types = self.split_around_star( diff --git a/test-data/unit/check-inference-context.test b/test-data/unit/check-inference-context.test index a41ee5f59670..8a2a371677aa 100644 --- a/test-data/unit/check-inference-context.test +++ b/test-data/unit/check-inference-context.test @@ -126,6 +126,22 @@ class A(Generic[T]): pass class B: pass [out] +[case testInferUnpackedOverloadWithTupleAndOtherReturns] +# https://github.com/python/mypy/issues/19920 +from typing import Tuple, TypeVar, overload + +_T = TypeVar("_T") + +@overload # type: ignore[no-overload-impl] +def gather(f1: _T) -> Tuple[_T]: ... +@overload +def gather(*fns: object) -> int: ... + +def crash() -> None: + foo: str + (foo,) = gather(0) # E: "int" object is not iterable +[builtins fixtures/tuple.pyi] + [case testInferMultipleLocalVariableTypesWithArrayRvalueAndNesting] from typing import TypeVar, List, Generic T = TypeVar('T')