config: expose Config for typing purposes

This type is used in hooks and fixtures.

The constructor is publicly documented so is not marked private.
This commit is contained in:
Ran Benita 2021-05-24 11:45:07 +03:00
parent c198a7a67e
commit 88d84a5791
11 changed files with 37 additions and 32 deletions

View File

@ -2,6 +2,7 @@ The types of objects used in pytest's API are now exported so they may be used i
The newly-exported types are: The newly-exported types are:
- ``pytest.Config`` for :class:`Config <pytest.Config>`.
- ``pytest.Mark`` for :class:`marks <pytest.Mark>`. - ``pytest.Mark`` for :class:`marks <pytest.Mark>`.
- ``pytest.MarkDecorator`` for :class:`mark decorators <pytest.MarkDecorator>`. - ``pytest.MarkDecorator`` for :class:`mark decorators <pytest.MarkDecorator>`.
- ``pytest.MarkGenerator`` for the :class:`pytest.mark <pytest.MarkGenerator>` singleton. - ``pytest.MarkGenerator`` for the :class:`pytest.mark <pytest.MarkGenerator>` singleton.

View File

@ -61,7 +61,7 @@ For information about fixtures, see :ref:`fixtures`. To see a complete list of a
namespace of doctests. namespace of doctests.
pytestconfig [session scope] pytestconfig [session scope]
Session-scoped fixture that returns the :class:`_pytest.config.Config` object. Session-scoped fixture that returns the :class:`pytest.Config` object.
Example:: Example::

View File

@ -179,12 +179,12 @@ Files will only be matched for configuration if:
The files are considered in the order above. Options from multiple ``configfiles`` candidates The files are considered in the order above. Options from multiple ``configfiles`` candidates
are never merged - the first match wins. are never merged - the first match wins.
The internal :class:`Config <_pytest.config.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture) The :class:`Config <pytest.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture)
will subsequently carry these attributes: will subsequently carry these attributes:
- :attr:`config.rootpath <_pytest.config.Config.rootpath>`: the determined root directory, guaranteed to exist. - :attr:`config.rootpath <pytest.Config.rootpath>`: the determined root directory, guaranteed to exist.
- :attr:`config.inipath <_pytest.config.Config.inipath>`: the determined ``configfile``, may be ``None`` - :attr:`config.inipath <pytest.Config.inipath>`: the determined ``configfile``, may be ``None``
(it is named ``inipath`` for historical reasons). (it is named ``inipath`` for historical reasons).
.. versionadded:: 6.1 .. versionadded:: 6.1

View File

@ -787,7 +787,7 @@ CollectReport
Config Config
~~~~~~ ~~~~~~
.. autoclass:: _pytest.config.Config() .. autoclass:: pytest.Config()
:members: :members:
ExceptionInfo ExceptionInfo

View File

@ -841,6 +841,7 @@ class Config:
"""Access to configuration values, pluginmanager and plugin hooks. """Access to configuration values, pluginmanager and plugin hooks.
:param PytestPluginManager pluginmanager: :param PytestPluginManager pluginmanager:
A pytest PluginManager.
:param InvocationParams invocation_params: :param InvocationParams invocation_params:
Object containing parameters regarding the :func:`pytest.main` Object containing parameters regarding the :func:`pytest.main`
@ -1226,8 +1227,8 @@ class Config:
@hookimpl(hookwrapper=True) @hookimpl(hookwrapper=True)
def pytest_collection(self) -> Generator[None, None, None]: def pytest_collection(self) -> Generator[None, None, None]:
"""Validate invalid ini keys after collection is done so we take in account # Validate invalid ini keys after collection is done so we take in account
options added by late-loading conftest files.""" # options added by late-loading conftest files.
yield yield
self._validate_config_options() self._validate_config_options()

View File

@ -189,7 +189,7 @@ class Parser:
Default value if no ini-file option exists but is queried. Default value if no ini-file option exists but is queried.
The value of ini-variables can be retrieved via a call to The value of ini-variables can be retrieved via a call to
:py:func:`config.getini(name) <_pytest.config.Config.getini>`. :py:func:`config.getini(name) <pytest.Config.getini>`.
""" """
assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool") assert type in (None, "string", "paths", "pathlist", "args", "linelist", "bool")
self._inidict[name] = (help, type, default) self._inidict[name] = (help, type, default)

View File

@ -1379,7 +1379,8 @@ def yield_fixture(
@fixture(scope="session") @fixture(scope="session")
def pytestconfig(request: FixtureRequest) -> Config: def pytestconfig(request: FixtureRequest) -> Config:
"""Session-scoped fixture that returns the :class:`_pytest.config.Config` object. """Session-scoped fixture that returns the session's :class:`pytest.Config`
object.
Example:: Example::

View File

@ -100,12 +100,12 @@ def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") ->
to change how command line options are added. to change how command line options are added.
Options can later be accessed through the Options can later be accessed through the
:py:class:`config <_pytest.config.Config>` object, respectively: :py:class:`config <pytest.Config>` object, respectively:
- :py:func:`config.getoption(name) <_pytest.config.Config.getoption>` to - :py:func:`config.getoption(name) <pytest.Config.getoption>` to
retrieve the value of a command line option. retrieve the value of a command line option.
- :py:func:`config.getini(name) <_pytest.config.Config.getini>` to retrieve - :py:func:`config.getini(name) <pytest.Config.getini>` to retrieve
a value read from an ini-style file. a value read from an ini-style file.
The config object is passed around on many internal objects via the ``.config`` The config object is passed around on many internal objects via the ``.config``
@ -129,7 +129,7 @@ def pytest_configure(config: "Config") -> None:
.. note:: .. note::
This hook is incompatible with ``hookwrapper=True``. This hook is incompatible with ``hookwrapper=True``.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
""" """
@ -166,7 +166,7 @@ def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None:
.. note:: .. note::
This hook will not be called for ``conftest.py`` files, only for setuptools plugins. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param List[str] args: Arguments passed on the command line. :param List[str] args: Arguments passed on the command line.
""" """
@ -178,7 +178,7 @@ def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]:
Stops at first non-None result, see :ref:`firstresult`. Stops at first non-None result, see :ref:`firstresult`.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
""" """
@ -191,7 +191,7 @@ def pytest_load_initial_conftests(
.. note:: .. note::
This hook will not be called for ``conftest.py`` files, only for setuptools plugins. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
:param _pytest.config.Config early_config: The pytest config object. :param pytest.Config early_config: The pytest config object.
:param List[str] args: Arguments passed on the command line. :param List[str] args: Arguments passed on the command line.
:param _pytest.config.argparsing.Parser parser: To add command line options. :param _pytest.config.argparsing.Parser parser: To add command line options.
""" """
@ -246,7 +246,7 @@ def pytest_collection_modifyitems(
the items in-place. the items in-place.
:param pytest.Session session: The pytest session object. :param pytest.Session session: The pytest session object.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param List[pytest.Item] items: List of item objects. :param List[pytest.Item] items: List of item objects.
""" """
@ -271,7 +271,7 @@ def pytest_ignore_collect(
:param pathlib.Path fspath: The path to analyze. :param pathlib.Path fspath: The path to analyze.
:param LEGACY_PATH path: The path to analyze. :param LEGACY_PATH path: The path to analyze.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
.. versionchanged:: 6.3.0 .. versionchanged:: 6.3.0
The ``fspath`` parameter was added as a :class:`pathlib.Path` The ``fspath`` parameter was added as a :class:`pathlib.Path`
@ -385,7 +385,7 @@ def pytest_make_parametrize_id(
Stops at first non-None result, see :ref:`firstresult`. Stops at first non-None result, see :ref:`firstresult`.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param val: The parametrized value. :param val: The parametrized value.
:param str argname: The automatic parameter name produced by pytest. :param str argname: The automatic parameter name produced by pytest.
""" """
@ -609,7 +609,7 @@ def pytest_sessionfinish(
def pytest_unconfigure(config: "Config") -> None: def pytest_unconfigure(config: "Config") -> None:
"""Called before test process is exited. """Called before test process is exited.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
""" """
@ -628,7 +628,7 @@ def pytest_assertrepr_compare(
*in* a string will be escaped. Note that all but the first line will *in* a string will be escaped. Note that all but the first line will
be indented slightly, the intention is for the first line to be a summary. be indented slightly, the intention is for the first line to be a summary.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
""" """
@ -677,7 +677,7 @@ def pytest_report_header(
) -> Union[str, List[str]]: ) -> Union[str, List[str]]:
"""Return a string or list of strings to be displayed as header info for terminal reporting. """Return a string or list of strings to be displayed as header info for terminal reporting.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param Path startpath: The starting dir. :param Path startpath: The starting dir.
:param LEGACY_PATH startdir: The starting dir. :param LEGACY_PATH startdir: The starting dir.
@ -713,7 +713,7 @@ def pytest_report_collectionfinish(
.. versionadded:: 3.2 .. versionadded:: 3.2
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param Path startpath: The starting path. :param Path startpath: The starting path.
:param LEGACY_PATH startdir: The starting dir. :param LEGACY_PATH startdir: The starting dir.
:param items: List of pytest items that are going to be executed; this list should not be modified. :param items: List of pytest items that are going to be executed; this list should not be modified.
@ -752,7 +752,7 @@ def pytest_report_teststatus(
for example ``"rerun", "R", ("RERUN", {"yellow": True})``. for example ``"rerun", "R", ("RERUN", {"yellow": True})``.
:param report: The report object whose status is to be returned. :param report: The report object whose status is to be returned.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
Stops at first non-None result, see :ref:`firstresult`. Stops at first non-None result, see :ref:`firstresult`.
""" """
@ -767,7 +767,7 @@ def pytest_terminal_summary(
:param _pytest.terminal.TerminalReporter terminalreporter: The internal terminal reporter object. :param _pytest.terminal.TerminalReporter terminalreporter: The internal terminal reporter object.
:param int exitstatus: The exit status that will be reported back to the OS. :param int exitstatus: The exit status that will be reported back to the OS.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
.. versionadded:: 4.2 .. versionadded:: 4.2
The ``config`` parameter. The ``config`` parameter.
@ -857,7 +857,7 @@ def pytest_markeval_namespace(config: "Config") -> Dict[str, Any]:
.. versionadded:: 6.2 .. versionadded:: 6.2
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:returns: A dictionary of additional globals to add. :returns: A dictionary of additional globals to add.
""" """
@ -909,7 +909,7 @@ def pytest_enter_pdb(config: "Config", pdb: "pdb.Pdb") -> None:
Can be used by plugins to take special action just before the python Can be used by plugins to take special action just before the python
debugger enters interactive mode. debugger enters interactive mode.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param pdb.Pdb pdb: The Pdb instance. :param pdb.Pdb pdb: The Pdb instance.
""" """
@ -920,6 +920,6 @@ def pytest_leave_pdb(config: "Config", pdb: "pdb.Pdb") -> None:
Can be used by plugins to take special action just after the python Can be used by plugins to take special action just after the python
debugger leaves interactive mode. debugger leaves interactive mode.
:param _pytest.config.Config config: The pytest config object. :param pytest.Config config: The pytest config object.
:param pdb.Pdb pdb: The Pdb instance. :param pdb.Pdb pdb: The Pdb instance.
""" """

View File

@ -954,7 +954,7 @@ class Pytester:
) -> Optional[Union[Collector, Item]]: ) -> Optional[Union[Collector, Item]]:
"""Return the collection node of a file. """Return the collection node of a file.
:param _pytest.config.Config config: :param pytest.Config config:
A pytest config. A pytest config.
See :py:meth:`parseconfig` and :py:meth:`parseconfigure` for creating it. See :py:meth:`parseconfig` and :py:meth:`parseconfigure` for creating it.
:param os.PathLike[str] arg: :param os.PathLike[str] arg:
@ -1186,7 +1186,7 @@ class Pytester:
This invokes the pytest bootstrapping code in _pytest.config to create This invokes the pytest bootstrapping code in _pytest.config to create
a new :py:class:`_pytest.core.PluginManager` and call the a new :py:class:`_pytest.core.PluginManager` and call the
pytest_cmdline_parse hook to create a new pytest_cmdline_parse hook to create a new
:py:class:`_pytest.config.Config` instance. :py:class:`pytest.Config` instance.
If :py:attr:`plugins` has been populated they should be plugin modules If :py:attr:`plugins` has been populated they should be plugin modules
to be registered with the PluginManager. to be registered with the PluginManager.
@ -1206,7 +1206,7 @@ class Pytester:
def parseconfigure(self, *args: Union[str, "os.PathLike[str]"]) -> Config: def parseconfigure(self, *args: Union[str, "os.PathLike[str]"]) -> Config:
"""Return a new pytest configured Config instance. """Return a new pytest configured Config instance.
Returns a new :py:class:`_pytest.config.Config` instance like Returns a new :py:class:`pytest.Config` instance like
:py:meth:`parseconfig`, but also calls the pytest_configure hook. :py:meth:`parseconfig`, but also calls the pytest_configure hook.
""" """
config = self.parseconfig(*args) config = self.parseconfig(*args)

View File

@ -970,7 +970,7 @@ class Metafunc:
#: Access to the underlying :class:`_pytest.python.FunctionDefinition`. #: Access to the underlying :class:`_pytest.python.FunctionDefinition`.
self.definition = definition self.definition = definition
#: Access to the :class:`_pytest.config.Config` object for the test session. #: Access to the :class:`pytest.Config` object for the test session.
self.config = config self.config = config
#: The module object where the test function is defined in. #: The module object where the test function is defined in.

View File

@ -7,6 +7,7 @@ from _pytest.assertion import register_assert_rewrite
from _pytest.cacheprovider import Cache from _pytest.cacheprovider import Cache
from _pytest.capture import CaptureFixture from _pytest.capture import CaptureFixture
from _pytest.config import cmdline from _pytest.config import cmdline
from _pytest.config import Config
from _pytest.config import console_main from _pytest.config import console_main
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.config import hookimpl from _pytest.config import hookimpl
@ -77,6 +78,7 @@ __all__ = [
"cmdline", "cmdline",
"collect", "collect",
"Collector", "Collector",
"Config",
"console_main", "console_main",
"deprecated_call", "deprecated_call",
"exit", "exit",