From 20e7a5bcd7270e3de0bb9b5679720698b797d148 Mon Sep 17 00:00:00 2001 From: Andy Dirnberger Date: Mon, 6 Jul 2020 09:41:10 -0400 Subject: [PATCH] Publish MonkeyPatch for typing This builds on the work done in 2bcad38fbd1fb4022e2dc261817b9918cfbda43e to add `MonkeyPatch` to pytest's public API. This will allow users of pytest to annotation their tests like def test_some_code(monkeypatch: MonkeyPatch) -> None: ... To make this possible, the `monkeypatch` module can't import `pytest` anymore. Instead it will import `PytestWarning` directly from `_pytest.warning_types`. --- src/_pytest/monkeypatch.py | 4 ++-- src/pytest/__init__.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/_pytest/monkeypatch.py b/src/_pytest/monkeypatch.py index 2e5cca52628..e360c9d334e 100644 --- a/src/_pytest/monkeypatch.py +++ b/src/_pytest/monkeypatch.py @@ -13,10 +13,10 @@ from typing import TypeVar from typing import Union -import pytest from _pytest.compat import overload from _pytest.fixtures import fixture from _pytest.pathlib import Path +from _pytest.warning_types import PytestWarning RE_IMPORT_ERROR_NAME = re.compile(r"^No module named (.*)$") @@ -272,7 +272,7 @@ def setenv(self, name: str, value: str, prepend: Optional[str] = None) -> None: and prepend the ``value`` adjoined with the ``prepend`` character.""" if not isinstance(value, str): warnings.warn( - pytest.PytestWarning( + PytestWarning( "Value of environment variable {name} type should be str, but got " "{value!r} (type: {type}); converted to str implicitly".format( name=name, value=value, type=type(value).__name__ diff --git a/src/pytest/__init__.py b/src/pytest/__init__.py index 64d6d1f23ee..a7bea9be15f 100644 --- a/src/pytest/__init__.py +++ b/src/pytest/__init__.py @@ -21,6 +21,7 @@ from _pytest.main import Session from _pytest.mark import MARK_GEN as mark from _pytest.mark import param +from _pytest.monkeypatch import MonkeyPatch from _pytest.nodes import Collector from _pytest.nodes import File from _pytest.nodes import Item @@ -76,6 +77,7 @@ "main", "mark", "Module", + "MonkeyPatch", "Package", "param", "PytestAssertRewriteWarning",