Skip to content

Commit f4d3a6c

Browse files
committed
Preserve getini strings for max_warnings
Co-authored-by: CenFangyu <[email protected]>
1 parent 72679ce commit f4d3a6c

4 files changed

Lines changed: 22 additions & 96 deletions

File tree

‎doc/en/reference/reference.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ passed multiple times. The expected format is ``name=value``. For example::
17051705

17061706

17071707
.. confval:: max_warnings
1708-
:type: ``int | str``
1708+
:type: ``str | int``
17091709

17101710
.. versionadded:: 9.1
17111711

@@ -3671,7 +3671,7 @@ All the command-line flags can also be obtained by running ``pytest --help``::
36713671
Each line specifies a pattern for
36723672
warnings.filterwarnings. Processed after
36733673
-W/--pythonwarnings.
3674-
max_warnings (int | string):
3674+
max_warnings (string | int):
36753675
Exit with error if all tests pass but the number of
36763676
warnings exceeds this threshold
36773677
norecursedirs (args): Directory patterns to avoid for recursion

‎src/_pytest/main.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ def pytest_addoption(parser: Parser) -> None:
141141
"warnings.filterwarnings. "
142142
"Processed after -W/--pythonwarnings.",
143143
)
144-
# ``int | str`` (not plain ``int``) for backward compatibility: INI files
145-
# and ``-o`` overrides provide the value as a string.
144+
# ``str | int`` (not plain ``int``) so INI files and ``-o`` keep returning
145+
# strings, while native TOML integers are still accepted.
146146
parser.addini(
147147
"max_warnings",
148148
help="Exit with error if all tests pass but the number of warnings exceeds this threshold",
149-
type=int | str,
150-
default=None,
149+
type=str | int,
150+
default="",
151151
)
152152

153153
group = parser.getgroup("collect", "collection")

‎src/_pytest/terminal.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ def _get_max_warnings(self) -> int | None:
10931093
if value is not None:
10941094
return int(value)
10951095
ini_value = self.config.getini("max_warnings")
1096-
if ini_value is None:
1096+
if ini_value == "":
10971097
return None
10981098
return int(ini_value)
10991099

‎testing/test_warnings.py‎

Lines changed: 15 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,12 +1005,6 @@ def test_two():
10051005
warnings.warn(UserWarning("warning two"))
10061006
"""
10071007

1008-
ONE_WARNING_PYFILE = """
1009-
import warnings
1010-
def test_warning():
1011-
warnings.warn(UserWarning("example warning"))
1012-
"""
1013-
10141008
@pytest.mark.filterwarnings("default::UserWarning")
10151009
def test_max_warnings_not_set(self, pytester: Pytester) -> None:
10161010
"""Without --max-warnings, warnings don't affect exit code."""
@@ -1152,94 +1146,26 @@ def test_one():
11521146
assert result.ret == ExitCode.OK
11531147

11541148
@pytest.mark.filterwarnings("default::UserWarning")
1155-
@pytest.mark.parametrize(
1156-
("writer", "source"),
1157-
[
1158-
pytest.param(
1159-
"toml",
1160-
"""
1161-
[pytest]
1162-
max_warnings = 0
1163-
""",
1164-
id="pytest-toml-int",
1165-
),
1166-
pytest.param(
1167-
"toml",
1168-
"""
1169-
[pytest]
1170-
max_warnings = "0"
1171-
""",
1172-
id="pytest-toml-string",
1173-
),
1174-
pytest.param(
1175-
"pyproject",
1176-
"""
1177-
[tool.pytest]
1178-
max_warnings = 0
1179-
""",
1180-
id="pyproject-native-int",
1181-
),
1182-
pytest.param(
1183-
"pyproject",
1184-
"""
1185-
[tool.pytest]
1186-
max_warnings = "0"
1187-
""",
1188-
id="pyproject-native-string",
1189-
),
1190-
pytest.param(
1191-
"ini",
1192-
"""
1193-
[pytest]
1194-
max_warnings = 0
1195-
""",
1196-
id="pytest-ini",
1197-
),
1198-
],
1199-
)
1200-
def test_max_warnings_config_accepts_int_and_string(
1201-
self, pytester: Pytester, writer: str, source: str
1202-
) -> None:
1203-
"""Native TOML integers and quoted/INI strings are accepted (#14953).
1204-
1205-
An explicit zero must be distinguished from the unset default.
1206-
"""
1207-
if writer == "toml":
1208-
pytester.maketoml(source)
1209-
elif writer == "pyproject":
1210-
pytester.makepyprojecttoml(source)
1211-
else:
1212-
assert writer == "ini"
1213-
pytester.makeini(source)
1214-
pytester.makepyfile(self.ONE_WARNING_PYFILE)
1215-
result = pytester.runpytest()
1216-
assert result.ret == ExitCode.MAX_WARNINGS_ERROR
1217-
result.stdout.fnmatch_lines(
1218-
["*Tests pass, but maximum allowed warnings exceeded: 1 > 0*"]
1219-
)
1220-
1221-
@pytest.mark.filterwarnings("default::UserWarning")
1222-
def test_max_warnings_override_ini(self, pytester: Pytester) -> None:
1223-
"""-o max_warnings=0 is accepted and treated as an explicit zero (#14953)."""
1224-
pytester.makepyfile(self.ONE_WARNING_PYFILE)
1225-
result = pytester.runpytest("-o", "max_warnings=0")
1226-
assert result.ret == ExitCode.MAX_WARNINGS_ERROR
1227-
result.stdout.fnmatch_lines(
1228-
["*Tests pass, but maximum allowed warnings exceeded: 1 > 0*"]
1229-
)
1230-
1231-
@pytest.mark.filterwarnings("default::UserWarning")
1232-
def test_max_warnings_cli_overrides_ini(self, pytester: Pytester) -> None:
1233-
"""--max-warnings takes precedence over the configuration value."""
1234-
pytester.makeini(
1149+
def test_max_warnings_toml_native_int_zero(self, pytester: Pytester) -> None:
1150+
"""Unquoted native TOML integer 0 is accepted and enforced (#14953)."""
1151+
pytester.maketoml(
12351152
"""
12361153
[pytest]
1237-
max_warnings = 10
1154+
max_warnings = 0
12381155
"""
12391156
)
1240-
pytester.makepyfile(self.PYFILE)
1241-
result = pytester.runpytest("--max-warnings", "0")
1157+
pytester.makepyfile(
1158+
"""
1159+
import warnings
1160+
def test_warning():
1161+
warnings.warn(UserWarning("example warning"))
1162+
"""
1163+
)
1164+
result = pytester.runpytest()
12421165
assert result.ret == ExitCode.MAX_WARNINGS_ERROR
1166+
result.stdout.fnmatch_lines(
1167+
["*Tests pass, but maximum allowed warnings exceeded: 1 > 0*"]
1168+
)
12431169

12441170

12451171
def test_pythonwarnings_not_duplicated(pytester: Pytester) -> None:

0 commit comments

Comments
 (0)