Backtestspytestrun-20261106-234554#14909
MonkeyPatch records stale undo entries when delattr/setitem/delitem fails
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
The source changes in the two patches are byte-identical (same blob hashes d6db72455..9ae9e2ba9): both capture the old value, perform the mutation, and append the undo entry only on success, for delattr, setitem and delitem. Independent tests confirm both fix every scenario, so the decision rests on tests, scope and craft. Patch A adds three standalone MonkeyPatch unit tests that assert the target is unchanged both before and after undo(), plus a pytester-based test that reproduces the issue's actual reported symptom: the monkeypatch fixture teardown no longer errors after a failed delattr/setitem/delitem. That end-to-end test is the most faithful regression check for the issue as described. Patch B's three failure tests are equivalent in strength to A's unit tests (they would fail on base because the explicit undo() raises), but B adds a fourth test that merely re-exercises successful setitem/delitem/undo, which existing test_setitem/test_delitem already cover; its docstring explicitly justifies it as a codecov patch-coverage workaround, which is noise unrelated to the bug. B's placement at module level next to test_delitem is slightly more natural than A's placement inside TestSetattrWithImportPath, though A's location sits next to the analogous setattr-failure test from the prior fix, so it is defensible. Changelogs are both adequate. Overall A's test suite is more targeted at the reported teardown behavior and contains no filler, giving it a modest edge.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
5m 46s
Wall-clock time in the container
Full test suite
No new failures
2,154 passed · 31 skipped · 2,185 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
84vs 61 merged
Changed lines, ours vs merged
The issue
View on GitHubWhile looking at #14161 and its fix in #14209, I noticed that the same undo-ordering problem still exists in MonkeyPatch.delattr, MonkeyPatch.setitem, and MonkeyPatch.delitem.
These methods register an undo entry before attempting the mutation. If the mutation raises, the target remains unchanged, but undo() later tries to restore a change that never happened.
Minimal reproduction:
from types import MappingProxyType
import pytest
def test_failed_delattr(monkeypatch):
class A:
__slots__ = ()
x = 1
a = A()
with pytest.raises(AttributeError):
monkeypatch.delattr(a, "x")
assert a.x == 1
def test_failed_setitem(monkeypatch):
mapping = MappingProxyType({"x": 1})
with pytest.raises(TypeError):
monkeypatch.setitem(mapping, "x", 2)
assert mapping["x"] == 1
def test_failed_delitem(monkeypatch):
mapping = MappingProxyType({"x": 1})
with pytest.raises(TypeError):
monkeypatch.delitem(mapping, "x")
assert mapping["x"] == 1
On current main, the original exceptions are caught and all three targets remain unchanged. However, fixture teardown calls undo() and produces another error for each test because the failed operations left stale undo entries.
I think the fix would be to follow the ordering already used by MonkeyPatch.setattr after #14209:
- Save the original value.
- Attempt the mutation.
- Register the undo entry only after the mutation succeeds.
Successful operations would keep their existing behavior.
I reproduced this locally and have a small fix with regression tests ready. If maintainers agree that this is a bug and that the proposed approach is appropriate, I would be happy to submit a PR. Could this issue be assigned to me?
Environment:
- pytest
9.2.0.dev204+gc0ab40c93 - Python 3.12.13
- Red Hat Enterprise Linux 9.8
The two patches
changelog/14909.bugfix.rstnew file mode 100644index 000000000..84494b1a3@@ -0,0 +1 @@+:meth:`MonkeyPatch.delattr <pytest.MonkeyPatch.delattr>`, :meth:`MonkeyPatch.setitem <pytest.MonkeyPatch.setitem>` and :meth:`MonkeyPatch.delitem <pytest.MonkeyPatch.delitem>` no longer record an undo entry when the underlying operation fails, which previously caused errors during ``monkeypatch`` fixture teardown.src/_pytest/monkeypatch.pyindex d6db72455..9ae9e2ba9 100644@@ -282,14 +282,15 @@ class MonkeyPatch: # Avoid class descriptors like staticmethod/classmethod. if inspect.isclass(target): oldval = target.__dict__.get(name, NOTSET)- self._setattr.append((target, name, oldval)) delattr(target, name)+ self._setattr.append((target, name, oldval)) def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None: """Set dictionary entry ``name`` to value."""- self._setitem.append((dic, name, dic.get(name, NOTSET)))+ oldval = dic.get(name, NOTSET) # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict dic[name] = value # type: ignore[index]+ self._setitem.append((dic, name, oldval)) def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None: """Delete ``name`` from dict.@@ -301,9 +302,10 @@ class MonkeyPatch: if raising: raise KeyError(name) else:- self._setitem.append((dic, name, dic.get(name, NOTSET)))+ oldval = dic.get(name, NOTSET) # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict del dic[name] # type: ignore[attr-defined]+ self._setitem.append((dic, name, oldval)) def setenv(self, name: str, value: str, prepend: str | None = None) -> None: """Set environment variable ``name`` to ``value``.testing/test_monkeypatch.pyindex 04b16a1e8..5a487cd94 100644@@ -7,6 +7,7 @@ from pathlib import Path import re import sys import textwrap+from types import MappingProxyType import warnings from _pytest.monkeypatch import MonkeyPatch@@ -106,6 +107,80 @@ class TestSetattrWithImportPath: # undo() must not raise — no entry should be on the undo stack. monkeypatch.undo() + def test_delattr_failure_does_not_corrupt_undo(self) -> None:+ """A failed delattr() must not leave a stale undo entry."""++ class A:+ __slots__ = ()+ x = 1++ a = A()+ mp = MonkeyPatch()+ with pytest.raises(AttributeError):+ mp.delattr(a, "x")+ assert a.x == 1+ # undo() must not try to restore an attribute that was never deleted.+ mp.undo()+ assert a.x == 1++ def test_setitem_failure_does_not_corrupt_undo(self) -> None:+ """A failed setitem() must not leave a stale undo entry."""+ mapping = MappingProxyType({"x": 1})+ mp = MonkeyPatch()+ with pytest.raises(TypeError):+ mp.setitem(mapping, "x", 2)+ assert mapping["x"] == 1+ mp.undo()+ assert mapping["x"] == 1++ def test_delitem_failure_does_not_corrupt_undo(self) -> None:+ """A failed delitem() must not leave a stale undo entry."""+ mapping = MappingProxyType({"x": 1})+ mp = MonkeyPatch()+ with pytest.raises(TypeError):+ mp.delitem(mapping, "x")+ assert mapping["x"] == 1+ mp.undo()+ assert mapping["x"] == 1++ def test_failed_patches_do_not_break_teardown(self, pytester: Pytester) -> None:+ """Failed delattr/setitem/delitem do not cause errors in the+ monkeypatch fixture teardown."""+ pytester.makepyfile(+ """+ from types import MappingProxyType++ import pytest+++ def test_failed_delattr(monkeypatch):+ class A:+ __slots__ = ()+ x = 1++ a = A()+ with pytest.raises(AttributeError):+ monkeypatch.delattr(a, "x")+ assert a.x == 1+++ def test_failed_setitem(monkeypatch):+ mapping = MappingProxyType({"x": 1})+ with pytest.raises(TypeError):+ monkeypatch.setitem(mapping, "x", 2)+ assert mapping["x"] == 1+++ def test_failed_delitem(monkeypatch):+ mapping = MappingProxyType({"x": 1})+ with pytest.raises(TypeError):+ monkeypatch.delitem(mapping, "x")+ assert mapping["x"] == 1+ """+ )+ result = pytester.runpytest()+ result.assert_outcomes(passed=3)+ def test_delattr(self, monkeypatch: MonkeyPatch) -> None: with monkeypatch.context() as mp: mp.delattr("os.path.abspath")
changelog/14909.bugfix.rstnew file mode 100644index 000000000..32dced313@@ -0,0 +1 @@+Fixed :meth:`MonkeyPatch.delattr <pytest.MonkeyPatch.delattr>`, :meth:`MonkeyPatch.setitem <pytest.MonkeyPatch.setitem>` and :meth:`MonkeyPatch.delitem <pytest.MonkeyPatch.delitem>` recording a stale undo entry when the underlying mutation failed, which could cause spurious errors during teardown. The undo entry is now recorded only after the mutation succeeds, matching the behavior of :meth:`MonkeyPatch.setattr <pytest.MonkeyPatch.setattr>`.src/_pytest/monkeypatch.pyindex d6db72455..9ae9e2ba9 100644@@ -282,14 +282,15 @@ class MonkeyPatch: # Avoid class descriptors like staticmethod/classmethod. if inspect.isclass(target): oldval = target.__dict__.get(name, NOTSET)- self._setattr.append((target, name, oldval)) delattr(target, name)+ self._setattr.append((target, name, oldval)) def setitem(self, dic: Mapping[K, V], name: K, value: V) -> None: """Set dictionary entry ``name`` to value."""- self._setitem.append((dic, name, dic.get(name, NOTSET)))+ oldval = dic.get(name, NOTSET) # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict dic[name] = value # type: ignore[index]+ self._setitem.append((dic, name, oldval)) def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None: """Delete ``name`` from dict.@@ -301,9 +302,10 @@ class MonkeyPatch: if raising: raise KeyError(name) else:- self._setitem.append((dic, name, dic.get(name, NOTSET)))+ oldval = dic.get(name, NOTSET) # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict del dic[name] # type: ignore[attr-defined]+ self._setitem.append((dic, name, oldval)) def setenv(self, name: str, value: str, prepend: str | None = None) -> None: """Set environment variable ``name`` to ``value``.testing/test_monkeypatch.pyindex 04b16a1e8..63e145c66 100644@@ -7,6 +7,7 @@ from pathlib import Path import re import sys import textwrap+from types import MappingProxyType import warnings from _pytest.monkeypatch import MonkeyPatch@@ -196,6 +197,57 @@ def test_delitem() -> None: assert d == {"hello": "world", "x": 1} +def test_failed_delattr(monkeypatch: MonkeyPatch) -> None:+ """If delattr() raises, no stale undo entry should be recorded (#14909)."""++ class A:+ __slots__ = ()+ x = 1++ a = A()+ with pytest.raises(AttributeError):+ monkeypatch.delattr(a, "x")+ assert a.x == 1+ # undo() must not raise — no entry should be on the undo stack.+ monkeypatch.undo()+++def test_failed_setitem(monkeypatch: MonkeyPatch) -> None:+ """If setitem() raises, no stale undo entry should be recorded (#14909)."""+ mapping = MappingProxyType({"x": 1})+ with pytest.raises(TypeError):+ monkeypatch.setitem(mapping, "x", 2)+ assert mapping["x"] == 1+ # undo() must not raise — no entry should be on the undo stack.+ monkeypatch.undo()+++def test_failed_delitem(monkeypatch: MonkeyPatch) -> None:+ """If delitem() raises, no stale undo entry should be recorded (#14909)."""+ mapping = MappingProxyType({"x": 1})+ with pytest.raises(TypeError):+ monkeypatch.delitem(mapping, "x")+ assert mapping["x"] == 1+ # undo() must not raise — no entry should be on the undo stack.+ monkeypatch.undo()+++def test_setitem_delitem_oldval_captured_before_mutation(+ monkeypatch: MonkeyPatch,+) -> None:+ """For setitem/delitem the old value must be captured *before* the+ mutation so undo() restores the correct value (#14909). This case+ exercises both the capture-before line and the append-after line in+ the success path (no exception), so codecov patch coverage for the+ new lines stays 100% even when the surrounding pytest suite changes.+ """+ inner: dict[str, int] = {"x": 1, "y": 2}+ monkeypatch.setitem(inner, "x", 99)+ monkeypatch.delitem(inner, "y")+ monkeypatch.undo()+ assert inner == {"x": 1, "y": 2}++ def test_setenv() -> None: monkeypatch = MonkeyPatch() with pytest.warns(pytest.PytestWarning):