Skip to content

Support starred and dict unpacking in local Python executor - #2592

Open
DivyaNarahari97 wants to merge 1 commit into
huggingface:mainfrom
DivyaNarahari97:fix-executor-unpacking
Open

Support starred and dict unpacking in local Python executor#2592
DivyaNarahari97 wants to merge 1 commit into
huggingface:mainfrom
DivyaNarahari97:fix-executor-unpacking

Conversation

@DivyaNarahari97

Copy link
Copy Markdown

The local Python interpreter rejected several common assignment and dict forms that an LLM writes routinely, so valid agent code failed at runtime.

  • Starred assignment targets (a, *b = ...) raised "Cannot unpack tuple of wrong size": set_value compared target and value lengths without accounting for ast.Starred. This affected for-loop targets and comprehensions too, since both assign through set_value.
  • List assignment targets ([a, b] = [1, 2]) fell through the target if-chain and silently assigned nothing, leaving the names undefined.
  • ** unpacking inside a dict literal ({**d, "b": 2}) raised "NoneType is not supported": for these entries the key node is None, which was passed straight to evaluate_ast. Note **kwargs unpacking in calls was already supported, so the two behaved inconsistently.

set_value now handles ast.Tuple and ast.List targets together, with at most one starred element absorbing the values the fixed targets don't take, and the ast.Dict branch expands ** entries. Genuine size mismatches still raise as before.

Also drops the unreachable ast.Starred branch in evaluate_assign: multiple assign.targets means a chained assignment (a = b = value), which never yields a bare Starred target.

Fixes #2552
Fixes #2555

The local Python interpreter rejected several common assignment and dict
forms that an LLM writes routinely, so valid agent code failed at runtime.

- Starred assignment targets (`a, *b = ...`) raised "Cannot unpack tuple of
  wrong size": `set_value` compared target and value lengths without
  accounting for `ast.Starred`. This affected for-loop targets and
  comprehensions too, since both assign through `set_value`.
- List assignment targets (`[a, b] = [1, 2]`) fell through the target
  if-chain and silently assigned nothing, leaving the names undefined.
- `**` unpacking inside a dict literal (`{**d, "b": 2}`) raised "NoneType is
  not supported": for these entries the key node is `None`, which was passed
  straight to `evaluate_ast`. Note `**kwargs` unpacking in calls was already
  supported, so the two behaved inconsistently.

`set_value` now handles `ast.Tuple` and `ast.List` targets together, with at
most one starred element absorbing the values the fixed targets don't take,
and the `ast.Dict` branch expands `**` entries. Genuine size mismatches still
raise as before.

Also drops the unreachable `ast.Starred` branch in `evaluate_assign`:
multiple `assign.targets` means a chained assignment (`a = b = value`), which
never yields a bare `Starred` target.

Fixes huggingface#2552
Fixes huggingface#2555

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant