Skip to content
Merged
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Ilya Konstantinov
Ionuț Turturică
Isaac Virshup
Israel Fruchter
Israël Hallé
Itxaso Aizpurua
Iwan Briquemont
Jaap Broekhuizen
Expand Down
1 change: 1 addition & 0 deletions changelog/13732.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Previously, when filtering warnings, pytest would fail if the filter referenced a class that could not be imported. Now, this only outputs a message indicating the problem.
18 changes: 16 additions & 2 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,8 @@ def parse_warning_filter(
raise UsageError(error_template.format(error=str(e))) from None
try:
category: type[Warning] = _resolve_warning_category(category_)
except ImportError:
raise
except Exception:
exc_info = ExceptionInfo.from_current()
exception_text = exc_info.getrepr(style="native")
Expand Down Expand Up @@ -2023,7 +2025,19 @@ def apply_warning_filters(
# Filters should have this precedence: cmdline options, config.
# Filters should be applied in the inverse order of precedence.
for arg in config_filters:
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
try:
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
except ImportError as e:
warnings.warn(
f"Failed to import filter module '{e.name}': {arg}", PytestConfigWarning
)
continue

for arg in cmdline_filters:
warnings.filterwarnings(*parse_warning_filter(arg, escape=True))
try:
warnings.filterwarnings(*parse_warning_filter(arg, escape=True))
except ImportError as e:
warnings.warn(
f"Failed to import filter module '{e.name}': {arg}", PytestConfigWarning
)
continue
2 changes: 0 additions & 2 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2400,8 +2400,6 @@ def test_parse_warning_filter(
":" * 5,
# Invalid action.
"FOO::",
# ImportError when importing the warning class.
"::test_parse_warning_filter_failure.NonExistentClass::",
# Class is not a Warning subclass.
"::list::",
# Negative line number.
Expand Down
27 changes: 27 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,33 @@ def test():
result.stdout.fnmatch_lines(["* 1 failed in*"])


def test_accept_unknown_category(pytester: Pytester) -> None:
"""Category types that can't be imported don't cause failure (#13732)."""
pytester.makeini(
"""
[pytest]
filterwarnings =
always:Failed to import filter module.*:pytest.PytestConfigWarning
ignore::foobar.Foobar
"""
)
pytester.makepyfile(
"""
def test():
pass
"""
)
result = pytester.runpytest("-W", "ignore::bizbaz.Bizbaz")
result.stdout.fnmatch_lines(
[
f"*== {WARNINGS_SUMMARY_HEADER} ==*",
"*PytestConfigWarning: Failed to import filter module 'foobar': ignore::foobar.Foobar",
"*PytestConfigWarning: Failed to import filter module 'bizbaz': ignore::bizbaz.Bizbaz",
"* 1 passed, * warning*",
]
)


class TestDeprecationWarningsByDefault:
"""
Note: all pytest runs are executed in a subprocess so we don't inherit warning filters
Expand Down
Loading