()
| 24 | |
| 25 | |
| 26 | def test_setattr() -> None: |
| 27 | class A: |
| 28 | x = 1 |
| 29 | |
| 30 | monkeypatch = MonkeyPatch() |
| 31 | with pytest.raises(AttributeError): |
| 32 | monkeypatch.setattr(A, "notexists", 2) |
| 33 | monkeypatch.setattr(A, "y", 2, raising=False) |
| 34 | assert A.y == 2 # type: ignore |
| 35 | monkeypatch.undo() |
| 36 | assert not hasattr(A, "y") |
| 37 | |
| 38 | monkeypatch = MonkeyPatch() |
| 39 | monkeypatch.setattr(A, "x", 2) |
| 40 | assert A.x == 2 |
| 41 | monkeypatch.setattr(A, "x", 3) |
| 42 | assert A.x == 3 |
| 43 | monkeypatch.undo() |
| 44 | assert A.x == 1 |
| 45 | |
| 46 | A.x = 5 |
| 47 | monkeypatch.undo() # double-undo makes no modification |
| 48 | assert A.x == 5 |
| 49 | |
| 50 | with pytest.raises(TypeError): |
| 51 | monkeypatch.setattr(A, "y") # type: ignore[call-overload] |
| 52 | |
| 53 | |
| 54 | class TestSetattrWithImportPath: |
nothing calls this directly
no test coverage detected