From 6f13d1b03b1e1af7def99505234075878407767d Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Fri, 6 Nov 2020 19:27:33 +0200 Subject: Export MonkeyPatch as pytest.MonkeyPatch We want to export `pytest.MonkeyPatch` for the purpose of type-annotating the `monkeypatch` fixture. For other fixtures we export in this way, we also make direct construction of them (e.g. `MonkeyPatch()`) private. But unlike the others, `MonkeyPatch` is also widely used directly already, mostly because the `monkeypatch` fixture only works in `function` scope (issue #363), but also in other cases. So making it private will be annoying and we don't offer a decent replacement yet. So, let's just make direct construction public & documented. --- changelog/8006.feature.rst | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 changelog/8006.feature.rst (limited to 'changelog') diff --git a/changelog/8006.feature.rst b/changelog/8006.feature.rst new file mode 100644 index 000000000..0203689ba --- /dev/null +++ b/changelog/8006.feature.rst @@ -0,0 +1,8 @@ +It is now possible to construct a :class:`~pytest.MonkeyPatch` object directly as ``pytest.MonkeyPatch()``, +in cases when the :fixture:`monkeypatch` fixture cannot be used. Previously some users imported it +from the private `_pytest.monkeypatch.MonkeyPatch` namespace. + +Additionally, :meth:`MonkeyPatch.context ` is now a classmethod, +and can be used as ``with MonkeyPatch.context() as mp: ...``. This is the recommended way to use +``MonkeyPatch`` directly, since unlike the ``monkeypatch`` fixture, an instance created directly +is not ``undo()``-ed automatically. -- cgit v1.2.3