Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/14808.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Passing a non-string value to a ``string``-typed ini option now emits a ``pytest.PytestRemovedIn10Warning``. This affects ``[tool.pytest.ini_options]`` in ``pyproject.toml``, where TOML arrays are kept as lists by ``make_scalar`` instead of being converted to strings. In pytest 10 this will raise a ``TypeError``, matching the behavior of the corresponding TOML config path.
5 changes: 5 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,11 @@ def _getini_ini(
elif type == "bool":
return _strtobool(str(value).strip())
elif type == "string":
if not isinstance(value, str):
warnings.warn(
_pytest.deprecated.INI_STRING_TYPE_NON_STR_VALUE,
stacklevel=2,
)
return value
elif type == "int":
if not isinstance(value, str):
Expand Down
8 changes: 8 additions & 0 deletions src/_pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@
"See https://docs.pytest.org/en/stable/deprecations.html#the-pastebin-option"
)

INI_STRING_TYPE_NON_STR_VALUE = PytestRemovedIn10Warning(
"Passing a value that is not a string to a 'string'-typed ini option is deprecated.\n"
"In a future version this will raise a TypeError, matching the behavior of the "
"corresponding TOML config path.\n"
"If your plugin intentionally accepts non-string values, declare an explicit type "
'(e.g. type="args") instead of relying on the implicit string default.'
)

# You want to make some `__init__` or function "private".
#
# def my_private_function(some, args):
Expand Down
24 changes: 24 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,30 @@ def pytest_addoption(parser):
):
_ = config.getini("ini_param")

def test_addini_string_non_str_deprecated(self, pytester: Pytester) -> None:
"""Passing a non-string value to a 'string'-typed ini option emits a
deprecation warning. The value is still returned as-is for now, but will
raise TypeError in pytest 10."""
pytester.makeconftest(
"""
def pytest_addoption(parser):
parser.addini("myname", "", type="string")
"""
)
pytester.makepyprojecttoml(
"""
[tool.pytest.ini_options]
myname = ["value1", "value2"]
"""
)
config = pytester.parseconfig()
with pytest.warns(
pytest.PytestRemovedIn10Warning,
match="Passing a value that is not a string to a 'string'-typed ini option",
):
result = config.getini("myname")
assert result == ["value1", "value2"]

UNION_CONFTEST = """
def pytest_addoption(parser):
parser.addini("ini_param", "", type=int | str, default=None)
Expand Down