Repository navigation
Update augmented assignment targets in place, as Python does - #949
Merged
Merged
Conversation
ast_augassign() ran x += y as x = x + y, so in-place methods (__iadd__, __ior__ and so on) were never called: y = x; x += [3] left y unchanged, list += tuple raised TypeError, and the target's object and index were evaluated twice (counts[key()] += 1 called key() twice). It also toggled the shared AST node's ctx across an await. Evaluate the target's object and index once, apply the matching operator.iadd()/ior()/... function, which calls the in-place method or falls back to the binary one as Python does, and store the result back. State variables and their attributes, properties and pyscript classes' own in-place methods keep working. Fixes #944 Co-Authored-By: Claude Opus 5.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.
Fixes #944.
ast_augassign()ranx += yasx = x + y. So the in-place methods (__iadd__,__ior__and so on) were never called, and the target's object and index were evaluated twice:The same applied to
d |= {...},s -= {...},deque += list, a function'stags += [...], and a class that defines only__iadd__.The fix
ast_augassign()now does what Python does:operatorfunction (operator.iadd(),operator.ior(), …). That calls the in-place method, or falls back to the binary one.The evaluated object and index go into
ast.Constantnodes, so the existing load path andrecurse_assign()are unchanged. These keep working:global,nonlocaland class bodies;pyscript.x += "b",pyscript.x.attr += 1), which are dotted names, not Python attributes;__iadd__, which is callable by Python since Make special methods of pyscript classes work when Python calls them #940.Each statement now builds its own nodes rather than switching the shared AST node's
ctxbetween Load and Store across anawait. Error positions stay on the target, as before.Behavior change
Code that relied on
+=making a copy now matches Python. For example, aftery = x; x += [3],ychanges too.Testing
evalTestscases cover in-place updates and aliasing for lists, dicts and sets, every operator,list += tuple,deque += list, slices, single evaluation ofcounts[key()]andget_obj().items, attributes, properties,__iadd__-only and__add__-only classes, and a function'stags += [...]. Expected values come from CPython, and the cases fail on master.pytest(307 passed), ruff, pylint, mypy and pre-commit pass.🤖 Generated with Claude Code