Skip to content

Commit 8db15d9

Browse files
committed
config: Do not fail if a warning filter category cannot be imported
1 parent 5366cd2 commit 8db15d9

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
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+
Do not fail when a warning category cannot be imported. Log a warning instead.

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_warnings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,32 @@ 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 'bizbaz': ignore::bizbaz.Bizbaz",
448+
"* 1 passed, * warning*",
449+
]
450+
)
451+
452+
427453
class TestDeprecationWarningsByDefault:
428454
"""
429455
Note: all pytest runs are executed in a subprocess so we don't inherit warning filters

0 commit comments

Comments
 (0)