Skip to content

Commit c51d21f

Browse files
committed
ENH: Allow tuple of Warnings in tm.assert_produces_warning()
1 parent 40a4d05 commit c51d21f

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

pandas/tests/sparse/frame/test_frame.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,23 +461,26 @@ def test_set_value(self):
461461

462462
# ok, as the index gets converted to object
463463
frame = self.frame.copy()
464-
with tm.assert_produces_warning(FutureWarning,
464+
with tm.assert_produces_warning((FutureWarning,
465+
PerformanceWarning),
465466
check_stacklevel=False):
466467
res = frame.set_value('foobar', 'B', 1.5)
467468
assert res.index.dtype == 'object'
468469

469470
res = self.frame
470471
res.index = res.index.astype(object)
471472

472-
with tm.assert_produces_warning(FutureWarning,
473+
with tm.assert_produces_warning((FutureWarning,
474+
PerformanceWarning),
473475
check_stacklevel=False):
474476
res = self.frame.set_value('foobar', 'B', 1.5)
475477
assert res.index[-1] == 'foobar'
476478
with tm.assert_produces_warning(FutureWarning,
477479
check_stacklevel=False):
478480
assert res.get_value('foobar', 'B') == 1.5
479481

480-
with tm.assert_produces_warning(FutureWarning,
482+
with tm.assert_produces_warning((FutureWarning,
483+
PerformanceWarning),
481484
check_stacklevel=False):
482485
res2 = res.set_value('foobar', 'qux', 1.5)
483486
tm.assert_index_equal(res2.columns,

pandas/tests/sparse/series/test_series.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,14 @@ def test_get_get_value(self):
487487
def test_set_value(self):
488488

489489
idx = self.btseries.index[7]
490-
with tm.assert_produces_warning(FutureWarning,
490+
with tm.assert_produces_warning((FutureWarning,
491+
PerformanceWarning),
491492
check_stacklevel=False):
492493
self.btseries.set_value(idx, 0)
493494
assert self.btseries[idx] == 0
494495

495-
with tm.assert_produces_warning(FutureWarning,
496+
with tm.assert_produces_warning((FutureWarning,
497+
PerformanceWarning),
496498
check_stacklevel=False):
497499
self.iseries.set_value('foobar', 0)
498500
assert self.iseries.index[-1] == 'foobar'

pandas/util/testing.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,9 +2437,10 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
24372437
24382438
Parameters
24392439
----------
2440-
expected_warning : {Warning, False, None}, default Warning
2440+
expected_warning : {Warning, tuple, False, None}, default Warning
24412441
The type of Exception raised. ``exception.Warning`` is the base
2442-
class for all warnings. To check that no warning is returned,
2442+
class for all warnings. To expect mltiple warnings, pass a tuple
2443+
of warning classes. To check that no warning is returned,
24432444
specify ``False`` or ``None``.
24442445
filter_level : str, default "always"
24452446
Specifies whether warnings are ignored, displayed, or turned
@@ -2486,6 +2487,11 @@ class for all warnings. To check that no warning is returned,
24862487
24872488
..warn:: This is *not* thread-safe.
24882489
"""
2490+
# Ensure a tuple
2491+
if (isinstance(expected_warning, type) and
2492+
issubclass(expected_warning, Warning)):
2493+
expected_warning = (expected_warning,)
2494+
24892495
with warnings.catch_warnings(record=True) as w:
24902496

24912497
if clear is not None:
@@ -2500,15 +2506,17 @@ class for all warnings. To check that no warning is returned,
25002506
except Exception:
25012507
pass
25022508

2503-
saw_warning = False
2509+
saw_warning = set()
25042510
warnings.simplefilter(filter_level)
25052511
yield w
25062512
extra_warnings = []
25072513

25082514
for actual_warning in w:
25092515
if (expected_warning and issubclass(actual_warning.category,
25102516
expected_warning)):
2511-
saw_warning = True
2517+
saw_warning.add(
2518+
next(w for w in expected_warning
2519+
if issubclass(actual_warning.category, w)))
25122520

25132521
if check_stacklevel and issubclass(actual_warning.category,
25142522
(FutureWarning,
@@ -2528,9 +2536,10 @@ class for all warnings. To check that no warning is returned,
25282536
actual_warning.filename,
25292537
actual_warning.lineno))
25302538
if expected_warning:
2531-
msg = "Did not see expected warning of class {name!r}.".format(
2532-
name=expected_warning.__name__)
2533-
assert saw_warning, msg
2539+
unseen = set(expected_warning) - saw_warning
2540+
msg = ("Did not see expected warning(s) of class: " +
2541+
', '.join(w.__name__ for w in unseen))
2542+
assert not unseen, msg
25342543
assert not extra_warnings, ("Caused unexpected warning(s): {extra!r}."
25352544
).format(extra=extra_warnings)
25362545

0 commit comments

Comments
 (0)