Skip to content

Commit 03832fa

Browse files
authored
Merge pull request #11326 from DetachHead/DetachHead-patch-1
use `if not TYPE_CHECKING` on `pytest.__getattr__` to prevent type checkers from using it
2 parents 8f36fd5 + cada6c1 commit 03832fa

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/pytest/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# PYTHON_ARGCOMPLETE_OK
22
"""pytest: unit and functional testing with Python."""
3+
from typing import TYPE_CHECKING
4+
35
from _pytest import __version__
46
from _pytest import version_tuple
57
from _pytest._code import ExceptionInfo
@@ -165,11 +167,12 @@
165167
"yield_fixture",
166168
]
167169

170+
if not TYPE_CHECKING:
168171

169-
def __getattr__(name: str) -> object:
170-
if name == "Instance":
171-
# The import emits a deprecation warning.
172-
from _pytest.python import Instance
172+
def __getattr__(name: str) -> object:
173+
if name == "Instance":
174+
# The import emits a deprecation warning.
175+
from _pytest.python import Instance
173176

174-
return Instance
175-
raise AttributeError(f"module {__name__} has no attribute {name}")
177+
return Instance
178+
raise AttributeError(f"module {__name__} has no attribute {name}")

testing/deprecated_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def test_importing_instance_is_deprecated(pytester: Pytester) -> None:
272272
pytest.PytestDeprecationWarning,
273273
match=re.escape("The pytest.Instance collector type is deprecated"),
274274
):
275-
pytest.Instance
275+
pytest.Instance # type:ignore[attr-defined]
276276

277277
with pytest.warns(
278278
pytest.PytestDeprecationWarning,

0 commit comments

Comments
 (0)