Fix monkeypatch undo when deleting missing attrs/items with raising=False#14271
Open
kartikdp wants to merge 1 commit intopytest-dev:mainfrom
Open
Fix monkeypatch undo when deleting missing attrs/items with raising=False#14271kartikdp wants to merge 1 commit intopytest-dev:mainfrom
kartikdp wants to merge 1 commit intopytest-dev:mainfrom
Conversation
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.
Summary
Fixes a state-restoration bug in
MonkeyPatchwhen deleting a missing attribute/item withraising=False.Previously:
delattr(..., raising=False)anddelitem(..., raising=False)did not record the missing state.undo()would not reliably restore the original missing state.This change records
NOTSETfor those delete calls, and makesundo()tolerant when the target attribute is already absent.Closes #14094.
Why this matters
Tests that use non-raising deletes expect monkeypatch teardown to fully restore pre-test state. Without this fix, state could leak after tests.
How tested
uv run -m pytest testing/test_monkeypatch.py -k "delattr_non_existing_with_raising_false or delitem_non_existing_with_raising_false or test_delattr or test_delitem"uv run -m pytest testing/test_monkeypatch.pyuv run --with pre-commit pre-commit run --files src/_pytest/monkeypatch.py testing/test_monkeypatch.py changelog/14094.bugfix.rst