fix(executor): evaluate augmented assignment targets once#2537
Open
chuenchen309 wants to merge 1 commit into
Open
fix(executor): evaluate augmented assignment targets once#2537chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
Augmented assignment currently resolves subscript keys and attribute objects once to read the current value, then resolves them again through set_value when storing the result. Side-effecting target expressions can therefore write to a different location than the one that was read. Cache the resolved object and subscript key across the operation, matching Python evaluation order. Add regression cases for both subscript and attribute targets. Co-Authored-By: Claude Opus 4.8 <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.
What
evaluate_augassign()currently evaluates subscript and attribute target expressions twice: once inget_current_value()to read the old value, and again inset_value()to store the result. Python evaluates the target object and subscript key only once for augmented assignment.A side-effecting target can therefore read from one location and write the result to another:
Native Python produces
values == [11, 2]with one call. The local executor producedvalues == [1, 11]with two calls: it readvalues[0], then re-rannext_index()and stored that result intovalues[1].Attribute targets had the same problem.
next_box().value += 10read from the first returned object, then callednext_box()again and wrote the result onto the second object.Fix
Resolve subscript objects/keys and attribute objects once before evaluating the right-hand side, then reuse those exact references for the final store. Name targets and the operation dispatch are unchanged; the general-purpose
set_value()helper is also unchanged.This also preserves Python's evaluation order: target object, subscript key (if any), right-hand side, operation, then store.
Testing
Added two parametrized regression cases covering side-effecting subscript and attribute targets.
([1, 11], 2)instead of([11, 2], 1).tests/test_local_python_executor.py: 399 passed, 2 skipped.TestTimeout.test_timeout_decorator_raises_error_when_exceeded; it passed immediately in isolation, and the complete file then passed on rerun.ruff checkandruff format --check: clean.git diff --check: clean.Duplicate-work check
Checked every currently open PR page (roughly 500 PRs), including changed file paths. Many open PRs touch this hot executor file, but none of their diffs touches
evaluate_augassignor claims this evaluation-order bug. Also inspected the merged historical augassign PRs #285 and #313; they implemented/refactored operator dispatch and do not address repeated target evaluation.AI disclosure
Developed with AI assistance (Codex), which surfaced the candidate during a targeted executor review. I independently reproduced both wrong-write cases against the real evaluator, audited all open PR file overlaps and the historical augassign changes, reviewed the implementation, and ran the focused and full tests before submitting.