Skip to content

Commit 382db6b

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 77dd763 commit 382db6b

3 files changed

Lines changed: 84 additions & 24 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: 31 additions & 24 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,34 +979,32 @@ 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 a name or a walrus can reach the check below: the visit_* methods
999+
hoist what they build into a temporary and generic_visit assigns
1000+
whatever is left -- a literal included -- so anything else is already
1001+
ordered by the time it arrives here.
9931002
"""
9941003
specifiers = set(self.explanation_specifiers)
9951004
res, expl = self.visit(operand)
9961005
value = res.value if isinstance(res, ast.Starred) else res
997-
if isinstance(value, ast.NamedExpr):
998-
needs_freeze = bool(later)
999-
else:
1000-
# Every other operand arrives as a temporary: the visit_* methods
1001-
# hoist what they build, and generic_visit assigns whatever is left
1002-
# -- a literal included -- so a name is all that can reach here.
1003-
assert isinstance(value, ast.Name)
1004-
needs_freeze = value.id in _walrus_targets(later)
1006+
assert isinstance(value, ast.NamedExpr | ast.Name)
1007+
needs_freeze = _can_rebind(later)
10051008
if needs_freeze:
10061009
snapshot = self.assign(value)
10071010
for key in set(self.explanation_specifiers) - specifiers:
@@ -1021,9 +1024,8 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10211024
body = save = self.statements
10221025
fail_save = self.expl_stmts
10231026
levels = len(boolop.values) - 1
1024-
later_walrus_targets = [
1025-
_walrus_targets(boolop.values[idx + 1 :])
1026-
for idx in range(len(boolop.values))
1027+
later_can_rebind = [
1028+
_can_rebind(boolop.values[idx + 1 :]) for idx in range(len(boolop.values))
10271029
]
10281030
self.push_format_context()
10291031
# Process each operand, short-circuiting as needed.
@@ -1040,10 +1042,10 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10401042
# as a condition or explanation reference:
10411043
# - NamedExpr (non-last): reusing the node re-evaluates the
10421044
# walrus expression including any side effects.
1043-
# - Name whose variable a later walrus overwrites: the
1044-
# explanation would show the post-walrus value.
1045+
# - Name whose binding a later operand may change: the
1046+
# explanation would show the value it ended up with.
10451047
needs_snapshot = (isinstance(v, ast.NamedExpr) and i < levels) or (
1046-
isinstance(v, ast.Name) and v.id in later_walrus_targets[i]
1048+
isinstance(v, ast.Name) and later_can_rebind[i]
10471049
)
10481050
if needs_snapshot:
10491051
snapshot = self.assign(ast.Name(res_var, ast.Load()))
@@ -1177,6 +1179,11 @@ def visit_Compare(self, comp: ast.Compare) -> tuple[ast.expr, str]:
11771179
left_res, left_expl = self.visit_operand(comp.left, comp.comparators)
11781180
if isinstance(comp.left, ast.Compare | ast.BoolOp):
11791181
left_expl = f"({left_expl})"
1182+
if isinstance(left_res, ast.NamedExpr):
1183+
# visit_operand only freezes an operand something later can rebind,
1184+
# so a walrus with nothing but literals after it arrives raw -- and
1185+
# the comparison below would evaluate it a second time.
1186+
left_res = self.assign(left_res)
11801187
res_variables = [self.variable() for i in range(len(comp.ops))]
11811188
load_names: list[ast.expr] = [ast.Name(v, ast.Load()) for v in res_variables]
11821189
store_names = [ast.Name(v, ast.Store()) for v in res_variables]

‎testing/test_assertrewrite_coverage.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,58 @@ def collect(**kwargs):
11411141
return "passed", mapping
11421142
""")
11431143

1144+
def test_global_rebound_by_call_precedes_compare(self) -> None:
1145+
assert_evaluation_order("""
1146+
count = 0
1147+
1148+
def bump():
1149+
global count
1150+
count = 99
1151+
return 0
1152+
1153+
def check():
1154+
try:
1155+
assert count == bump()
1156+
except AssertionError:
1157+
return "raised", count
1158+
return "passed", count
1159+
""")
1160+
1161+
def test_nonlocal_rebound_by_call_precedes_compare(self) -> None:
1162+
assert_evaluation_order("""
1163+
def check():
1164+
value = 1
1165+
def bump():
1166+
nonlocal value
1167+
value = 99
1168+
return 1
1169+
try:
1170+
assert value == bump()
1171+
except AssertionError:
1172+
return "raised", value
1173+
return "passed", value
1174+
""")
1175+
1176+
def test_global_rebound_by_call_precedes_argument(self) -> None:
1177+
assert_evaluation_order("""
1178+
value = "a"
1179+
1180+
def bump():
1181+
global value
1182+
value = "b"
1183+
return "x"
1184+
1185+
def collect(*args):
1186+
return args
1187+
1188+
def check():
1189+
try:
1190+
assert collect(value, bump()) == ("a", "x")
1191+
except AssertionError:
1192+
return "raised", value
1193+
return "passed", value
1194+
""")
1195+
11441196
def test_method_lookup_precedes_arguments(self) -> None:
11451197
"""Guard: the bound method is looked up before the arguments run."""
11461198
assert_evaluation_order("""

0 commit comments

Comments
 (0)