From 19251aa38e950ff625c139c272ba6cadc12e684f Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Sun, 8 Jun 2025 19:36:15 -0400 Subject: [PATCH] Fix missing error context for unpacking assignment involving star expr --- mypy/checker.py | 2 +- test-data/unit/check-lists.test | 9 +++++++++ test-data/unit/check-tuples.test | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 49f1bc15f583..c0a1766ee87e 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3864,7 +3864,7 @@ def check_assignment_to_multiple_lvalues( if rvalue_needed > 0: rvalues = ( rvalues[0:iterable_start] - + [TempNode(iterable_type) for i in range(rvalue_needed)] + + [TempNode(iterable_type, context=rval) for _ in range(rvalue_needed)] + rvalues[iterable_end + 1 :] ) diff --git a/test-data/unit/check-lists.test b/test-data/unit/check-lists.test index 77acdafd3319..ee3115421e40 100644 --- a/test-data/unit/check-lists.test +++ b/test-data/unit/check-lists.test @@ -94,3 +94,12 @@ def foo(x: object) -> None: [reveal_type(x) for x in [1, 2, 3]] # N: Revealed type is "builtins.int" [builtins fixtures/isinstancelist.pyi] + +[case testUnpackAssignmentWithStarExpr] +a: A +b: list[B] +if int(): + (a,) = [*b] # E: Incompatible types in assignment (expression has type "B", variable has type "A") + +class A: pass +class B: pass diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index f118eec4f266..615ba129dad5 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -618,6 +618,15 @@ u, v, w = r, s = 1, 1 # E: Need more than 2 values to unpack (3 expected) d, e = f, g, h = 1, 1 # E: Need more than 2 values to unpack (3 expected) [builtins fixtures/tuple.pyi] +[case testUnpackAssignmentWithStarExpr] +a: A +b: list[B] +if int(): + (a,) = (*b,) # E: Incompatible types in assignment (expression has type "B", variable has type "A") + +class A: pass +class B: pass + -- Assignment to starred expressions -- ---------------------------------