Skip to content

Commit 30a7a68

Browse files
committed
Remove dynamic setup of pytest.collect fake module
Make it a real module instead. This was initially added in 9b58d6e (#2315), when removing support for `pytest_namespace`. Not sure if it ever worked "properly", but currently `import pytest.collect` and `from pytest.collect import File` do not work (but with this patch). This is only relevant for (backward) compatibility, but the more it appears to make sense to move it into a lazily loaded, separate file.
1 parent bfd0d18 commit 30a7a68

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

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/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55
from _pytest import __version__
66
from _pytest.assertion import register_assert_rewrite
7-
from _pytest.compat import _setup_collect_fakemodule
87
from _pytest.config import cmdline
98
from _pytest.config import ExitCode
109
from _pytest.config import hookimpl
@@ -93,7 +92,3 @@
9392
"xfail",
9493
"yield_fixture",
9594
]
96-
97-
98-
_setup_collect_fakemodule()
99-
del _setup_collect_fakemodule

src/pytest/collect.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Fake module for backward compatibility."""
2+
from . import _fillfuncargs # noqa: F401
3+
from . import Class # noqa: F401
4+
from . import Collector # noqa: F401
5+
from . import File # noqa: F401
6+
from . import Function # noqa: F401
7+
from . import Instance # noqa: F401
8+
from . import Item # noqa: F401
9+
from . import Module # noqa: F401
10+
from . import Session # noqa: F401

0 commit comments

Comments
 (0)