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 11 commits into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,22 @@ 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:*
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
"""

)
result = pytester.runpytest_subprocess()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should assert on the exit code also

result.stderr.fnmatch_lines(
[
"*Invalid regex '*': nothing to repeat at position 0*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more thorough to test on the whole "while parsing ..." output?

]
)


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