Fix walrus state leaking between asserts#14570
Closed
marko1olo wants to merge 1 commit into
Closed
Conversation
2f74f31 to
f3e46f4
Compare
f3e46f4 to
b87c761
Compare
Member
|
unattended ai contribution |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14445.
Assertion rewriting keeps
variables_overwritestate 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 laterassertin 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\srcso the local worktree was used:python -m pytest testing\test_assertrewrite.py::TestAssertionRewriteWalrusOperator::test_assertion_walrus_operator_value_changes_cleared_after_each_assert -qpython -m pytest testing\test_assertrewrite.py -k walrus -qpython -m pytest testing\test_assertrewrite.py::TestIssue11028 testing\test_assertrewrite.py::TestIssue11239 -qpython -m pytest testing\test_assertrewrite.py -qpython -m pytest testing\test_assertion.py -qpython -m ruff check src\_pytest\assertion\rewrite.py testing\test_assertrewrite.pypython -m ruff format --check src\_pytest\assertion\rewrite.py testing\test_assertrewrite.pygit diff --check