Skip to content

Commit 93b3fee

Browse files
Merge pull request #6981 from RonnyPfannschmidt/deprecate-pytest.collect
deprecate the pytest.collect module
2 parents ce42938 + f1d51ba commit 93b3fee

File tree

6 files changed

+54
-30
lines changed

6 files changed

+54
-30
lines changed

changelog/6981.deprecation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate the ``pytest.collect`` module as it's just aliases into ``pytest``.

src/_pytest/compat.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -336,30 +336,6 @@ def safe_isclass(obj: object) -> bool:
336336
return False
337337

338338

339-
COLLECT_FAKEMODULE_ATTRIBUTES = (
340-
"Collector",
341-
"Module",
342-
"Function",
343-
"Instance",
344-
"Session",
345-
"Item",
346-
"Class",
347-
"File",
348-
"_fillfuncargs",
349-
)
350-
351-
352-
def _setup_collect_fakemodule() -> None:
353-
from types import ModuleType
354-
import pytest
355-
356-
# Types ignored because the module is created dynamically.
357-
pytest.collect = ModuleType("pytest.collect") # type: ignore
358-
pytest.collect.__all__ = [] # type: ignore # used for setns
359-
for attr_name in COLLECT_FAKEMODULE_ATTRIBUTES:
360-
setattr(pytest.collect, attr_name, getattr(pytest, attr_name)) # type: ignore
361-
362-
363339
class CaptureIO(io.TextIOWrapper):
364340
def __init__(self) -> None:
365341
super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True)

src/_pytest/deprecated.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656
"Please use collect_ignore in conftests or pytest_collection_modifyitems."
5757
)
5858

59+
PYTEST_COLLECT_MODULE = UnformattedWarning(
60+
PytestDeprecationWarning,
61+
"pytest.collect.{name} was moved to pytest.{name}\n"
62+
"Please update to the new name.",
63+
)
64+
5965

6066
TERMINALWRITER_WRITER = PytestDeprecationWarning(
6167
"The TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"

src/pytest/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"""
33
pytest: unit and functional testing with Python.
44
"""
5+
from . import collect
56
from _pytest import __version__
67
from _pytest.assertion import register_assert_rewrite
7-
from _pytest.compat import _setup_collect_fakemodule
88
from _pytest.config import cmdline
99
from _pytest.config import ExitCode
1010
from _pytest.config import hookimpl
@@ -46,7 +46,6 @@
4646
from _pytest.warning_types import PytestUnknownMarkWarning
4747
from _pytest.warning_types import PytestWarning
4848

49-
5049
set_trace = __pytestPDB.set_trace
5150

5251
__all__ = [
@@ -55,6 +54,7 @@
5554
"approx",
5655
"Class",
5756
"cmdline",
57+
"collect",
5858
"Collector",
5959
"deprecated_call",
6060
"exit",
@@ -93,7 +93,3 @@
9393
"xfail",
9494
"yield_fixture",
9595
]
96-
97-
98-
_setup_collect_fakemodule()
99-
del _setup_collect_fakemodule

src/pytest/collect.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
import warnings
3+
from types import ModuleType
4+
5+
import pytest
6+
from _pytest.deprecated import PYTEST_COLLECT_MODULE
7+
8+
9+
COLLECT_FAKEMODULE_ATTRIBUTES = [
10+
"Collector",
11+
"Module",
12+
"Function",
13+
"Instance",
14+
"Session",
15+
"Item",
16+
"Class",
17+
"File",
18+
"_fillfuncargs",
19+
]
20+
21+
22+
class FakeCollectModule(ModuleType):
23+
def __init__(self):
24+
super().__init__("pytest.collect")
25+
self.__all__ = list(COLLECT_FAKEMODULE_ATTRIBUTES)
26+
self.__pytest = pytest
27+
28+
def __dir__(self):
29+
return dir(super()) + self.__all__
30+
31+
def __getattr__(self, name):
32+
if name not in self.__all__:
33+
raise AttributeError(name)
34+
warnings.warn(PYTEST_COLLECT_MODULE.format(name=name), stacklevel=2)
35+
return getattr(pytest, name)
36+
37+
38+
sys.modules["pytest.collect"] = FakeCollectModule()

testing/deprecated_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def test():
2525
)
2626

2727

28+
@pytest.mark.parametrize("attribute", pytest.collect.__all__) # type: ignore
29+
# false positive due to dynamic attribute
30+
def test_pytest_collect_module_deprecated(attribute):
31+
with pytest.warns(DeprecationWarning, match=attribute):
32+
getattr(pytest.collect, attribute)
33+
34+
2835
def test_terminal_reporter_writer_attr(pytestconfig):
2936
"""Check that TerminalReporter._tw is also available as 'writer' (#2984)
3037
This attribute has been deprecated in 5.4.

0 commit comments

Comments
 (0)