Skip to content

Commit 950e781

Browse files
committed
config: Do not fail if a warning filter category cannot be imported
1 parent 9913ced commit 950e781

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Ilya Konstantinov
201201
Ionuț Turturică
202202
Isaac Virshup
203203
Israel Fruchter
204+
Israël Hallé
204205
Itxaso Aizpurua
205206
Iwan Briquemont
206207
Jaap Broekhuizen

changelog/13732.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

src/_pytest/config/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,8 @@ def parse_warning_filter(
19651965
raise UsageError(error_template.format(error=str(e))) from None
19661966
try:
19671967
category: type[Warning] = _resolve_warning_category(category_)
1968+
except ImportError:
1969+
raise
19681970
except Exception:
19691971
exc_info = ExceptionInfo.from_current()
19701972
exception_text = exc_info.getrepr(style="native")
@@ -2023,7 +2025,19 @@ def apply_warning_filters(
20232025
# Filters should have this precedence: cmdline options, config.
20242026
# Filters should be applied in the inverse order of precedence.
20252027
for arg in config_filters:
2026-
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
2028+
try:
2029+
warnings.filterwarnings(*parse_warning_filter(arg, escape=False))
2030+
except ImportError as e:
2031+
warnings.warn(
2032+
f"Failed to import filter module '{e.name}': {arg}", PytestConfigWarning
2033+
)
2034+
continue
20272035

20282036
for arg in cmdline_filters:
2029-
warnings.filterwarnings(*parse_warning_filter(arg, escape=True))
2037+
try:
2038+
warnings.filterwarnings(*parse_warning_filter(arg, escape=True))
2039+
except ImportError as e:
2040+
warnings.warn(
2041+
f"Failed to import filter module '{e.name}': {arg}", PytestConfigWarning
2042+
)
2043+
continue

testing/test_config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,8 +2400,6 @@ def test_parse_warning_filter(
24002400
":" * 5,
24012401
# Invalid action.
24022402
"FOO::",
2403-
# ImportError when importing the warning class.
2404-
"::test_parse_warning_filter_failure.NonExistentClass::",
24052403
# Class is not a Warning subclass.
24062404
"::list::",
24072405
# Negative line number.

testing/test_warnings.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,33 @@ def test():
424424
result.stdout.fnmatch_lines(["* 1 failed in*"])
425425

426426

427+
def test_accept_unknown_category(pytester: Pytester) -> None:
428+
"""Category types that can't be imported don't cause failure (#13732)."""
429+
pytester.makeini(
430+
"""
431+
[pytest]
432+
filterwarnings =
433+
always:Failed to import filter module.*:pytest.PytestConfigWarning
434+
ignore::foobar.Foobar
435+
"""
436+
)
437+
pytester.makepyfile(
438+
"""
439+
def test():
440+
pass
441+
"""
442+
)
443+
result = pytester.runpytest("-W", "ignore::bizbaz.Bizbaz")
444+
result.stdout.fnmatch_lines(
445+
[
446+
f"*== {WARNINGS_SUMMARY_HEADER} ==*",
447+
"*PytestConfigWarning: Failed to import filter module 'foobar': ignore::foobar.Foobar",
448+
"*PytestConfigWarning: Failed to import filter module 'bizbaz': ignore::bizbaz.Bizbaz",
449+
"* 1 passed, * warning*",
450+
]
451+
)
452+
453+
427454
class TestDeprecationWarningsByDefault:
428455
"""
429456
Note: all pytest runs are executed in a subprocess so we don't inherit warning filters

0 commit comments

Comments
 (0)