@@ -2536,7 +2536,8 @@ def exception_matches(self, exc_type, exc_value, trace_back):
25362536
25372537@contextmanager
25382538def assert_produces_warning (expected_warning = Warning , filter_level = "always" ,
2539- clear = None , check_stacklevel = True ):
2539+ clear = None , check_stacklevel = True ,
2540+ raise_on_extra_warnings = True ):
25402541 """
25412542 Context manager for running code expected to either raise a specific
25422543 warning, or not raise any warnings. Verifies that the code raises the
@@ -2549,7 +2550,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
25492550 The type of Exception raised. ``exception.Warning`` is the base
25502551 class for all warnings. To check that no warning is returned,
25512552 specify ``False`` or ``None``.
2552- filter_level : str, default "always"
2553+ filter_level : str or None , default "always"
25532554 Specifies whether warnings are ignored, displayed, or turned
25542555 into errors.
25552556 Valid values are:
@@ -2573,6 +2574,9 @@ class for all warnings. To check that no warning is returned,
25732574 If True, displays the line that called the function containing
25742575 the warning to show were the function is called. Otherwise, the
25752576 line that implements the function is displayed.
2577+ raise_on_extra_warnings : bool, default True
2578+ Whether extra warnings not of the type `expected_warning` should
2579+ cause the test to fail.
25762580
25772581 Examples
25782582 --------
@@ -2641,8 +2645,10 @@ class for all warnings. To check that no warning is returned,
26412645 msg = "Did not see expected warning of class {name!r}." .format (
26422646 name = expected_warning .__name__ )
26432647 assert saw_warning , msg
2644- assert not extra_warnings , ("Caused unexpected warning(s): {extra!r}."
2645- ).format (extra = extra_warnings )
2648+ if raise_on_extra_warnings and extra_warnings :
2649+ raise AssertionError (
2650+ "Caused unexpected warning(s): {!r}." .format (extra_warnings )
2651+ )
26462652
26472653
26482654class RNGContext (object ):
0 commit comments