Skip to content

Commit 6ed587d

Browse files
committed
Accept numeric strings for verbosity options in native TOML again
pytest-dev#15153 registered the verbosity_* options as `int | Literal["auto"]`. In native TOML mode neither member accepts a numeric string, so `verbosity_assertions = "2"` raised a UsageError, although the changelog promised strings keep working. Register them as `int | str` (like tmp_path_retention_count) and validate the string in `Config.get_verbosity`, which now raises a UsageError naming the option for values that are neither an int nor "auto". Fixes pytest-dev#15290. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a7b2446 commit 6ed587d

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

‎changelog/15290.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Native TOML configuration again accepts numeric strings such as ``"2"`` for :confval:`verbosity_assertions`, :confval:`verbosity_test_cases` and :confval:`verbosity_subtests`, as promised for backward compatibility in pytest 9.2.0 -- regressed by :pr:`15153`.

‎src/_pytest/config/__init__.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2268,8 +2268,15 @@ def get_verbosity(self, verbosity_type: str | None = None) -> int:
22682268
level = self.getini(ini_name)
22692269
if level == Config._VERBOSITY_INI_DEFAULT:
22702270
return global_level
2271-
2272-
return int(level)
2271+
if isinstance(level, int):
2272+
return level
2273+
try:
2274+
return int(level)
2275+
except ValueError:
2276+
raise UsageError(
2277+
f"{self.inipath}: config option '{ini_name}' expects an int "
2278+
f"or 'auto', got {level!r}"
2279+
) from None
22732280

22742281
@staticmethod
22752282
def _verbosity_ini_name(verbosity_type: str) -> str:
@@ -2289,7 +2296,9 @@ def _add_verbosity_ini(parser: Parser, verbosity_type: str, help: str) -> None:
22892296
parser.addini(
22902297
Config._verbosity_ini_name(verbosity_type),
22912298
help=help,
2292-
type=int | Literal["auto"],
2299+
# str rather than Literal["auto"]: numeric strings stay accepted
2300+
# in TOML for backward compatibility, see get_verbosity.
2301+
type=int | str,
22932302
default=Config._VERBOSITY_INI_DEFAULT,
22942303
)
22952304

‎testing/test_config.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,7 +3630,6 @@ def test_getini(
36303630
@pytest.mark.parametrize(
36313631
("name", "value"),
36323632
[
3633-
("verbosity_assertions", '"loud"'),
36343633
("log_file_mode", '"x"'),
36353634
("parametrize_long_str_id_strategy", '"bogus"'),
36363635
],
@@ -3641,7 +3640,15 @@ def test_invalid_value(self, pytester: Pytester, name: str, value: str) -> None:
36413640
with pytest.raises(UsageError, match=name):
36423641
config.getini(name)
36433642

3644-
def test_get_verbosity(self, pytester: Pytester) -> None:
3645-
pytester.maketoml("[pytest]\nverbosity_assertions = 2\n")
3643+
@pytest.mark.parametrize("toml_value", ["2", '"2"'])
3644+
def test_get_verbosity(self, pytester: Pytester, toml_value: str) -> None:
3645+
pytester.maketoml(f"[pytest]\nverbosity_assertions = {toml_value}\n")
36463646
config = pytester.parseconfig()
36473647
assert config.get_verbosity(Config.VERBOSITY_ASSERTIONS) == 2
3648+
3649+
@pytest.mark.parametrize("toml_value", ['"loud"', "true", "2.5"])
3650+
def test_get_verbosity_invalid(self, pytester: Pytester, toml_value: str) -> None:
3651+
pytester.maketoml(f"[pytest]\nverbosity_assertions = {toml_value}\n")
3652+
config = pytester.parseconfig()
3653+
with pytest.raises(UsageError, match="verbosity_assertions"):
3654+
config.get_verbosity(Config.VERBOSITY_ASSERTIONS)

0 commit comments

Comments
 (0)