Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/14094.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``MonkeyPatch.delattr(..., raising=False)`` and ``MonkeyPatch.delitem(..., raising=False)`` so ``undo()`` restores the original missing state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a bug-fix, seems like an improvement: now delattr will always delete the attribute at the end of the test.

This is a behavior change which I don't think can cause problems later, as it depends on the user explicitly calling monkeydelattr.delattr(..., raising=False).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@The-Compiler you commented on the issue; are you OK with this?

7 changes: 6 additions & 1 deletion src/_pytest/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def delattr(
if not hasattr(target, name):
if raising:
raise AttributeError(name)
self._setattr.append((target, name, NOTSET))
else:
oldval = getattr(target, name, NOTSET)
# Avoid class descriptors like staticmethod/classmethod.
Expand All @@ -300,6 +301,7 @@ def delitem(self, dic: Mapping[K, V], name: K, raising: bool = True) -> None:
if name not in dic:
if raising:
raise KeyError(name)
self._setitem.append((dic, name, NOTSET))
else:
self._setitem.append((dic, name, dic.get(name, NOTSET)))
# Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict
Expand Down Expand Up @@ -408,7 +410,10 @@ def undo(self) -> None:
if value is not NOTSET:
setattr(obj, name, value)
else:
delattr(obj, name)
try:
delattr(obj, name)
except AttributeError:
pass # Was already deleted, so we have the desired state.
self._setattr[:] = []
for dictionary, key, value in reversed(self._setitem):
if value is NOTSET:
Expand Down
22 changes: 22 additions & 0 deletions testing/test_monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ class A:
assert A.x == 1


def test_delattr_non_existing_with_raising_false() -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test already passes on main, so it is not reflecting/reproducing the issue.

Did you intend to use setattr directly to set x (instead of monkeypatch.setattr).

class A:
pass

monkeypatch = MonkeyPatch()
monkeypatch.delattr(A, "x", raising=False)
monkeypatch.setattr(A, "x", 1, raising=False)
assert A.x == 1 # type: ignore[attr-defined]
monkeypatch.undo()
assert not hasattr(A, "x")


def test_setitem() -> None:
d = {"x": 1}
monkeypatch = MonkeyPatch()
Expand Down Expand Up @@ -177,6 +189,16 @@ def test_delitem() -> None:
assert d == {"hello": "world", "x": 1}


def test_delitem_non_existing_with_raising_false() -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as the other test.

d: dict[str, object] = {}
monkeypatch = MonkeyPatch()
monkeypatch.delitem(d, "x", raising=False)
monkeypatch.setitem(d, "x", 1)
assert d["x"] == 1
monkeypatch.undo()
assert "x" not in d


def test_setenv() -> None:
monkeypatch = MonkeyPatch()
with pytest.warns(pytest.PytestWarning):
Expand Down