Fix monkeypatch undo when deleting missing attrs/items with raising=False by kartikdp · Pull Request #14271 · pytest-dev/pytest

I am indeed -1 on this. Here's a trivial example:

import types


def test_attribute(monkeypatch):
    obj = types.SimpleNamespace()
    with monkeypatch.context() as mp:
        mp.delattr(obj, "attr", raising=False)
        obj.attr = 42
    assert hasattr(obj, "attr")

It seems natural to me to assume that the object still has the attribute after monkeypatch is done. After all, I never set the attribute through monkeypatch, so I really wouldn't expect monkeypatch to mess with it (as, with raising=False, it was a no-op). I would be fine if there was an opt-in way with an undo=True or something, that also opens up the opportunity to document this quirk in a nice way. Right now, with this PR, this seems like a footgun to me.