Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecate the pytest.collect module #6981

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6981.deprecation.rst
@@ -0,0 +1 @@
Deprecate the ``pytest.collect`` module as it's just aliases into ``pytest``.
24 changes: 0 additions & 24 deletions src/_pytest/compat.py
Expand Up @@ -336,30 +336,6 @@ def safe_isclass(obj: object) -> bool:
return False


COLLECT_FAKEMODULE_ATTRIBUTES = (
"Collector",
"Module",
"Function",
"Instance",
"Session",
"Item",
"Class",
"File",
"_fillfuncargs",
)


def _setup_collect_fakemodule() -> None:
from types import ModuleType
import pytest

# Types ignored because the module is created dynamically.
pytest.collect = ModuleType("pytest.collect") # type: ignore
pytest.collect.__all__ = [] # type: ignore # used for setns
for attr_name in COLLECT_FAKEMODULE_ATTRIBUTES:
setattr(pytest.collect, attr_name, getattr(pytest, attr_name)) # type: ignore


class CaptureIO(io.TextIOWrapper):
def __init__(self) -> None:
super().__init__(io.BytesIO(), encoding="UTF-8", newline="", write_through=True)
Expand Down
6 changes: 6 additions & 0 deletions src/_pytest/deprecated.py
Expand Up @@ -56,6 +56,12 @@
"Please use collect_ignore in conftests or pytest_collection_modifyitems."
)

PYTEST_COLLECT_MODULE = UnformattedWarning(
PytestDeprecationWarning,
"pytest.collect.{name} was moved to pytest.{name}\n"
"Please update to the new name.",
)


TERMINALWRITER_WRITER = PytestDeprecationWarning(
"The TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"
Expand Down
8 changes: 2 additions & 6 deletions src/pytest/__init__.py
Expand Up @@ -2,9 +2,9 @@
"""
pytest: unit and functional testing with Python.
"""
from . import collect
from _pytest import __version__
from _pytest.assertion import register_assert_rewrite
from _pytest.compat import _setup_collect_fakemodule
from _pytest.config import cmdline
from _pytest.config import ExitCode
from _pytest.config import hookimpl
Expand Down Expand Up @@ -46,7 +46,6 @@
from _pytest.warning_types import PytestUnknownMarkWarning
from _pytest.warning_types import PytestWarning


set_trace = __pytestPDB.set_trace

__all__ = [
Expand All @@ -55,6 +54,7 @@
"approx",
"Class",
"cmdline",
"collect",
"Collector",
"deprecated_call",
"exit",
Expand Down Expand Up @@ -93,7 +93,3 @@
"xfail",
"yield_fixture",
]


_setup_collect_fakemodule()
del _setup_collect_fakemodule
38 changes: 38 additions & 0 deletions src/pytest/collect.py
@@ -0,0 +1,38 @@
import sys
import warnings
from types import ModuleType

import pytest
from _pytest.deprecated import PYTEST_COLLECT_MODULE


COLLECT_FAKEMODULE_ATTRIBUTES = [
"Collector",
"Module",
"Function",
"Instance",
"Session",
"Item",
"Class",
"File",
"_fillfuncargs",
]


class FakeCollectModule(ModuleType):
def __init__(self):
super().__init__("pytest.collect")
self.__all__ = list(COLLECT_FAKEMODULE_ATTRIBUTES)
self.__pytest = pytest

def __dir__(self):
return dir(super()) + self.__all__

def __getattr__(self, name):
if name not in self.__all__:
raise AttributeError(name)
warnings.warn(PYTEST_COLLECT_MODULE.format(name=name), stacklevel=2)
return getattr(pytest, name)


sys.modules["pytest.collect"] = FakeCollectModule()
7 changes: 7 additions & 0 deletions testing/deprecated_test.py
Expand Up @@ -25,6 +25,13 @@ def test():
)


@pytest.mark.parametrize("attribute", pytest.collect.__all__) # type: ignore
# false positive due to dynamic attribute
def test_pytest_collect_module_deprecated(attribute):
with pytest.warns(DeprecationWarning, match=attribute):
getattr(pytest.collect, attribute)


def test_terminal_reporter_writer_attr(pytestconfig):
"""Check that TerminalReporter._tw is also available as 'writer' (#2984)
This attribute has been deprecated in 5.4.
Expand Down