Undo previous changes. This call consumes the undo stack. Calling it a second time has no effect unless you do more monkeypatching after the undo call. There is generally no need to call `undo()`, since it is called automatically during tear-down. .. note::
(self)
| 387 | os.chdir(path) |
| 388 | |
| 389 | def undo(self) -> None: |
| 390 | """Undo previous changes. |
| 391 | |
| 392 | This call consumes the undo stack. Calling it a second time has no |
| 393 | effect unless you do more monkeypatching after the undo call. |
| 394 | |
| 395 | There is generally no need to call `undo()`, since it is |
| 396 | called automatically during tear-down. |
| 397 | |
| 398 | .. note:: |
| 399 | The same `monkeypatch` fixture is used across a |
| 400 | single test function invocation. If `monkeypatch` is used both by |
| 401 | the test function itself and one of the test fixtures, |
| 402 | calling `undo()` will undo all of the changes made in |
| 403 | both functions. |
| 404 | |
| 405 | Prefer to use :meth:`context() <pytest.MonkeyPatch.context>` instead. |
| 406 | """ |
| 407 | for obj, name, value in reversed(self._setattr): |
| 408 | if value is not NOTSET: |
| 409 | setattr(obj, name, value) |
| 410 | else: |
| 411 | delattr(obj, name) |
| 412 | self._setattr[:] = [] |
| 413 | for dictionary, key, value in reversed(self._setitem): |
| 414 | if value is NOTSET: |
| 415 | try: |
| 416 | # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict |
| 417 | del dictionary[key] # type: ignore[attr-defined] |
| 418 | except KeyError: |
| 419 | pass # Was already deleted, so we have the desired state. |
| 420 | else: |
| 421 | # Not all Mapping types support indexing, but MutableMapping doesn't support TypedDict |
| 422 | dictionary[key] = value # type: ignore[index] |
| 423 | self._setitem[:] = [] |
| 424 | if self._savesyspath is not None: |
| 425 | sys.path[:] = self._savesyspath |
| 426 | self._savesyspath = None |
| 427 | |
| 428 | if self._cwd is not None: |
| 429 | os.chdir(self._cwd) |
| 430 | self._cwd = None |