@@ -2571,7 +2571,8 @@ def exception_matches(self, exc_type, exc_value, trace_back):
2571
2571
2572
2572
@contextmanager
2573
2573
def assert_produces_warning (expected_warning = Warning , filter_level = "always" ,
2574
- clear = None , check_stacklevel = True ):
2574
+ clear = None , check_stacklevel = True ,
2575
+ raise_on_extra_warnings = True ):
2575
2576
"""
2576
2577
Context manager for running code expected to either raise a specific
2577
2578
warning, or not raise any warnings. Verifies that the code raises the
@@ -2584,7 +2585,7 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
2584
2585
The type of Exception raised. ``exception.Warning`` is the base
2585
2586
class for all warnings. To check that no warning is returned,
2586
2587
specify ``False`` or ``None``.
2587
- filter_level : str, default "always"
2588
+ filter_level : str or None , default "always"
2588
2589
Specifies whether warnings are ignored, displayed, or turned
2589
2590
into errors.
2590
2591
Valid values are:
@@ -2608,6 +2609,9 @@ class for all warnings. To check that no warning is returned,
2608
2609
If True, displays the line that called the function containing
2609
2610
the warning to show were the function is called. Otherwise, the
2610
2611
line that implements the function is displayed.
2612
+ raise_on_extra_warnings : bool, default True
2613
+ Whether extra warnings not of the type `expected_warning` should
2614
+ cause the test to fail.
2611
2615
2612
2616
Examples
2613
2617
--------
@@ -2676,8 +2680,10 @@ class for all warnings. To check that no warning is returned,
2676
2680
msg = "Did not see expected warning of class {name!r}." .format (
2677
2681
name = expected_warning .__name__ )
2678
2682
assert saw_warning , msg
2679
- assert not extra_warnings , ("Caused unexpected warning(s): {extra!r}."
2680
- ).format (extra = extra_warnings )
2683
+ if raise_on_extra_warnings and extra_warnings :
2684
+ raise AssertionError (
2685
+ "Caused unexpected warning(s): {!r}." .format (extra_warnings )
2686
+ )
2681
2687
2682
2688
2683
2689
class RNGContext (object ):
0 commit comments