Support starred and dict unpacking in local Python executor - #2592
Open
DivyaNarahari97 wants to merge 1 commit into
Open
Support starred and dict unpacking in local Python executor#2592DivyaNarahari97 wants to merge 1 commit into
DivyaNarahari97 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The local Python interpreter rejected several common assignment and dict forms that an LLM writes routinely, so valid agent code failed at runtime.
a, *b = ...) raised "Cannot unpack tuple of wrong size":set_valuecompared target and value lengths without accounting forast.Starred. This affected for-loop targets and comprehensions too, since both assign throughset_value.[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 isNone, which was passed straight toevaluate_ast. Note**kwargsunpacking in calls was already supported, so the two behaved inconsistently.set_valuenow handlesast.Tupleandast.Listtargets together, with at most one starred element absorbing the values the fixed targets don't take, and theast.Dictbranch expands**entries. Genuine size mismatches still raise as before.Also drops the unreachable
ast.Starredbranch inevaluate_assign: multipleassign.targetsmeans a chained assignment (a = b = value), which never yields a bareStarredtarget.Fixes #2552
Fixes #2555