Skip to content

Update augmented assignment targets in place, as Python does - #949

Merged
craigbarratt merged 1 commit into
masterfrom
augassign-inplace
Dec 5, 2026
Merged

craigbarratt merged 1 commit into
masterfrom
augassign-inplace

Conversation

@craigbarratt

Copy link
Copy Markdown
Member

Fixes #944.

ast_augassign() ran x += y as x = 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:

x = [1, 2]
y = x
x += [3]          # y stayed [1, 2]; Python gives [1, 2, 3]
x += (4, 5)       # TypeError; Python accepts any iterable
counts[key()] += 1  # called key() twice

The same applied to d |= {...}, s -= {...}, deque += list, a function's tags += [...], and a class that defines only __iadd__.

The fix

ast_augassign() now does what Python does:

  • It evaluates the target's object (and index, for a subscript) once.
  • It applies the matching operator function (operator.iadd(), operator.ior(), …). That calls the in-place method, or falls back to the binary one.
  • It stores the result back in the same target.

The evaluated object and index go into ast.Constant nodes, so the existing load path and recurse_assign() are unchanged. These keep working:

  • names, including global, nonlocal and class bodies;
  • state variables and their attributes (pyscript.x += "b", pyscript.x.attr += 1), which are dotted names, not Python attributes;
  • properties (getter, in-place method, setter, as in Python);
  • a pyscript class's own __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 ctx between Load and Store across an await. Error positions stay on the target, as before.

Behavior change

Code that relied on += making a copy now matches Python. For example, after y = x; x += [3], y changes too.

Testing

  • New evalTests cases cover in-place updates and aliasing for lists, dicts and sets, every operator, list += tuple, deque += list, slices, single evaluation of counts[key()] and get_obj().items, attributes, properties, __iadd__-only and __add__-only classes, and a function's tags += [...]. Expected values come from CPython, and the cases fail on master.
  • I also compared 25 cases with CPython: all match, and master differs on 13.
  • pytest (307 passed), ruff, pylint, mypy and pre-commit pass.

🤖 Generated with Claude Code

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>
@craigbarratt
craigbarratt merged commit 21bf875 into master Dec 5, 2026
11 checks passed
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.

Unlike Python, x += [3] and other augmented assignments make a new object instead of changing it in place

1 participant