Backtestspytestrun-20261106-234554#14445
Walrus expression duplicate evaluation failures with rewrite
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches remove the stale cross-assert `variables_overwrite`/scope state and both make the independent reproducers pass. The difference is in how fully they fix the root cause and in scope. Root cause: `visit_NamedExpr` returned the NamedExpr node itself, which was then reused as a result, as a BoolOp condition, and inside `display()` for the explanation, so the walrus ran again on each reuse. Patch B fixes this at the source: `visit_NamedExpr` assigns the walrus to a temporary once at its position in the statement sequence and the explanation reads the temporary, and `visit_Name` snapshots any load of a name that a walrus rebinds anywhere in the same assert. That gives correct single evaluation, correct left-to-right ordering, and correct explanation values uniformly across Compare, Call, BinOp, BoolOp, UnaryOp and generic paths with no per-visitor special cases. Patch A keeps returning the raw NamedExpr and instead patches each consumer (assign in Compare, snapshot of bare NamedExpr/Name operands in BoolOp, a new `visit_operand` lookahead for Compare/Call/BinOp). Its explanation for a walrus is `display(Name(target))`, a load of the variable evaluated later in the failure block, so a walrus nested inside a Compare operand of a BoolOp whose name is rebound by a later operand (e.g. `assert (a := 1) == 2 or (a := 3) == 4`) still reports the post-rebind value; B's temporaries avoid this. A's lookahead-based design is also considerably harder to reason about and does not cover container literals, IfExp or other generic_visit paths the same way. On tests, A deletes the whole existing `TestAssertionRewriteWalrusOperator` class (181 lines, including the `PYTEST_DONT_REWRITE` and several explanation checks) and reconstitutes only some of them in the coverage file, which is unrelated churn and loses coverage; B keeps the existing suite intact and adds focused regression tests for the issue, a call/boolop reuse case, failure-explanation single evaluation, and the `enable_assertion_pass_hook` path. B is the more correct, simpler and better-scoped fix.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
8m 5s
Wall-clock time in the container
Full test suite
No new failures
206 passed · 0 skipped · 206 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
231vs 588 merged
Changed lines, ours vs merged
The issue
View on GitHubWhen we upgraded pytest from 7.2.0 -> 9.0.3, test failures appeared due to our use of walrus operators (:=) within assert statements. I have narrowed and reproduced this in test_walrus.py below. These two failing tests will _pass_ if we add PYTEST_DONT_REWRITE to the docstring, or run pytest --assert=plain test_walrus.py.
"""
Minimal reproducer: pytest 9.0.3 assertion rewriting evaluates walrus operator (:=) twice.
pytest /tmp/test_walrus.py # FAILS
pytest /tmp/test_walrus.py --assert=plain # PASSES
"""
class Counter:
def __init__(self):
self.value = 0
def increment(self):
self.value += 1
def test_walrus_in_assertion_basic():
"""Walrus captures wrong value: rewriter evaluates := twice."""
c = Counter()
assert (before := c.value) == 0
c.increment()
assert before != (after := c.value)
def test_walrus_running_counter():
"""Cumulative walrus increments fire twice each → count doubles."""
count = 0
items = []
items.append("a")
assert (count := count + 1) == len(items) # count becomes 2, not 1
items.append("b")
assert (count := count + 1) == len(items) # count becomes 4, not 2
items.append("c")
assert (count := count + 1) == len(items) # count becomes 6, not 3
assert count == 3 # fails: count is 6
- a detailed description of the bug or problem you are having
- output of
pip listfrom the virtual environment you are using - pytest and operating system versions
- minimal example if possible
% python --version
Python 3.13.3
% pip list
Package Version
--------- -------
iniconfig 2.3.0
packaging 26.2
pip 25.0.1
pluggy 1.6.0
Pygments 2.20.0
pytest 9.0.3
I suspect related changes are https://github.com/pytest-dev/pytest/pull/10758 and https://github.com/pytest-dev/pytest/pull/11041
The two patches
changelog/14445.bugfix.rstnew file mode 100644index 000000000..b596adf03@@ -0,0 +1 @@+Fixed assertion rewriting evaluating walrus operator (``:=``) expressions more than once, which could give variables wrong values, for example when the assigned name was used again in a later ``assert``.src/_pytest/assertion/rewrite.pyindex 3f44ce72f..2cbbd7553 100644@@ -3,7 +3,6 @@ from __future__ import annotations import ast-from collections import defaultdict from collections.abc import Callable from collections.abc import Iterable from collections.abc import Iterator@@ -57,10 +56,6 @@ if TYPE_CHECKING: from _pytest.assertion import AssertionState -class Sentinel:- pass-- assertstate_key = StashKey["AssertionState"]() # pytest caches rewritten pycs in pycache dirs@@ -68,9 +63,6 @@ PYTEST_TAG = f"{sys.implementation.cache_tag}-pytest-{version}" PYC_EXT = ".py" + ((__debug__ and "c") or "o") PYC_TAIL = "." + PYTEST_TAG + PYC_EXT -# Special marker that denotes we have just left a scope definition-_SCOPE_END_MARKER = Sentinel()- class AssertionRewritingHook(importlib.abc.MetaPathFinder, importlib.abc.Loader): """PEP302/PEP451 import hook which rewrites asserts."""@@ -642,14 +634,13 @@ class AssertionRewriter(ast.NodeVisitor): .push_format_context() and .pop_format_context() which allows to build another %-formatted string while already building one. - :scope: A tuple containing the current scope used for variables_overwrite.+ :walrus_targets: The names assigned with the walrus operator anywhere+ in the current assert; loads of these names are snapshotted into+ temporary variables so that they keep the value they had at that+ point of the evaluation. - :variables_overwrite: A dict filled with references to variables- that change value within an assert. This happens when a variable is- reassigned with the walrus operator-- This state, except the variables_overwrite, is reset on every new assert- statement visited and used by the other visitors.+ This state is reset on every new assert statement visited and used+ by the other visitors. """ def __init__(@@ -665,10 +656,6 @@ class AssertionRewriter(ast.NodeVisitor): else: self.enable_assertion_pass_hook = False self.source = source- self.scope: tuple[ast.AST, ...] = ()- self.variables_overwrite: defaultdict[tuple[ast.AST, ...], dict[str, str]] = (- defaultdict(dict)- ) def run(self, mod: ast.Module) -> None: """Find all assert statements in *mod* and rewrite them."""@@ -718,17 +705,9 @@ class AssertionRewriter(ast.NodeVisitor): mod.body[pos:pos] = imports # Collect asserts.- self.scope = (mod,)- nodes: list[ast.AST | Sentinel] = [mod]+ nodes: list[ast.AST] = [mod] while nodes: node = nodes.pop()- if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef):- self.scope = (*self.scope, node)- nodes.append(_SCOPE_END_MARKER)- if node == _SCOPE_END_MARKER:- self.scope = self.scope[:-1]- continue- assert isinstance(node, ast.AST) for name, field in ast.iter_fields(node): if isinstance(field, list): new: list[ast.AST] = []@@ -859,6 +838,11 @@ class AssertionRewriter(ast.NodeVisitor): self.statements: list[ast.stmt] = [] self.variables: list[str] = [] self.variable_counter = itertools.count()+ self.walrus_targets = {+ node.target.id+ for node in ast.walk(assert_.test)+ if isinstance(node, ast.NamedExpr)+ } if self.enable_assertion_pass_hook: self.format_variables: list[str] = []@@ -953,27 +937,27 @@ class AssertionRewriter(ast.NodeVisitor): ast.copy_location(node, assert_) return self.statements - def visit_NamedExpr(self, name: ast.NamedExpr) -> tuple[ast.NamedExpr, str]:- # This method handles the 'walrus operator' repr of the target- # name if it's a local variable or _should_repr_global_name()- # thinks it's acceptable.- locs = ast.Call(self.builtin("locals"), [], [])- target_id = name.target.id- inlocs = ast.Compare(ast.Constant(target_id), [ast.In()], [locs])- dorepr = self.helper("_should_repr_global_name", name)- test = ast.BoolOp(ast.Or(), [inlocs, dorepr])- expr = ast.IfExp(test, self.display(name), ast.Constant(target_id))- return name, self.explanation_param(expr)+ def visit_NamedExpr(self, name: ast.NamedExpr) -> tuple[ast.Name, str]:+ # Evaluate the walrus exactly once, at its place in the evaluation+ # order, and refer to the stored result afterwards.+ res = self.assign(name)+ return res, self.explanation_param(self.display(res)) def visit_Name(self, name: ast.Name) -> tuple[ast.Name, str]: # Display the repr of the name if it's a local variable or # _should_repr_global_name() thinks it's acceptable.+ value: ast.Name = name+ if name.id in self.walrus_targets:+ # The name is rebound by a walrus within this assert: capture its+ # current value so that later reassignments don't affect the+ # result nor the explanation.+ value = self.assign(name) locs = ast.Call(self.builtin("locals"), [], []) inlocs = ast.Compare(ast.Constant(name.id), [ast.In()], [locs])- dorepr = self.helper("_should_repr_global_name", name)+ dorepr = self.helper("_should_repr_global_name", value) test = ast.BoolOp(ast.Or(), [inlocs, dorepr])- expr = ast.IfExp(test, self.display(name), ast.Constant(name.id))- return name, self.explanation_param(expr)+ expr = ast.IfExp(test, self.display(value), ast.Constant(name.id))+ return value, self.explanation_param(expr) def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]: res_var = self.variable()@@ -991,17 +975,6 @@ class AssertionRewriter(ast.NodeVisitor): # cond is set in a prior loop iteration below self.expl_stmts.append(ast.If(cond, fail_inner, [])) # noqa: F821 self.expl_stmts = fail_inner- match v:- # Check if the left operand is an ast.NamedExpr and the value has already been visited- case ast.Compare(- left=ast.NamedExpr(target=ast.Name(id=target_id))- ) if target_id in [- e.id for e in boolop.values[:i] if hasattr(e, "id")- ]:- pytest_temp = self.variable()- self.variables_overwrite[self.scope][target_id] = v.left # type:ignore[assignment]- # mypy's false positive, we're checking that the 'target' attribute exists.- v.left.target.id = pytest_temp # type:ignore[attr-defined] self.push_format_context() res, expl = self.visit(v) body.append(ast.Assign([ast.Name(res_var, ast.Store())], res))@@ -1043,19 +1016,10 @@ class AssertionRewriter(ast.NodeVisitor): new_args = [] new_kwargs = [] for arg in call.args:- if isinstance(arg, ast.Name) and arg.id in self.variables_overwrite.get(- self.scope, {}- ):- arg = self.variables_overwrite[self.scope][arg.id] # type:ignore[assignment] res, expl = self.visit(arg) arg_expls.append(expl) new_args.append(res) for keyword in call.keywords:- match keyword.value:- case ast.Name(id=id) if id in self.variables_overwrite.get(- self.scope, {}- ):- keyword.value = self.variables_overwrite[self.scope][id] # type:ignore[assignment] res, expl = self.visit(keyword.value) new_kwargs.append(ast.keyword(keyword.arg, res)) if keyword.arg:@@ -1090,14 +1054,6 @@ class AssertionRewriter(ast.NodeVisitor): def visit_Compare(self, comp: ast.Compare) -> tuple[ast.expr, str]: self.push_format_context()- # We first check if we have overwritten a variable in the previous assert- match comp.left:- case ast.Name(id=name_id) if name_id in self.variables_overwrite.get(- self.scope, {}- ):- comp.left = self.variables_overwrite[self.scope][name_id] # type: ignore[assignment]- case ast.NamedExpr(target=ast.Name(id=target_id)):- self.variables_overwrite[self.scope][target_id] = comp.left # type: ignore[assignment] left_res, left_expl = self.visit(comp.left) if isinstance(comp.left, ast.Compare | ast.BoolOp): left_expl = f"({left_expl})"@@ -1109,14 +1065,6 @@ class AssertionRewriter(ast.NodeVisitor): syms: list[ast.expr] = [] results = [left_res] for i, op, next_operand in it:- match (next_operand, left_res):- case (- ast.NamedExpr(target=ast.Name(id=target_id)),- ast.Name(id=name_id),- ) if target_id == name_id:- next_operand.target.id = self.variable()- self.variables_overwrite[self.scope][name_id] = next_operand # type: ignore[assignment]- next_res, next_expl = self.visit(next_operand) if isinstance(next_operand, ast.Compare | ast.BoolOp): next_expl = f"({next_expl})"testing/test_assertrewrite.pyindex ef038383e..a15e5b564 100644@@ -1823,6 +1823,132 @@ class TestAssertionRewriteWalrusOperator: result.stdout.fnmatch_lines(["*assert ('World' == 'Test'*"]) +class TestIssue14445:+ """Walrus expressions in asserts must be evaluated exactly once."""++ def test_walrus_value_not_reevaluated_in_later_assert(+ self, pytester: Pytester+ ) -> None:+ pytester.makepyfile(+ """+ class Counter:+ def __init__(self):+ self.value = 0++ def increment(self):+ self.value += 1++ def test_walrus_in_assertion_basic():+ c = Counter()+ assert (before := c.value) == 0+ c.increment()+ assert before != (after := c.value)+ assert before == 0+ assert after == 1+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=1)++ def test_walrus_running_counter(self, pytester: Pytester) -> None:+ pytester.makepyfile(+ """+ def test_walrus_running_counter():+ count = 0+ items = []+ items.append("a")+ assert (count := count + 1) == len(items)+ items.append("b")+ assert (count := count + 1) == len(items)+ items.append("c")+ assert (count := count + 1) == len(items)+ assert count == 3+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=1)++ def test_walrus_name_reused_in_call_and_boolop(self, pytester: Pytester) -> None:+ pytester.makepyfile(+ """+ calls = []++ def bump(x):+ calls.append(x)+ return x + 1++ def ident(a=None):+ return a++ def test_reuse():+ n = 0+ assert (n := bump(n)) == 1 and ident(n) == 1 and ident(a=n) == 1+ assert ident(n) == 1+ assert ident(a=n) == 1+ assert n == 1 and n != (n := bump(n)) and n == 2+ assert calls == [0, 1]+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=1)++ def test_walrus_not_reevaluated_for_failure_explanation(+ self, pytester: Pytester+ ) -> None:+ pytester.makepyfile(+ """+ calls = []++ def bump(x):+ calls.append(x)+ return x + 1++ def test_fails():+ n = 0+ try:+ assert (n := bump(n)) == 5+ except AssertionError:+ pass+ assert calls == [0]+ assert n == 1++ def test_explanation():+ n = 0+ assert n == (n := bump(n))+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=1, failed=1)+ result.stdout.fnmatch_lines(["E*assert 0 == 1"])++ def test_walrus_evaluated_once_with_assertion_pass_hook(+ self, pytester: Pytester+ ) -> None:+ pytester.makeini(+ """+ [pytest]+ enable_assertion_pass_hook = True+ """+ )+ pytester.makeconftest(+ """+ def pytest_assertion_pass(item, lineno, orig, expl):+ pass+ """+ )+ pytester.makepyfile(+ """+ def test_walrus_running_counter():+ count = 0+ assert (count := count + 1) == 1+ assert count != (count := count + 1)+ assert count == 2+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=1)++ class TestIssue11028: def test_assertion_walrus_operator_in_operand(self, pytester: Pytester) -> None: pytester.makepyfile(
changelog/14445.bugfix.rstnew file mode 100644index 000000000..68b37754b@@ -0,0 +1,3 @@+Assertion rewriting no longer evaluates walrus operator (``:=``) expressions more than once, which reported results the test never computed when the expression had side effects.++An operand evaluated before a later walrus is now reported with the value it actually saw, so ``assert value != identity(value := value.lower())`` keeps Python's left-to-right order.src/_pytest/assertion/rewrite.pyindex 3f44ce72f..27953336c 100644@@ -3,7 +3,6 @@ from __future__ import annotations import ast-from collections import defaultdict from collections.abc import Callable from collections.abc import Iterable from collections.abc import Iterator@@ -57,10 +56,6 @@ if TYPE_CHECKING: from _pytest.assertion import AssertionState -class Sentinel:- pass-- assertstate_key = StashKey["AssertionState"]() # pytest caches rewritten pycs in pycache dirs@@ -68,9 +63,6 @@ PYTEST_TAG = f"{sys.implementation.cache_tag}-pytest-{version}" PYC_EXT = ".py" + ((__debug__ and "c") or "o") PYC_TAIL = "." + PYTEST_TAG + PYC_EXT -# Special marker that denotes we have just left a scope definition-_SCOPE_END_MARKER = Sentinel()- class AssertionRewritingHook(importlib.abc.MetaPathFinder, importlib.abc.Loader): """PEP302/PEP451 import hook which rewrites asserts."""@@ -538,6 +530,16 @@ def traverse_node(node: ast.AST) -> Iterator[ast.AST]: yield from traverse_node(child) +def _walrus_targets(nodes: Iterable[ast.expr]) -> set[str]:+ """Return the names any walrus operator in *nodes* rebinds."""+ return {+ sub.target.id+ for node in nodes+ for sub in ast.walk(node)+ if isinstance(sub, ast.NamedExpr)+ }++ @functools.lru_cache(maxsize=1) def _get_assertion_exprs(src: bytes) -> dict[int, str]: """Return a mapping from {lineno: "assertion test expression"}."""@@ -642,14 +644,8 @@ class AssertionRewriter(ast.NodeVisitor): .push_format_context() and .pop_format_context() which allows to build another %-formatted string while already building one. - :scope: A tuple containing the current scope used for variables_overwrite.-- :variables_overwrite: A dict filled with references to variables- that change value within an assert. This happens when a variable is- reassigned with the walrus operator-- This state, except the variables_overwrite, is reset on every new assert- statement visited and used by the other visitors.+ This state is reset on every new assert statement visited and used by+ the other visitors. """ def __init__(@@ -665,10 +661,6 @@ class AssertionRewriter(ast.NodeVisitor): else: self.enable_assertion_pass_hook = False self.source = source- self.scope: tuple[ast.AST, ...] = ()- self.variables_overwrite: defaultdict[tuple[ast.AST, ...], dict[str, str]] = (- defaultdict(dict)- ) def run(self, mod: ast.Module) -> None: """Find all assert statements in *mod* and rewrite them."""@@ -718,16 +710,9 @@ class AssertionRewriter(ast.NodeVisitor): mod.body[pos:pos] = imports # Collect asserts.- self.scope = (mod,)- nodes: list[ast.AST | Sentinel] = [mod]+ nodes: list[ast.AST] = [mod] while nodes: node = nodes.pop()- if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef):- self.scope = (*self.scope, node)- nodes.append(_SCOPE_END_MARKER)- if node == _SCOPE_END_MARKER:- self.scope = self.scope[:-1]- continue assert isinstance(node, ast.AST) for name, field in ast.iter_fields(node): if isinstance(field, list):@@ -954,15 +939,16 @@ class AssertionRewriter(ast.NodeVisitor): return self.statements def visit_NamedExpr(self, name: ast.NamedExpr) -> tuple[ast.NamedExpr, str]:- # This method handles the 'walrus operator' repr of the target- # name if it's a local variable or _should_repr_global_name()- # thinks it's acceptable.+ # Return the NamedExpr as-is so it evaluates in its natural position+ # (preserving left-to-right evaluation order in function calls, etc.).+ # For the explanation, reference the target variable. locs = ast.Call(self.builtin("locals"), [], []) target_id = name.target.id+ target_name = ast.Name(target_id, ast.Load()) inlocs = ast.Compare(ast.Constant(target_id), [ast.In()], [locs])- dorepr = self.helper("_should_repr_global_name", name)+ dorepr = self.helper("_should_repr_global_name", target_name) test = ast.BoolOp(ast.Or(), [inlocs, dorepr])- expr = ast.IfExp(test, self.display(name), ast.Constant(target_id))+ expr = ast.IfExp(test, self.display(target_name), ast.Constant(target_id)) return name, self.explanation_param(expr) def visit_Name(self, name: ast.Name) -> tuple[ast.Name, str]:@@ -975,6 +961,27 @@ class AssertionRewriter(ast.NodeVisitor): expr = ast.IfExp(test, self.display(name), ast.Constant(name.id)) return name, self.explanation_param(expr) + def visit_operand(+ self, operand: ast.expr, later: Sequence[ast.expr]+ ) -> tuple[ast.expr, str]:+ """Visit an operand, freezing it against walrus operators in *later*.++ Operands are rewritten into statements that run in source order, but+ a plain name is left as a bare load evaluated at the very end, when+ the enclosing expression is assembled. A walrus operator in a later+ operand rebinds that name in between, so both the value used and the+ value reported would be the post-walrus one -- Python evaluates the+ earlier operand first. Copy the value into a temporary instead.+ """+ specifiers = set(self.explanation_specifiers)+ res, expl = self.visit(operand)+ if isinstance(res, ast.Name) and res.id in _walrus_targets(later):+ snapshot = self.assign(res)+ for key in set(self.explanation_specifiers) - specifiers:+ self.explanation_specifiers[key] = self.display(snapshot)+ res = snapshot+ return res, expl+ def visit_BoolOp(self, boolop: ast.BoolOp) -> tuple[ast.Name, str]: res_var = self.variable() expl_list = self.assign(ast.List([], ast.Load()))@@ -983,32 +990,43 @@ class AssertionRewriter(ast.NodeVisitor): body = save = self.statements fail_save = self.expl_stmts levels = len(boolop.values) - 1+ later_walrus_targets = [+ _walrus_targets(boolop.values[idx + 1 :])+ for idx in range(len(boolop.values))+ ] self.push_format_context()- # Process each operand, short-circuiting if needed.+ # Process each operand, short-circuiting as needed. for i, v in enumerate(boolop.values): if i: fail_inner: list[ast.stmt] = [] # cond is set in a prior loop iteration below self.expl_stmts.append(ast.If(cond, fail_inner, [])) # noqa: F821 self.expl_stmts = fail_inner- match v:- # Check if the left operand is an ast.NamedExpr and the value has already been visited- case ast.Compare(- left=ast.NamedExpr(target=ast.Name(id=target_id))- ) if target_id in [- e.id for e in boolop.values[:i] if hasattr(e, "id")- ]:- pytest_temp = self.variable()- self.variables_overwrite[self.scope][target_id] = v.left # type:ignore[assignment]- # mypy's false positive, we're checking that the 'target' attribute exists.- v.left.target.id = pytest_temp # type:ignore[attr-defined] self.push_format_context() res, expl = self.visit(v) body.append(ast.Assign([ast.Name(res_var, ast.Store())], res))+ # Snapshot when the raw ``res`` node would be unsafe to reuse+ # as a condition or explanation reference:+ # - NamedExpr (non-last): reusing the node re-evaluates the+ # walrus expression including any side effects.+ # - Name whose variable a later walrus overwrites: the+ # explanation would show the post-walrus value.+ needs_snapshot = (isinstance(v, ast.NamedExpr) and i < levels) or (+ isinstance(v, ast.Name) and v.id in later_walrus_targets[i]+ )+ if needs_snapshot:+ snapshot = self.assign(ast.Name(res_var, ast.Load()))+ res = snapshot+ for key in self.stack[-1]:+ self.stack[-1][key] = self.display(snapshot) expl_format = self.pop_format_context(ast.Constant(expl)) call = ast.Call(app, [expl_format], []) self.expl_stmts.append(ast.Expr(call)) if i < levels:+ # Short-circuit: and → continue if truthy; or → if falsy.+ # ``res`` is a stable reference (Name vars are only+ # snapshotted when a later walrus would corrupt them;+ # calls/compares return @py_assert vars from assign()). cond: ast.expr = res if is_or: cond = ast.UnaryOp(ast.Not(), cond)@@ -1029,7 +1047,7 @@ class AssertionRewriter(ast.NodeVisitor): def visit_BinOp(self, binop: ast.BinOp) -> tuple[ast.Name, str]: symbol = BINOP_MAP[binop.op.__class__]- left_expr, left_expl = self.visit(binop.left)+ left_expr, left_expl = self.visit_operand(binop.left, [binop.right]) right_expr, right_expl = self.visit(binop.right) explanation = f"({left_expl} {symbol} {right_expl})" res = self.assign(@@ -1038,25 +1056,19 @@ class AssertionRewriter(ast.NodeVisitor): return res, explanation def visit_Call(self, call: ast.Call) -> tuple[ast.Name, str]:- new_func, func_expl = self.visit(call.func)+ # The callee and every argument are evaluated left to right, so each of+ # them has to be frozen against walrus operators in what follows.+ operands = [*call.args, *(keyword.value for keyword in call.keywords)]+ new_func, func_expl = self.visit_operand(call.func, operands) arg_expls = [] new_args = [] new_kwargs = []- for arg in call.args:- if isinstance(arg, ast.Name) and arg.id in self.variables_overwrite.get(- self.scope, {}- ):- arg = self.variables_overwrite[self.scope][arg.id] # type:ignore[assignment]- res, expl = self.visit(arg)+ for i, arg in enumerate(call.args):+ res, expl = self.visit_operand(arg, operands[i + 1 :]) arg_expls.append(expl) new_args.append(res)- for keyword in call.keywords:- match keyword.value:- case ast.Name(id=id) if id in self.variables_overwrite.get(- self.scope, {}- ):- keyword.value = self.variables_overwrite[self.scope][id] # type:ignore[assignment]- res, expl = self.visit(keyword.value)+ for i, keyword in enumerate(call.keywords, start=len(call.args)):+ res, expl = self.visit_operand(keyword.value, operands[i + 1 :]) new_kwargs.append(ast.keyword(keyword.arg, res)) if keyword.arg: arg_expls.append(keyword.arg + "=" + expl)@@ -1090,17 +1102,11 @@ class AssertionRewriter(ast.NodeVisitor): def visit_Compare(self, comp: ast.Compare) -> tuple[ast.expr, str]: self.push_format_context()- # We first check if we have overwritten a variable in the previous assert- match comp.left:- case ast.Name(id=name_id) if name_id in self.variables_overwrite.get(- self.scope, {}- ):- comp.left = self.variables_overwrite[self.scope][name_id] # type: ignore[assignment]- case ast.NamedExpr(target=ast.Name(id=target_id)):- self.variables_overwrite[self.scope][target_id] = comp.left # type: ignore[assignment]- left_res, left_expl = self.visit(comp.left)+ left_res, left_expl = self.visit_operand(comp.left, comp.comparators) if isinstance(comp.left, ast.Compare | ast.BoolOp): left_expl = f"({left_expl})"+ if isinstance(left_res, ast.NamedExpr):+ left_res = self.assign(left_res) res_variables = [self.variable() for i in range(len(comp.ops))] load_names: list[ast.expr] = [ast.Name(v, ast.Load()) for v in res_variables] store_names = [ast.Name(v, ast.Store()) for v in res_variables]@@ -1109,17 +1115,13 @@ class AssertionRewriter(ast.NodeVisitor): syms: list[ast.expr] = [] results = [left_res] for i, op, next_operand in it:- match (next_operand, left_res):- case (- ast.NamedExpr(target=ast.Name(id=target_id)),- ast.Name(id=name_id),- ) if target_id == name_id:- next_operand.target.id = self.variable()- self.variables_overwrite[self.scope][name_id] = next_operand # type: ignore[assignment]-- next_res, next_expl = self.visit(next_operand)+ next_res, next_expl = self.visit_operand(+ next_operand, comp.comparators[i + 1 :]+ ) if isinstance(next_operand, ast.Compare | ast.BoolOp): next_expl = f"({next_expl})"+ if isinstance(next_res, ast.NamedExpr):+ next_res = self.assign(next_res) results.append(next_res) sym = BINOP_MAP[op.__class__] syms.append(ast.Constant(sym))testing/test_assertrewrite.pyindex ef038383e..c9736f8fa 100644@@ -1637,190 +1637,29 @@ class TestIssue2121: result.stdout.fnmatch_lines(["*E*assert (1 + 1) == 3"]) -class TestAssertionRewriteWalrusOperator:- """See #10743"""-- def test_assertion_walrus_operator(self, pytester: Pytester) -> None:- pytester.makepyfile(- """- def my_func(before, after):- return before == after-- def change_value(value):- return value.lower()-- def test_walrus_conversion():- a = "Hello"- assert not my_func(a, a := change_value(a))- assert a == "hello"- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_walrus_operator_dont_rewrite(self, pytester: Pytester) -> None:- pytester.makepyfile(- """- 'PYTEST_DONT_REWRITE'- def my_func(before, after):- return before == after-- def change_value(value):- return value.lower()-- def test_walrus_conversion_dont_rewrite():- a = "Hello"- assert not my_func(a, a := change_value(a))- assert a == "hello"- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_inline_walrus_operator(self, pytester: Pytester) -> None:- pytester.makepyfile(- """- def my_func(before, after):- return before == after-- def test_walrus_conversion_inline():- a = "Hello"- assert not my_func(a, a := a.lower())- assert a == "hello"- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_inline_walrus_operator_reverse(self, pytester: Pytester) -> None:- pytester.makepyfile(- """- def my_func(before, after):- return before == after-- def test_walrus_conversion_reverse():- a = "Hello"- assert my_func(a := a.lower(), a)- assert a == 'hello'- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_walrus_no_variable_name_conflict(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_walrus_conversion_no_conflict():- a = "Hello"- assert a == (b := a.lower())- """- )- result = pytester.runpytest()- assert result.ret == 1- result.stdout.fnmatch_lines(["*AssertionError: assert 'Hello' == 'hello'"])-- def test_assertion_walrus_operator_true_assertion_and_changes_variable_value(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_walrus_conversion_succeed():- a = "Hello"- assert a != (a := a.lower())- assert a == 'hello'- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_walrus_operator_fail_assertion(self, pytester: Pytester) -> None:- pytester.makepyfile(- """- def test_walrus_conversion_fails():- a = "Hello"- assert a == (a := a.lower())- """- )- result = pytester.runpytest()- assert result.ret == 1- result.stdout.fnmatch_lines(["*AssertionError: assert 'Hello' == 'hello'"])-- def test_assertion_walrus_operator_boolean_composite(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_walrus_operator_change_boolean_value():- a = True- assert a and True and ((a := False) is False) and (a is False) and ((a := None) is None)- assert a is None- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_walrus_operator_compare_boolean_fails(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_walrus_operator_change_boolean_value():- a = True- assert not (a and ((a := False) is False))- """- )- result = pytester.runpytest()- assert result.ret == 1- result.stdout.fnmatch_lines(["*assert not (True and False is False)"])+def test_walrus_rebinding_does_not_outlive_its_statement(+ pytester: Pytester,+) -> None:+ """A walrus target must not be rebound by a later, unrelated assertion. - def test_assertion_walrus_operator_boolean_none_fails(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_walrus_operator_change_boolean_value():- a = True- assert not (a and ((a := None) is None))+ The rest of #10743's suite moved to the coverage matrix, which runs+ in-process. This one stays here because it needs two tests in one module+ to say anything: the rewriter may keep no state that survives a statement,+ let alone a test. The matrix cannot express that.+ """+ pytester.makepyfile( """- )- result = pytester.runpytest()- assert result.ret == 1- result.stdout.fnmatch_lines(["*assert not (True and None is None)"])+ def test_walrus_operator_change_value():+ a = True+ assert (a := None) is None - def test_assertion_walrus_operator_value_changes_cleared_after_each_test(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_walrus_operator_change_value():- a = True- assert (a := None) is None-- def test_walrus_operator_not_override_value():- a = True- assert a is True- """- )- result = pytester.runpytest()- assert result.ret == 0-- def test_assertion_namedexpr_compare_left_overwrite(- self, pytester: Pytester- ) -> None:- pytester.makepyfile(- """- def test_namedexpr_compare_left_overwrite():- a = "Hello"- b = "World"- c = "Test"- assert (a := b) == c and (a := "Test") == "Test"- """- )- result = pytester.runpytest()- assert result.ret == 1- result.stdout.fnmatch_lines(["*assert ('World' == 'Test'*"])+ def test_walrus_operator_not_override_value():+ a = True+ assert a is True+ """+ )+ result = pytester.runpytest()+ assert result.ret == 0 class TestIssue11028:@@ -1931,6 +1770,49 @@ class TestIssue11239: assert result.ret == 0 +class TestIssue14445:+ """Regression tests for #14445: walrus operator double evaluation."""++ def test_walrus_no_double_eval_basic(self, pytester: Pytester) -> None:+ """Walrus captures the value at assignment time, not re-evaluated later."""+ pytester.makepyfile(+ """+ class Counter:+ def __init__(self):+ self.value = 0+ def increment(self):+ self.value += 1++ def test_walrus_in_assertion_basic():+ c = Counter()+ assert (before := c.value) == 0+ c.increment()+ assert before != (after := c.value)+ """+ )+ result = pytester.runpytest()+ assert result.ret == 0++ def test_walrus_no_double_eval_running_counter(self, pytester: Pytester) -> None:+ """Walrus increments fire exactly once per assert statement."""+ pytester.makepyfile(+ """+ def test_walrus_running_counter():+ count = 0+ items = []+ items.append("a")+ assert (count := count + 1) == len(items)+ items.append("b")+ assert (count := count + 1) == len(items)+ items.append("c")+ assert (count := count + 1) == len(items)+ assert count == 3+ """+ )+ result = pytester.runpytest()+ assert result.ret == 0++ @pytest.mark.skipif( sys.maxsize <= (2**31 - 1), reason="Causes OverflowError on 32bit systems" )testing/test_assertrewrite_coverage.pyindex 3bc16d291..43a3f668f 100644@@ -674,6 +674,41 @@ class TestIntrospectionWalrus: assert (y := x * 2) == 100 """) + def test_walrus_in_boolop_reports_each_operand(self) -> None:+ """Two walrus assignments to one name: each operand shows what it saw."""+ assert_introspects(+ """+ def check():+ def side_effect():+ return True+ assert (x := side_effect()) and (x := False)+ """,+ must_contain=["assert (True and False)"],+ )++ def test_walrus_in_boolop_reports_assigned_value(self) -> None:+ assert_introspects(+ """+ def check():+ a = True+ assert not (a and ((a := False) is False))+ """,+ must_contain=["assert not (True and False is False)"],+ )++ def test_walrus_in_boolop_reports_left_operand(self) -> None:+ """A comparator walrus must not overwrite the left operand's report."""+ assert_introspects(+ """+ def check():+ a = "Hello"+ b = "World"+ c = "Test"+ assert (a := b) == c and (a := "Test") == "Test"+ """,+ must_contain=["assert ('World' == 'Test'"],+ )+ # --------------------------------------------------------------------------- # Single-evaluation tests: ensure no expression is evaluated multiple times@@ -763,6 +798,33 @@ class TestSingleEvaluation: assert d["a"] == 100 """) + def test_walrus_in_compare_evaluated_once(self) -> None:+ assert_single_evaluation("""+ def check():+ def side_effect():+ counter[0] += 1+ return 42+ assert (x := side_effect()) == 100+ """)++ def test_walrus_in_boolean_evaluated_once(self) -> None:+ assert_single_evaluation("""+ def check():+ def side_effect():+ counter[0] += 1+ return 42+ assert (x := side_effect()) and False+ """)++ def test_walrus_in_chained_compare_evaluated_once(self) -> None:+ assert_single_evaluation("""+ def check():+ def side_effect():+ counter[0] += 1+ return 5+ assert 1 < (x := side_effect()) < 3+ """)+ def test_method_call_evaluated_once(self) -> None: assert_single_evaluation(""" def check():@@ -836,6 +898,97 @@ class TestEvaluationOrder: given it. """ + def test_compare_left_operand_precedes_walrus(self) -> None:+ assert_evaluation_order("""+ def check():+ def identity(v):+ return v+ value = "Hello"+ try:+ assert value != identity(value := value.lower())+ except AssertionError:+ return "raised", value+ return "passed", value+ """)++ def test_compare_reports_left_operand(self) -> None:+ assert_introspects(+ """+ def check():+ def identity(v):+ return v+ value = 2+ assert value == identity(value := 3)+ """,+ must_contain=["assert 2 == 3"],+ )++ def test_call_earlier_argument_precedes_walrus(self) -> None:+ assert_evaluation_order("""+ def check():+ def identity(v):+ return v+ def collect(*values):+ return values+ value = "Hello"+ try:+ assert collect(value, identity(value := value.lower())) == ("Hello", "hello")+ except AssertionError:+ return "raised", value+ return "passed", value+ """)++ def test_call_later_argument_sees_walrus_value(self) -> None:+ """The mirror of the case above: a later operand sees the new value."""+ assert_evaluation_order("""+ def check():+ def collect(*values):+ return values+ value = "Hello"+ try:+ assert collect(value := value.lower(), value) == ("hello", "hello")+ except AssertionError:+ return "raised", value+ return "passed", value+ """)++ def test_boolop_chain_rebinds_in_order(self) -> None:+ assert_evaluation_order("""+ def check():+ a = True+ try:+ assert a and True and ((a := False) is False) and (a is False) and ((a := None) is None)+ except AssertionError:+ return "raised", a+ return "passed", a+ """)++ def test_binop_left_operand_precedes_walrus(self) -> None:+ assert_evaluation_order("""+ def check():+ def identity(v):+ return v+ value = 1+ try:+ assert value + identity(value := 5) == 6+ except AssertionError:+ return "raised", value+ return "passed", value+ """)++ def test_chained_compare_operands_in_order(self) -> None:+ assert_evaluation_order("""+ def check():+ def identity(v):+ return v+ value = 1+ try:+ assert value < identity(value := 5) < 9+ except AssertionError:+ return "raised", value+ return "passed", value+ """)+ def test_container_literal_operand_in_order(self) -> None: """Guard: ``generic_visit`` hoists container literals into a temporary.""" assert_evaluation_order("""@@ -914,6 +1067,36 @@ class TestEvaluationOrder: return "passed", obj """) + def test_keyword_argument_precedes_walrus(self) -> None:+ assert_evaluation_order("""+ def check():+ def identity(v):+ return v+ def collect(**kwargs):+ return kwargs+ value = 1+ try:+ assert collect(a=value, b=identity(value := 2)) == {"a": 1, "b": 2}+ except AssertionError:+ return "raised", value+ return "passed", value+ """)++ def test_double_star_argument_precedes_walrus(self) -> None:+ assert_evaluation_order("""+ def check():+ def identity(v):+ return v+ def collect(**kwargs):+ return kwargs+ mapping = {"a": 1}+ try:+ assert collect(**mapping, b=identity(mapping := {"a": 9})) == {"a": 1, "b": {"a": 9}}+ except AssertionError:+ return "raised", mapping+ return "passed", mapping+ """)+ def test_ifexp_branches_in_order(self) -> None: """Guard: the condition is evaluated before the selected branch.""" assert_evaluation_order("""