Skip to content

Fix monkeypatch undo when deleting missing attrs/items with raising=False#14271

Open
kartikdp wants to merge 1 commit intopytest-dev:mainfrom
kartikdp:bugfix/monkeypatch-restore-notset
Open

Fix monkeypatch undo when deleting missing attrs/items with raising=False#14271
kartikdp wants to merge 1 commit intopytest-dev:mainfrom
kartikdp:bugfix/monkeypatch-restore-notset

Conversation

@kartikdp
Copy link

@kartikdp kartikdp commented Mar 7, 2026

Summary

Fixes a state-restoration bug in MonkeyPatch when deleting a missing attribute/item with raising=False.

Previously:

  • delattr(..., raising=False) and delitem(..., raising=False) did not record the missing state.
  • If the test then added that attribute/item, undo() would not reliably restore the original missing state.

This change records NOTSET for those delete calls, and makes undo() 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.py
  • uv run --with pre-commit pre-commit run --files src/_pytest/monkeypatch.py testing/test_monkeypatch.py changelog/14094.bugfix.rst

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Mar 7, 2026
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.

Monkeypatch.delitem/delattr does not keep track of non-existing properties

1 participant