Skip to content

Commit c77995b

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

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
@@ -281,7 +281,7 @@ def _warn_already_imported(self, name: str) -> None:
281281

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

testing/test_assertrewrite.py

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

12021218
def test_rewrite_module_imported_from_conftest(self, pytester: Pytester) -> None:
12031219
pytester.makeconftest(

0 commit comments

Comments
 (0)