File tree Expand file tree Collapse file tree 5 files changed +45
-4
lines changed
Expand file tree Collapse file tree 5 files changed +45
-4
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,7 @@ Ilya Konstantinov
201201Ionuț Turturică
202202Isaac Virshup
203203Israel Fruchter
204+ Israël Hallé
204205Itxaso Aizpurua
205206Iwan Briquemont
206207Jaap Broekhuizen
Original file line number Diff line number Diff line change 1+ Do not fail when a warning category cannot be imported. Log a warning instead.
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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+
427454class TestDeprecationWarningsByDefault :
428455 """
429456 Note: all pytest runs are executed in a subprocess so we don't inherit warning filters
You can’t perform that action at this time.
0 commit comments