Skip to content

Commit edc350c

Browse 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.
1 parent 2480b28 commit edc350c

3 files changed

Lines changed: 26 additions & 33 deletions

File tree

‎changelog/14821.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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``.

‎src/_pytest/assertion/rewrite.py‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,14 +540,19 @@ def traverse_node(node: ast.AST) -> Iterator[ast.AST]:
540540
yield from traverse_node(child)
541541

542542

543-
def _walrus_targets(nodes: Iterable[ast.expr]) -> set[str]:
544-
"""Return the names any walrus operator in *nodes* rebinds."""
545-
return {
546-
sub.target.id
543+
def _can_rebind(nodes: Iterable[ast.expr]) -> bool:
544+
"""Return whether evaluating *nodes* could rebind a name read before them.
545+
546+
A walrus operator rebinds its target outright. A call -- including the
547+
implicit one behind ``await`` -- can rebind anything it declares ``global``
548+
or ``nonlocal``, and there is no way to tell from here which names those
549+
are, so assume the worst.
550+
"""
551+
return any(
552+
isinstance(sub, ast.NamedExpr | ast.Call | ast.Await)
547553
for node in nodes
548554
for sub in ast.walk(node)
549-
if isinstance(sub, ast.NamedExpr)
550-
}
555+
)
551556

552557

553558
@functools.lru_cache(maxsize=1)
@@ -974,30 +979,31 @@ def visit_Name(self, name: ast.Name) -> tuple[ast.Name, str]:
974979
def visit_operand(
975980
self, operand: ast.expr, later: Sequence[ast.expr]
976981
) -> tuple[ast.expr, str]:
977-
"""Visit an operand, freezing it against walrus operators in *later*.
982+
"""Visit an operand, freezing it against side effects in *later*.
978983
979984
Operands are rewritten into statements that run in source order, but
980985
two of them stay unhoisted and are evaluated at the very end, when the
981986
enclosing expression is assembled -- after everything that follows
982987
them:
983988
984-
* a plain name, which a walrus operator in a later operand rebinds in
985-
between, so the value used and the value reported would be the
986-
post-walrus one;
989+
* a plain name, whose binding anything in *later* may have changed by
990+
then, so the value used and the value reported would be the new one;
987991
* a walrus operator itself, which would then assign in the wrong
988992
order, and be visible to the operands that were meant to precede it.
989993
990994
Either way Python evaluates the earlier operand first, so copy it into
991995
a temporary here. A starred argument is unwrapped and rewrapped, its
992996
value being subject to the same problem.
997+
998+
Only names need this. Everything else either has no binding to lose
999+
(a literal) or was already hoisted into a temporary when it was
1000+
visited, which is what keeps it ordered.
9931001
"""
9941002
specifiers = set(self.explanation_specifiers)
9951003
res, expl = self.visit(operand)
9961004
value = res.value if isinstance(res, ast.Starred) else res
997-
if isinstance(value, ast.NamedExpr):
998-
needs_freeze = bool(later)
999-
elif isinstance(value, ast.Name):
1000-
needs_freeze = value.id in _walrus_targets(later)
1005+
if isinstance(value, ast.NamedExpr | ast.Name):
1006+
needs_freeze = _can_rebind(later)
10011007
else:
10021008
needs_freeze = False
10031009
if needs_freeze:
@@ -1019,9 +1025,8 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10191025
body = save = self.statements
10201026
fail_save = self.expl_stmts
10211027
levels = len(boolop.values) - 1
1022-
later_walrus_targets = [
1023-
_walrus_targets(boolop.values[idx + 1 :])
1024-
for idx in range(len(boolop.values))
1028+
later_can_rebind = [
1029+
_can_rebind(boolop.values[idx + 1 :]) for idx in range(len(boolop.values))
10251030
]
10261031
self.push_format_context()
10271032
# Process each operand, short-circuiting as needed.
@@ -1038,10 +1043,10 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10381043
# as a condition or explanation reference:
10391044
# - NamedExpr (non-last): reusing the node re-evaluates the
10401045
# walrus expression including any side effects.
1041-
# - Name whose variable a later walrus overwrites: the
1042-
# explanation would show the post-walrus value.
1046+
# - Name whose binding a later operand may change: the
1047+
# explanation would show the value it ended up with.
10431048
needs_snapshot = (isinstance(v, ast.NamedExpr) and i < levels) or (
1044-
isinstance(v, ast.Name) and v.id in later_walrus_targets[i]
1049+
isinstance(v, ast.Name) and later_can_rebind[i]
10451050
)
10461051
if needs_snapshot:
10471052
snapshot = self.assign(ast.Name(res_var, ast.Load()))

‎testing/test_assertrewrite_coverage.py‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
order-call-argument an earlier argument is read too late
2222
order-binop-left a binary operator's left operand is read too late
2323
order-starred-argument a starred argument is read too late
24-
order-name-rebound-by-call a call rebinding a name is not accounted for
2524
order-chained-compare-lazy a chained comparison evaluates every comparator
2625
2726
The xfails are strict on purpose: fixing the behaviour without flipping the
@@ -1284,10 +1283,6 @@ def collect(**kwargs):
12841283
return "passed", mapping
12851284
""")
12861285

1287-
@pytest.mark.xfail(
1288-
strict=True,
1289-
reason="order-name-rebound-by-call: #14820, a call rebinding a global is not seen",
1290-
)
12911286
def test_global_rebound_by_call_precedes_compare(self) -> None:
12921287
assert_evaluation_order("""
12931288
count = 0
@@ -1305,10 +1300,6 @@ def check():
13051300
return "passed", count
13061301
""")
13071302

1308-
@pytest.mark.xfail(
1309-
strict=True,
1310-
reason="order-name-rebound-by-call: #14820, a call rebinding a nonlocal is not seen",
1311-
)
13121303
def test_nonlocal_rebound_by_call_precedes_compare(self) -> None:
13131304
assert_evaluation_order("""
13141305
def check():
@@ -1324,10 +1315,6 @@ def bump():
13241315
return "passed", value
13251316
""")
13261317

1327-
@pytest.mark.xfail(
1328-
strict=True,
1329-
reason="order-name-rebound-by-call: #14820, an earlier argument is read after a rebinding call",
1330-
)
13311318
def test_global_rebound_by_call_precedes_argument(self) -> None:
13321319
assert_evaluation_order("""
13331320
value = "a"

0 commit comments

Comments
 (0)