From fc653d0d12fb43aa7fa843cb801d33854e1033b6 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Mon, 21 Aug 2023 18:00:28 +1000 Subject: [PATCH 1/3] use `if not TYPE_CHECKING` on `pytest.__getattr__` to prevent type checkers from using it --- src/pytest/__init__.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/pytest/__init__.py b/src/pytest/__init__.py index 831ede1fa..8d7cf520b 100644 --- a/src/pytest/__init__.py +++ b/src/pytest/__init__.py @@ -1,5 +1,7 @@ # PYTHON_ARGCOMPLETE_OK """pytest: unit and functional testing with Python.""" +from typing import TYPE_CHECKING + from _pytest import __version__ from _pytest import version_tuple from _pytest._code import ExceptionInfo @@ -165,11 +167,12 @@ __all__ = [ "yield_fixture", ] +if not TYPE_CHECKING: -def __getattr__(name: str) -> object: - if name == "Instance": - # The import emits a deprecation warning. - from _pytest.python import Instance - - return Instance - raise AttributeError(f"module {__name__} has no attribute {name}") + def __getattr__(name: str) -> object: + if name == "Instance": + # The import emits a deprecation warning. + from _pytest.python import Instance + + return Instance + raise AttributeError(f"module {__name__} has no attribute {name}") From dd5ae0c3b88058c4488fe1411c53e874e79bd7b2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 21 Aug 2023 08:03:06 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/pytest/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest/__init__.py b/src/pytest/__init__.py index 8d7cf520b..0aa496a2f 100644 --- a/src/pytest/__init__.py +++ b/src/pytest/__init__.py @@ -173,6 +173,6 @@ if not TYPE_CHECKING: if name == "Instance": # The import emits a deprecation warning. from _pytest.python import Instance - + return Instance raise AttributeError(f"module {__name__} has no attribute {name}") From cada6c105a7f9e22d5519c0e9355cc44df2a58b8 Mon Sep 17 00:00:00 2001 From: DetachHead <57028336+DetachHead@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:57:59 +1000 Subject: [PATCH 3/3] ignore mypy error on deprecated `Instance` usage --- testing/deprecated_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/deprecated_test.py b/testing/deprecated_test.py index f4197a1f6..08e193b5c 100644 --- a/testing/deprecated_test.py +++ b/testing/deprecated_test.py @@ -272,7 +272,7 @@ def test_importing_instance_is_deprecated(pytester: Pytester) -> None: pytest.PytestDeprecationWarning, match=re.escape("The pytest.Instance collector type is deprecated"), ): - pytest.Instance + pytest.Instance # type:ignore[attr-defined] with pytest.warns( pytest.PytestDeprecationWarning,