Skip to content

Fix walrus state leaking between asserts#14570

Open
marko1olo wants to merge 1 commit into
pytest-dev:mainfrom
marko1olo:fix-walrus-assert-rewrite
Open

Fix walrus state leaking between asserts#14570
marko1olo wants to merge 1 commit into
pytest-dev:mainfrom
marko1olo:fix-walrus-assert-rewrite

Conversation

@marko1olo
Copy link
Copy Markdown

Fixes #14445.

Assertion rewriting keeps variables_overwrite state for walrus expressions so later expressions inside the same rewritten assertion can preserve evaluation order when a target is temporarily renamed. That state currently lives for the whole function scope, so a later assert in the same test can reuse an earlier walrus expression and evaluate it again.

This resets the overwrite state for each assertion. For the cases where the rewriter temporarily renames a walrus target to avoid clobbering a value too early, the generated code now restores the real target after the rewritten comparison/boolean operand has been evaluated. That preserves the walrus side effect for code after the assertion without leaking the expression into later assertions.

Tests run with PYTHONPATH=C:\hades\oss\pytest-14445\src so the local worktree was used:

  • python -m pytest testing\test_assertrewrite.py::TestAssertionRewriteWalrusOperator::test_assertion_walrus_operator_value_changes_cleared_after_each_assert -q
  • python -m pytest testing\test_assertrewrite.py -k walrus -q
  • python -m pytest testing\test_assertrewrite.py::TestIssue11028 testing\test_assertrewrite.py::TestIssue11239 -q
  • python -m pytest testing\test_assertrewrite.py -q
  • python -m pytest testing\test_assertion.py -q
  • external reproducer from Walrus expression duplicate evaluation failures with rewrite #14445 with the two walrus tests: passed
  • python -m ruff check src\_pytest\assertion\rewrite.py testing\test_assertrewrite.py
  • python -m ruff format --check src\_pytest\assertion\rewrite.py testing\test_assertrewrite.py
  • git diff --check

@marko1olo marko1olo force-pushed the fix-walrus-assert-rewrite branch from 2f74f31 to f3e46f4 Compare June 6, 2026 23:52
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 6, 2026
@marko1olo marko1olo force-pushed the fix-walrus-assert-rewrite branch from f3e46f4 to b87c761 Compare June 7, 2026 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Walrus expression duplicate evaluation failures with rewrite

1 participant