Skip to content

Support starred and iterable unpacking in assignments#2556

Open
himanshu748 wants to merge 1 commit into
huggingface:mainfrom
himanshu748:fix-starred-unpacking-assignment
Open

Support starred and iterable unpacking in assignments#2556
himanshu748 wants to merge 1 commit into
huggingface:mainfrom
himanshu748:fix-starred-unpacking-assignment

Conversation

@himanshu748

Copy link
Copy Markdown

Fixes #2555.

What happens today

Several standard unpacking forms that models write constantly are rejected by the local executor, with errors that point nowhere:

a, *b = [1, 2, 3]        # InterpreterError: Cannot unpack tuple of wrong size
first, *rest = "hello"   # InterpreterError: Cannot unpack tuple of wrong size
a, b = "hi"              # InterpreterError: Cannot unpack non-tuple value
[a, b] = [1, 2]          # silently assigns nothing

set_value only handled ast.Tuple targets: it required an exact length match (so any starred target failed the size check), it excluded str/bytes from the iterable path (so string unpacking was refused), and it had no ast.List branch at all (so [a, b] = ... was silently dropped). The model gets no signal that * targets simply are not implemented, so it retries the same valid code.

The fix

Rewrite the tuple/list target branch to match CPython:

  • accept any iterable, not just tuples and lists
  • support a single starred target that absorbs the surplus into a list, in any position (a, *b, *a, b, a, *b, c)
  • handle ast.List targets alongside ast.Tuple
  • raise CPython-parity messages: not enough values to unpack (expected N, got M), too many values to unpack (expected N), not enough values to unpack (expected at least N, got M), cannot unpack non-iterable X object, and multiple starred expressions in assignment

Nested targets (a, (b, c) = 1, (2, 3)) keep working through the existing recursion.

Tests

14 new parametrized cases covering starred unpacking in every position, empty-star, string/list-pattern/nested unpacking, and the four error messages. One existing test (test_evaluate_assign_error) was updated from the old "Cannot unpack tuple of wrong size" wording to the new CPython message. All new cases fail on current main. Full test file: 411 passed, 2 skipped. Ruff check and format are clean.

Copilot AI review requested due to automatic review settings July 21, 2026 05:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: local executor rejects starred and iterable unpacking assignments (a, *b = ...)

2 participants