Visitar URL original
fix(rewrite): freeze operands against any later side effect (#14820) · pytest-dev/pytest@56e3c3b · GitHub
Skip to content

Commit 56e3c3b

Browse files
fix(rewrite): freeze operands against any later side effect (#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 aa87473 commit 56e3c3b

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
@@ -530,14 +530,19 @@ def traverse_node(node: ast.AST) -> Iterator[ast.AST]:
530530
yield from traverse_node(child)
531531

532532

533-
def _walrus_targets(nodes: Iterable[ast.expr]) -> set[str]:
534-
"""Return the names any walrus operator in *nodes* rebinds."""
535-
return {
536-
sub.target.id
533+
def _can_rebind(nodes: Iterable[ast.expr]) -> bool:
534+
"""Return whether evaluating *nodes* could rebind a name read before them.
535+
536+
A walrus operator rebinds its target outright. A call -- including the
537+
implicit one behind ``await`` -- can rebind anything it declares ``global``
538+
or ``nonlocal``, and there is no way to tell from here which names those
539+
are, so assume the worst.
540+
"""
541+
return any(
542+
isinstance(sub, ast.NamedExpr | ast.Call | ast.Await)
537543
for node in nodes
538544
for sub in ast.walk(node)
539-
if isinstance(sub, ast.NamedExpr)
540-
}
545+
)
541546

542547

543548
@functools.lru_cache(maxsize=1)
@@ -964,34 +969,32 @@ def visit_Name(self, name: ast.Name) -> tuple[ast.Name, str]:
964969
def visit_operand(
965970
self, operand: ast.expr, later: Sequence[ast.expr]
966971
) -> tuple[ast.expr, str]:
967-
"""Visit an operand, freezing it against walrus operators in *later*.
972+
"""Visit an operand, freezing it against side effects in *later*.
968973
969974
Operands are rewritten into statements that run in source order, but
970975
two of them stay unhoisted and are evaluated at the very end, when the
971976
enclosing expression is assembled -- after everything that follows
972977
them:
973978
974-
* a plain name, which a walrus operator in a later operand rebinds in
975-
between, so the value used and the value reported would be the
976-
post-walrus one;
979+
* a plain name, whose binding anything in *later* may have changed by
980+
then, so the value used and the value reported would be the new one;
977981
* a walrus operator itself, which would then assign in the wrong
978982
order, and be visible to the operands that were meant to precede it.
979983
980984
Either way Python evaluates the earlier operand first, so copy it into
981985
a temporary here. A starred argument is unwrapped and rewrapped, its
982986
value being subject to the same problem.
987+
988+
Only a name or a walrus can reach the check below: the visit_* methods
989+
hoist what they build into a temporary and generic_visit assigns
990+
whatever is left -- a literal included -- so anything else is already
991+
ordered by the time it arrives here.
983992
"""
984993
specifiers = set(self.explanation_specifiers)
985994
res, expl = self.visit(operand)
986995
value = res.value if isinstance(res, ast.Starred) else res
987-
if isinstance(value, ast.NamedExpr):
988-
needs_freeze = bool(later)
989-
else:
990-
# Every other operand arrives as a temporary: the visit_* methods
991-
# hoist what they build, and generic_visit assigns whatever is left
992-
# -- a literal included -- so a name is all that can reach here.
993-
assert isinstance(value, ast.Name)
994-
needs_freeze = value.id in _walrus_targets(later)
996+
assert isinstance(value, ast.NamedExpr | ast.Name)
997+
needs_freeze = _can_rebind(later)
995998
if needs_freeze:
996999
snapshot = self.assign(value)
9971000
for key in set(self.explanation_specifiers) - specifiers:
@@ -1011,9 +1014,8 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10111014
body = save = self.statements
10121015
fail_save = self.expl_stmts
10131016
levels = len(boolop.values) - 1
1014-
later_walrus_targets = [
1015-
_walrus_targets(boolop.values[idx + 1 :])
1016-
for idx in range(len(boolop.values))
1017+
later_can_rebind = [
1018+
_can_rebind(boolop.values[idx + 1 :]) for idx in range(len(boolop.values))
10171019
]
10181020
self.push_format_context()
10191021
# Process each operand, short-circuiting as needed.
@@ -1030,10 +1032,10 @@ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]:
10301032
# as a condition or explanation reference:
10311033
# - NamedExpr (non-last): reusing the node re-evaluates the
10321034
# walrus expression including any side effects.
1033-
# - Name whose variable a later walrus overwrites: the
1034-
# explanation would show the post-walrus value.
1035+
# - Name whose binding a later operand may change: the
1036+
# explanation would show the value it ended up with.
10351037
needs_snapshot = (isinstance(v, ast.NamedExpr) and i < levels) or (
1036-
isinstance(v, ast.Name) and v.id in later_walrus_targets[i]
1038+
isinstance(v, ast.Name) and later_can_rebind[i]
10371039
)
10381040
if needs_snapshot:
10391041
snapshot = self.assign(ast.Name(res_var, ast.Load()))
@@ -1167,6 +1169,11 @@ def visit_Compare(self, comp: ast.Compare) -> tuple[ast.expr, str]:
11671169
left_res, left_expl = self.visit_operand(comp.left, comp.comparators)
11681170
if isinstance(comp.left, ast.Compare | ast.BoolOp):
11691171
left_expl = f"({left_expl})"
1172+
if isinstance(left_res, ast.NamedExpr):
1173+
# visit_operand only freezes an operand something later can rebind,
1174+
# so a walrus with nothing but literals after it arrives raw -- and
1175+
# the comparison below would evaluate it a second time.
1176+
left_res = self.assign(left_res)
11701177
res_variables = [self.variable() for i in range(len(comp.ops))]
11711178
load_names: list[ast.expr] = [ast.Name(v, ast.Load()) for v in res_variables]
11721179
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
@@ -1201,6 +1201,58 @@ def collect(**kwargs):
12011201
return "passed", mapping
12021202
""")
12031203

1204+
def test_global_rebound_by_call_precedes_compare(self) -> None:
1205+
assert_evaluation_order("""
1206+
count = 0
1207+
1208+
def bump():
1209+
global count
1210+
count = 99
1211+
return 0
1212+
1213+
def check():
1214+
try:
1215+
assert count == bump()
1216+
except AssertionError:
1217+
return "raised", count
1218+
return "passed", count
1219+
""")
1220+
1221+
def test_nonlocal_rebound_by_call_precedes_compare(self) -> None:
1222+
assert_evaluation_order("""
1223+
def check():
1224+
value = 1
1225+
def bump():
1226+
nonlocal value
1227+
value = 99
1228+
return 1
1229+
try:
1230+
assert value == bump()
1231+
except AssertionError:
1232+
return "raised", value
1233+
return "passed", value
1234+
""")
1235+
1236+
def test_global_rebound_by_call_precedes_argument(self) -> None:
1237+
assert_evaluation_order("""
1238+
value = "a"
1239+
1240+
def bump():
1241+
global value
1242+
value = "b"
1243+
return "x"
1244+
1245+
def collect(*args):
1246+
return args
1247+
1248+
def check():
1249+
try:
1250+
assert collect(value, bump()) == ("a", "x")
1251+
except AssertionError:
1252+
return "raised", value
1253+
return "passed", value
1254+
""")
1255+
12041256
def test_method_lookup_precedes_arguments(self) -> None:
12051257
"""Guard: the bound method is looked up before the arguments run."""
12061258
assert_evaluation_order("""

0 commit comments

Comments
 (0)