-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix monkeypatch undo when deleting missing attrs/items with raising=False #14271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,18 @@ class A: | |
| assert A.x == 1 | ||
|
|
||
|
|
||
| def test_delattr_non_existing_with_raising_false() -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test already passes on Did you intend to use |
||
| 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() | ||
|
|
@@ -177,6 +189,16 @@ def test_delitem() -> None: | |
| assert d == {"hello": "world", "x": 1} | ||
|
|
||
|
|
||
| def test_delitem_non_existing_with_raising_false() -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
|
||
There was a problem hiding this comment.
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
delattrwill 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).There was a problem hiding this comment.
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?