Skip to content

Commit b88c3f8

Browse files
committed
Deprecate pytest.config
1 parent 4a3c8e2 commit b88c3f8

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

changelog/3050.deprecation.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate ``pytest.config`` global. See https://docs.pytest.org/en/latest/deprecations.html#pytest-config-global

doc/en/deprecations.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ Below is a complete list of all pytest features which are considered deprecated.
1414
:class:`_pytest.warning_types.PytestWarning` or subclasses, which can be filtered using
1515
:ref:`standard warning filters <warnings>`.
1616

17+
``pytest.config`` global
18+
~~~~~~~~~~~~~~~~~~~~~~~~
19+
20+
.. deprecated:: 4.1
21+
22+
The ``pytest.config`` global object is deprecated. Instead use
23+
``request.config`` (via the ``request`` fixture) or if you are a plugin author
24+
use the ``pytest_configure(config)`` hook.
25+
1726
.. _raises-warns-exec:
1827

1928
``raises`` / ``warns`` with a string as the second argument

src/_pytest/deprecated.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@
9797
"Please move it to the top level conftest file instead."
9898
)
9999

100+
PYTEST_CONFIG_GLOBAL = PytestDeprecationWarning(
101+
"the `pytest.config` global is deprecated. Please use `request.config` "
102+
"or `pytest_configure` (if you're a pytest plugin) instead."
103+
)
100104

101105
PYTEST_ENSURETEMP = RemovedInPytest4Warning(
102106
"pytest/tmpdir_factory.ensuretemp is deprecated, \n"

src/_pytest/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import pkgutil
1010
import sys
11+
import warnings
1112

1213
import attr
1314
import py
@@ -18,6 +19,7 @@
1819
from _pytest.config import directory_arg
1920
from _pytest.config import hookimpl
2021
from _pytest.config import UsageError
22+
from _pytest.deprecated import PYTEST_CONFIG_GLOBAL
2123
from _pytest.outcomes import exit
2224
from _pytest.runner import collect_one_node
2325

@@ -167,8 +169,24 @@ def pytest_addoption(parser):
167169
)
168170

169171

172+
class _ConfigDeprecated(object):
173+
def __init__(self, config):
174+
self.__dict__["_config"] = config
175+
176+
def __getattr__(self, attr):
177+
warnings.warn(PYTEST_CONFIG_GLOBAL, stacklevel=2)
178+
return getattr(self._config, attr)
179+
180+
def __setattr__(self, attr, val):
181+
warnings.warn(PYTEST_CONFIG_GLOBAL, stacklevel=2)
182+
return setattr(self._config, attr, val)
183+
184+
def __repr__(self):
185+
return "{}({!r})".format(type(self).__name__, self._config)
186+
187+
170188
def pytest_configure(config):
171-
__import__("pytest").config = config # compatibility
189+
__import__("pytest").config = _ConfigDeprecated(config) # compatibility
172190

173191

174192
def wrap_session(config, doit):

testing/test_terminal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def test_method(self):
539539
result.stdout.fnmatch_lines(["test_passes.py ..*", "* 2 pass*"])
540540
assert result.ret == 0
541541

542-
def test_header_trailer_info(self, testdir):
542+
def test_header_trailer_info(self, testdir, request):
543543
testdir.makepyfile(
544544
"""
545545
def test_passes():
@@ -563,7 +563,7 @@ def test_passes():
563563
"=* 1 passed*in *.[0-9][0-9] seconds *=",
564564
]
565565
)
566-
if pytest.config.pluginmanager.list_plugin_distinfo():
566+
if request.config.pluginmanager.list_plugin_distinfo():
567567
result.stdout.fnmatch_lines(["plugins: *"])
568568

569569
def test_showlocals(self, testdir):

0 commit comments

Comments
 (0)