Skip to content

Commit 78e1bd7

Browse files
committed
config: deprecate config.inicfg
As a private attribute, we broke it in pytest 9.0.0, but since it's not using a `_` prefix and has some external usage, let's keep it working until pytest 10 and deprecate it instead. Fix #13946.
1 parent 1738920 commit 78e1bd7

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

changelog/13946.deprecation.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The private ``config.inicfg`` attribute is now deprecated.
2+
Use :meth:`config.getini() <pytest.Config.getini>` to access configuration values instead.
3+
4+
See :ref:`config-inicfg` for more details.

doc/en/deprecations.rst

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

1717

18+
.. _config-inicfg:
19+
20+
``config.inicfg``
21+
~~~~~~~~~~~~~~~~~
22+
23+
.. deprecated:: 9.0
24+
25+
The private ``config.inicfg`` attribute is deprecated.
26+
Use :meth:`config.getini() <pytest.Config.getini>` to access configuration values instead.
27+
28+
``config.inicfg`` was never documented and it should have had a ``_`` prefix from the start.
29+
Pytest performs caching, transformation and aliasing on configuration options which make direct access to the raw ``config.inicfg`` untenable.
30+
31+
**Reading configuration values:**
32+
33+
Instead of accessing ``config.inicfg`` directly, use :meth:`config.getini() <pytest.Config.getini>`:
34+
35+
.. code-block:: python
36+
37+
# Deprecated
38+
value = config.inicfg["some_option"]
39+
40+
# Use this instead
41+
value = config.getini("some_option")
42+
43+
**Setting configuration values:**
44+
45+
Setting or deleting configuration values after initialization is not supported.
46+
If you need to override configuration values, use the ``-o`` command line option:
47+
48+
.. code-block:: bash
49+
50+
pytest -o some_option=value
51+
52+
or set them in your configuration file instead.
53+
54+
1855
.. _parametrize-iterators:
1956

2057
Non-Collection iterables in ``@pytest.mark.parametrize``

src/_pytest/config/__init__.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from _pytest._code.code import TracebackStyle
6060
from _pytest._io import TerminalWriter
6161
from _pytest.compat import assert_never
62+
from _pytest.compat import deprecated
6263
from _pytest.compat import NOTSET
6364
from _pytest.config.argparsing import Argument
6465
from _pytest.config.argparsing import FILE_OR_DIR
@@ -1126,9 +1127,23 @@ def __init__(
11261127
self.args_source = Config.ArgsSource.ARGS
11271128
self.args: list[str] = []
11281129

1129-
@property
1130-
def inicfg(self) -> _DeprecatedInicfgProxy:
1131-
return _DeprecatedInicfgProxy(self)
1130+
if TYPE_CHECKING:
1131+
1132+
@deprecated(
1133+
"config.inicfg is deprecated, use config.getini() to access configuration values instead.",
1134+
)
1135+
@property
1136+
def inicfg(self) -> _DeprecatedInicfgProxy:
1137+
raise NotImplementedError()
1138+
else:
1139+
1140+
@property
1141+
def inicfg(self) -> _DeprecatedInicfgProxy:
1142+
warnings.warn(
1143+
_pytest.deprecated.CONFIG_INICFG,
1144+
stacklevel=2,
1145+
)
1146+
return _DeprecatedInicfgProxy(self)
11321147

11331148
@property
11341149
def rootpath(self) -> pathlib.Path:

src/_pytest/deprecated.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
"See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators",
8484
)
8585

86+
CONFIG_INICFG = PytestRemovedIn10Warning(
87+
"config.inicfg is deprecated, use config.getini() to access configuration values instead.\n"
88+
"See https://docs.pytest.org/en/stable/deprecations.html#config-inicfg"
89+
)
90+
8691
# You want to make some `__init__` or function "private".
8792
#
8893
# def my_private_function(some, args):

testing/test_config.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from _pytest.monkeypatch import MonkeyPatch
3131
from _pytest.pathlib import absolutepath
3232
from _pytest.pytester import Pytester
33+
from _pytest.warning_types import PytestDeprecationWarning
3334
import pytest
3435

3536

@@ -3001,10 +3002,10 @@ def pytest_addoption(parser):
30013002

30023003

30033004
class TestInicfgDeprecation:
3004-
"""Tests for the upcoming deprecation of config.inicfg."""
3005+
"""Tests for the deprecation of config.inicfg."""
30053006

30063007
def test_inicfg_deprecated(self, pytester: Pytester) -> None:
3007-
"""Test that accessing config.inicfg issues a deprecation warning (not yet)."""
3008+
"""Test that accessing config.inicfg issues a deprecation warning."""
30083009
pytester.makeini(
30093010
"""
30103011
[pytest]
@@ -3013,7 +3014,10 @@ def test_inicfg_deprecated(self, pytester: Pytester) -> None:
30133014
)
30143015
config = pytester.parseconfig()
30153016

3016-
inicfg = config.inicfg
3017+
with pytest.warns(
3018+
PytestDeprecationWarning, match=r"config\.inicfg is deprecated"
3019+
):
3020+
inicfg = config.inicfg # type: ignore[deprecated]
30173021

30183022
assert config.getini("minversion") == "3.0"
30193023
assert inicfg["minversion"] == "3.0"

0 commit comments

Comments
 (0)