From c0d525e44cc5ca35d7abbd7024f6d999955165fd Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Mon, 24 May 2021 12:16:16 +0300 Subject: [PATCH] config: expose PytestPluginManager for typing purposes This type is used in hooks and transitively through `Config`. --- changelog/7469.feature.rst | 1 + doc/en/reference/reference.rst | 2 +- src/_pytest/config/__init__.py | 7 ++++++- src/_pytest/hookspec.py | 10 +++++----- src/pytest/__init__.py | 2 ++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/changelog/7469.feature.rst b/changelog/7469.feature.rst index 8d186c92d..02c936ec6 100644 --- a/changelog/7469.feature.rst +++ b/changelog/7469.feature.rst @@ -8,6 +8,7 @@ The newly-exported types are: - ``pytest.MarkGenerator`` for the :class:`pytest.mark ` singleton. - ``pytest.Metafunc`` for the :class:`metafunc ` argument to the :func:`pytest_generate_tests ` hook. - ``pytest.CallInfo`` for the :class:`CallInfo ` type passed to various hooks. +- ``pytest.PytestPluginManager`` for :class:`PytestPluginManager `. - ``pytest.ExceptionInfo`` for the :class:`ExceptionInfo ` type returned from :func:`pytest.raises` and passed to various hooks. Constructing them directly is not supported; they are only meant for use in type annotations. diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 279ceb76d..67fb00d1c 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -896,7 +896,7 @@ Parser PytestPluginManager ~~~~~~~~~~~~~~~~~~~ -.. autoclass:: _pytest.config.PytestPluginManager() +.. autoclass:: pytest.PytestPluginManager() :members: :undoc-members: :inherited-members: diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index f24f7d5d6..6531a3161 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -290,7 +290,7 @@ def get_config( def get_plugin_manager() -> "PytestPluginManager": """Obtain a new instance of the - :py:class:`_pytest.config.PytestPluginManager`, with default plugins + :py:class:`pytest.PytestPluginManager`, with default plugins already loaded. This function can be used by integration with other tools, like hooking @@ -632,6 +632,7 @@ class PytestPluginManager(PluginManager): def consider_preparse( self, args: Sequence[str], *, exclude_only: bool = False ) -> None: + """:meta private:""" i = 0 n = len(args) while i < n: @@ -653,6 +654,7 @@ class PytestPluginManager(PluginManager): self.consider_pluginarg(parg) def consider_pluginarg(self, arg: str) -> None: + """:meta private:""" if arg.startswith("no:"): name = arg[3:] if name in essential_plugins: @@ -678,12 +680,15 @@ class PytestPluginManager(PluginManager): self.import_plugin(arg, consider_entry_points=True) def consider_conftest(self, conftestmodule: types.ModuleType) -> None: + """:meta private:""" self.register(conftestmodule, name=conftestmodule.__file__) def consider_env(self) -> None: + """:meta private:""" self._import_plugin_specs(os.environ.get("PYTEST_PLUGINS")) def consider_module(self, mod: types.ModuleType) -> None: + """:meta private:""" self._import_plugin_specs(getattr(mod, "pytest_plugins", [])) def _import_plugin_specs( diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index 6524356ab..bb8502fa8 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -56,7 +56,7 @@ def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None: """Called at plugin registration time to allow adding new hooks via a call to ``pluginmanager.add_hookspecs(module_or_class, prefix)``. - :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager. + :param pytest.PytestPluginManager pluginmanager: The pytest plugin manager. .. note:: This hook is incompatible with ``hookwrapper=True``. @@ -70,7 +70,7 @@ def pytest_plugin_registered( """A new pytest plugin got registered. :param plugin: The plugin module or instance. - :param _pytest.config.PytestPluginManager manager: pytest plugin manager. + :param pytest.PytestPluginManager manager: pytest plugin manager. .. note:: This hook is incompatible with ``hookwrapper=True``. @@ -94,8 +94,8 @@ def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") -> To add ini-file values call :py:func:`parser.addini(...) <_pytest.config.argparsing.Parser.addini>`. - :param _pytest.config.PytestPluginManager pluginmanager: - pytest plugin manager, which can be used to install :py:func:`hookspec`'s + :param pytest.PytestPluginManager pluginmanager: + The pytest plugin manager, which can be used to install :py:func:`hookspec`'s or :py:func:`hookimpl`'s and allow one plugin to call another plugin's hooks to change how command line options are added. @@ -152,7 +152,7 @@ def pytest_cmdline_parse( ``plugins`` arg when using `pytest.main`_ to perform an in-process test run. - :param _pytest.config.PytestPluginManager pluginmanager: Pytest plugin manager. + :param pytest.PytestPluginManager pluginmanager: The pytest plugin manager. :param List[str] args: List of arguments passed on the command line. """ diff --git a/src/pytest/__init__.py b/src/pytest/__init__.py index ca28ee2c8..6e2f5e514 100644 --- a/src/pytest/__init__.py +++ b/src/pytest/__init__.py @@ -13,6 +13,7 @@ from _pytest.config import ExitCode from _pytest.config import hookimpl from _pytest.config import hookspec from _pytest.config import main +from _pytest.config import PytestPluginManager from _pytest.config import UsageError from _pytest.debugging import pytestPDB as __pytestPDB from _pytest.fixtures import _fillfuncargs @@ -114,6 +115,7 @@ __all__ = [ "PytestDeprecationWarning", "PytestExperimentalApiWarning", "Pytester", + "PytestPluginManager", "PytestUnhandledCoroutineWarning", "PytestUnhandledThreadExceptionWarning", "PytestUnknownMarkWarning",