Skip to content

Commit 3faf9c6

Browse files
ikappakiTusenka
authored andcommitted
Replace : with ; in the assertion rewrite warning message (pytest-dev#13429)
This makes it possible to filter them using standard Python warning filters. --- Co-authored-by: ikappaki <[email protected]>
1 parent 25815eb commit 3faf9c6

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

changelog/5473.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace `:` with `;` in the assertion rewrite warning message so it can be filtered using standard Python warning filters before calling :func:`pytest.main`.

src/_pytest/assertion/rewrite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def _warn_already_imported(self, name: str) -> None:
282282

283283
self.config.issue_config_time_warning(
284284
PytestAssertRewriteWarning(
285-
f"Module already imported so cannot be rewritten: {name}"
285+
f"Module already imported so cannot be rewritten; {name}"
286286
),
287287
stacklevel=5,
288288
)

testing/test_assertrewrite.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,23 @@ def test_rewrite_warning(self, pytester: Pytester) -> None:
12021202
)
12031203
# needs to be a subprocess because pytester explicitly disables this warning
12041204
result = pytester.runpytest_subprocess()
1205-
result.stdout.fnmatch_lines(["*Module already imported*: _pytest"])
1205+
result.stdout.fnmatch_lines(["*Module already imported*; _pytest"])
1206+
1207+
def test_rewrite_warning_ignore(self, pytester: Pytester) -> None:
1208+
pytester.makeconftest(
1209+
"""
1210+
import pytest
1211+
pytest.register_assert_rewrite("_pytest")
1212+
"""
1213+
)
1214+
# needs to be a subprocess because pytester explicitly disables this warning
1215+
result = pytester.runpytest_subprocess(
1216+
"-W",
1217+
"ignore:Module already imported so cannot be rewritten; _pytest:pytest.PytestAssertRewriteWarning",
1218+
)
1219+
# Previously, when the message pattern used to contain an extra `:`, an error was raised.
1220+
assert not result.stderr.str().strip()
1221+
result.stdout.no_fnmatch_line("*Module already imported*; _pytest")
12061222

12071223
def test_rewrite_module_imported_from_conftest(self, pytester: Pytester) -> None:
12081224
pytester.makeconftest(

0 commit comments

Comments
 (0)