Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid regex handling in filterwarnings #13124

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
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/13119.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved handling of invalid regex patterns for filter warnings by providing a clear error message.
7 changes: 7 additions & 0 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,13 @@ def parse_warning_filter(
) from None
else:
lineno = 0
try:
re.compile(message)
re.compile(module)
except re.error as e:
raise UsageError(
error_template.format(error=f"Invalid regex {e.pattern!r}: {e}")
) from None
return action, message, category, module, lineno


Expand Down
23 changes: 23 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,29 @@ def test_hidden_by_system(self, pytester: Pytester, monkeypatch) -> None:
result = pytester.runpytest_subprocess()
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()

def test_invalid_regex_in_filterwarning(self, pytester: Pytester) -> None:
self.create_file(pytester)
pytester.makeini(
"""
[pytest]
filterwarnings =
ignore::DeprecationWarning:*
"""
)
result = pytester.runpytest_subprocess()
virendrapatil24 marked this conversation as resolved.
Show resolved Hide resolved
assert result.ret == pytest.ExitCode.USAGE_ERROR
result.stderr.fnmatch_lines(
[
"ERROR: while parsing the following warning configuration:",
"",
" ignore::DeprecationWarning:[*]",
"",
"This error occurred:",
"",
"Invalid regex '[*]': nothing to repeat at position 0",
]
)


@pytest.mark.skip("not relevant until pytest 9.0")
@pytest.mark.parametrize("change_default", [None, "ini", "cmdline"])
Expand Down