You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 20d0b1c
Browse filesBrowse the repository at this point in the historyBrowse files
fix(rewrite): freeze operands against any later side effect (pytest-dev#14820)
visit_operand() froze a name only when a walrus operator in a later
operand targeted it. A call can rebind just as well, through global or
nonlocal, and then the name -- still unhoisted, still read when the
enclosing expression is assembled -- sees the new binding:
count = 0
def bump():
global count
count = 99
return 0
assert count == bump() # Python compares 0 == 0 and passes
There is no way to tell from the assert which names a call might rebind,
so _walrus_targets() becomes _can_rebind(): a name is frozen whenever
anything that follows it can execute code at all.
That sounds expensive and is not. An operand that was already hoisted
needs no freeze, so `assert len(items) == expected` rewrites unchanged;
only the bare-name-then-call shape gains one assignment. visit_BoolOp
used the same pre-scan and is generalized with it, which leaves one rule
in one place instead of two spellings of half of it.
Closes the order-name-rebound-by-call group in the coverage matrix.
Fixed assertion rewriting reading an operand after a later call had rebound its name, so ``assert count == bump()`` compares the ``count`` Python would have compared even when ``bump()`` assigns to it via ``global`` or ``nonlocal``.
0 commit comments