@@ -2536,7 +2536,8 @@ def exception_matches(self, exc_type, exc_value, trace_back):
2536
2536
2537
2537
@contextmanager
2538
2538
def 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 ):
2540
2541
"""
2541
2542
Context manager for running code expected to either raise a specific
2542
2543
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",
2549
2550
The type of Exception raised. ``exception.Warning`` is the base
2550
2551
class for all warnings. To check that no warning is returned,
2551
2552
specify ``False`` or ``None``.
2552
- filter_level : str, default "always"
2553
+ filter_level : str or None , default "always"
2553
2554
Specifies whether warnings are ignored, displayed, or turned
2554
2555
into errors.
2555
2556
Valid values are:
@@ -2573,6 +2574,9 @@ class for all warnings. To check that no warning is returned,
2573
2574
If True, displays the line that called the function containing
2574
2575
the warning to show were the function is called. Otherwise, the
2575
2576
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.
2576
2580
2577
2581
Examples
2578
2582
--------
@@ -2641,8 +2645,10 @@ class for all warnings. To check that no warning is returned,
2641
2645
msg = "Did not see expected warning of class {name!r}." .format (
2642
2646
name = expected_warning .__name__ )
2643
2647
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
+ )
2646
2652
2647
2653
2648
2654
class RNGContext (object ):
0 commit comments