Skip to content

fix(rewrite): freeze walrus and starred operands too - #14814

Draft
RonnyPfannschmidt wants to merge 2 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:ronny/fix-walrus-raw-operands
Draft

fix(rewrite): freeze walrus and starred operands too#14814
RonnyPfannschmidt wants to merge 2 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:ronny/fix-walrus-raw-operands

Conversation

@RonnyPfannschmidt

@RonnyPfannschmidt RonnyPfannschmidt commented Jul 31, 2026

Copy link
Copy Markdown
Member

Stacked on #14447#14921#14813. Its diff includes theirs.

visit_operand() (added in #14447) only froze a bare name, so two other unhoisted operands kept being evaluated after everything that follows them:

assert collect((x := 1), identity(x := 2)) == (1, 2)      # passes (1, 2) to plain Python, (2, 2) rewritten
assert collect(*items, identity(items := [9])) == (1, [9]) # passes (1, [9]) plain, (9, [9]) rewritten

A walrus operator left in place assigns only once the enclosing expression is assembled, which is after the later arguments have run — so the earlier argument saw the later assignment. A starred argument hid its value inside an ast.Starred, where the existing Name check could not reach it.

Lands the order-starred-argument case and the last order-call-argument case of the coverage matrix in #14813, as passing tests (+2).

The starred case was found by the matrix, not by hand — it is what the order-* axis is for.

RonnyPfannschmidt and others added 2 commits September 1, 2026 13:51
visit_operand() only froze a bare name, so two other unhoisted operands
kept being evaluated after everything that follows them:

    assert collect((x := 1), identity(x := 2)) == (1, 2)
    assert collect(*items, identity(items := [9])) == (1, [9])

A walrus operator left in place assigns once the enclosing expression is
assembled, which is after the later arguments have run -- so the earlier
argument saw the later assignment.  A starred argument hid its value
inside an ast.Starred, where the existing Name check could not see it.

Closes the order-starred-argument group and the remaining
order-call-argument entry in the coverage matrix.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…eeds

visit_operand freezes a walrus operand whenever anything follows it, and
a comparison always has at least one comparator -- so by the time
visit_Compare looks at its left operand, a NamedExpr has already been
copied into a temporary.  The special case that did it here can never
run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@RonnyPfannschmidt
RonnyPfannschmidt force-pushed the ronny/fix-walrus-raw-operands branch from 338cd67 to 6446871 Compare September 1, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant