@@ -2392,9 +2392,10 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
2392
2392
2393
2393
Parameters
2394
2394
----------
2395
- expected_warning : {Warning, False, None}, default Warning
2395
+ expected_warning : {Warning, tuple, False, None}, default Warning
2396
2396
The type of Exception raised. ``exception.Warning`` is the base
2397
- class for all warnings. To check that no warning is returned,
2397
+ class for all warnings. To expect mltiple warnings, pass a tuple
2398
+ of warning classes. To check that no warning is returned,
2398
2399
specify ``False`` or ``None``.
2399
2400
filter_level : str, default "always"
2400
2401
Specifies whether warnings are ignored, displayed, or turned
@@ -2441,6 +2442,11 @@ class for all warnings. To check that no warning is returned,
2441
2442
2442
2443
..warn:: This is *not* thread-safe.
2443
2444
"""
2445
+ # Ensure a tuple
2446
+ if (isinstance (expected_warning , type ) and
2447
+ issubclass (expected_warning , Warning )):
2448
+ expected_warning = (expected_warning ,)
2449
+
2444
2450
with warnings .catch_warnings (record = True ) as w :
2445
2451
2446
2452
if clear is not None :
@@ -2455,15 +2461,17 @@ class for all warnings. To check that no warning is returned,
2455
2461
except Exception :
2456
2462
pass
2457
2463
2458
- saw_warning = False
2464
+ saw_warning = set ()
2459
2465
warnings .simplefilter (filter_level )
2460
2466
yield w
2461
2467
extra_warnings = []
2462
2468
2463
2469
for actual_warning in w :
2464
2470
if (expected_warning and issubclass (actual_warning .category ,
2465
2471
expected_warning )):
2466
- saw_warning = True
2472
+ saw_warning .add (
2473
+ next (w for w in expected_warning
2474
+ if issubclass (actual_warning .category , w )))
2467
2475
2468
2476
if check_stacklevel and issubclass (actual_warning .category ,
2469
2477
(FutureWarning ,
@@ -2480,9 +2488,10 @@ class for all warnings. To check that no warning is returned,
2480
2488
else :
2481
2489
extra_warnings .append (actual_warning .category .__name__ )
2482
2490
if expected_warning :
2483
- msg = "Did not see expected warning of class {name!r}." .format (
2484
- name = expected_warning .__name__ )
2485
- assert saw_warning , msg
2491
+ unseen = set (expected_warning ) - saw_warning
2492
+ msg = ("Did not see expected warning(s) of class: " +
2493
+ ', ' .join (w .__name__ for w in unseen ))
2494
+ assert not unseen , msg
2486
2495
assert not extra_warnings , ("Caused unexpected warning(s): {extra!r}."
2487
2496
).format (extra = extra_warnings )
2488
2497
0 commit comments