Skip to content

[ai clone] Fix monkeypatch recording failed mutations - #14927

Closed
dhootraghav wants to merge 2 commits into
pytest-dev:mainfrom
dhootraghav:fix/monkeypatch-stale-undo
Closed

[ai clone] Fix monkeypatch recording failed mutations#14927
dhootraghav wants to merge 2 commits into
pytest-dev:mainfrom
dhootraghav:fix/monkeypatch-stale-undo

Conversation

@dhootraghav

Copy link
Copy Markdown

Summary

Fix MonkeyPatch methods recording undo state before the underlying mutation succeeds.

Previously, delattr, setitem, and delitem recorded their undo information before performing the mutation. If the mutation raised an exception, the failed operation was still recorded and later replayed by undo(), which could raise a second unexpected exception.

This change records the undo state only after the mutation succeeds.

Changes

  • Record delattr undo state after successful deletion.
  • Record setitem undo state after successful assignment.
  • Record delitem undo state after successful deletion.
  • Add regression tests covering failed mutations for all three operations.

Testing

python -m pytest testing/test_monkeypatch.py -q
37 passed, 2 skipped

also verified git diff --check with no errors

### 4. Before clicking **Create pull request**

Look at the **Files changed** tab.

You should see only:

```text
src/_pytest/monkeypatch.py
testing/test_monkeypatch.py

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Aug 23, 2026
@RonnyPfannschmidt RonnyPfannschmidt changed the title Fix monkeypatch recording failed mutations [ai clone] Fix monkeypatch recording failed mutations Sep 7, 2026
@RonnyPfannschmidt

Copy link
Copy Markdown
Member

identical to #14910

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.

2 participants