Skip to content

Commit

Permalink
Remove dynamic setup of pytest.collect fake module
Browse files Browse the repository at this point in the history
Make it a real module instead.

This was initially added in 9b58d6e
(pytest-dev#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.
  • Loading branch information
blueyed committed Mar 12, 2020
1 parent 347cc14 commit 4a7e526
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
24 changes: 0 additions & 24 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
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
5 changes: 0 additions & 5 deletions src/pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
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 @@ -90,7 +89,3 @@
"xfail",
"yield_fixture",
]


_setup_collect_fakemodule()
del _setup_collect_fakemodule
10 changes: 10 additions & 0 deletions src/pytest/collect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Fake module for backward compatibility."""
from . import _fillfuncargs # noqa: F401
from . import Class # noqa: F401
from . import Collector # noqa: F401
from . import File # noqa: F401
from . import Function # noqa: F401
from . import Instance # noqa: F401
from . import Item # noqa: F401
from . import Module # noqa: F401
from . import Session # noqa: F401

0 comments on commit 4a7e526

Please sign in to comment.