Skip to content

Commit 5012bcc

Browse files
committed
ENH: Allow tuple of Warnings in tm.assert_produces_warning()
1 parent a7f24cb commit 5012bcc

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

pandas/tests/sparse/frame/test_frame.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -449,23 +449,26 @@ def test_set_value(self):
449449

450450
# ok, as the index gets converted to object
451451
frame = self.frame.copy()
452-
with tm.assert_produces_warning(FutureWarning,
452+
with tm.assert_produces_warning((FutureWarning,
453+
PerformanceWarning),
453454
check_stacklevel=False):
454455
res = frame.set_value('foobar', 'B', 1.5)
455456
assert res.index.dtype == 'object'
456457

457458
res = self.frame
458459
res.index = res.index.astype(object)
459460

460-
with tm.assert_produces_warning(FutureWarning,
461+
with tm.assert_produces_warning((FutureWarning,
462+
PerformanceWarning),
461463
check_stacklevel=False):
462464
res = self.frame.set_value('foobar', 'B', 1.5)
463465
assert res.index[-1] == 'foobar'
464466
with tm.assert_produces_warning(FutureWarning,
465467
check_stacklevel=False):
466468
assert res.get_value('foobar', 'B') == 1.5
467469

468-
with tm.assert_produces_warning(FutureWarning,
470+
with tm.assert_produces_warning((FutureWarning,
471+
PerformanceWarning),
469472
check_stacklevel=False):
470473
res2 = res.set_value('foobar', 'qux', 1.5)
471474
tm.assert_index_equal(res2.columns,

pandas/tests/sparse/series/test_series.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,14 @@ def test_get_get_value(self):
477477
def test_set_value(self):
478478

479479
idx = self.btseries.index[7]
480-
with tm.assert_produces_warning(FutureWarning,
480+
with tm.assert_produces_warning((FutureWarning,
481+
PerformanceWarning),
481482
check_stacklevel=False):
482483
self.btseries.set_value(idx, 0)
483484
assert self.btseries[idx] == 0
484485

485-
with tm.assert_produces_warning(FutureWarning,
486+
with tm.assert_produces_warning((FutureWarning,
487+
PerformanceWarning),
486488
check_stacklevel=False):
487489
self.iseries.set_value('foobar', 0)
488490
assert self.iseries.index[-1] == 'foobar'

pandas/util/testing.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -2392,9 +2392,10 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
23922392
23932393
Parameters
23942394
----------
2395-
expected_warning : {Warning, False, None}, default Warning
2395+
expected_warning : {Warning, tuple, False, None}, default Warning
23962396
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,
23982399
specify ``False`` or ``None``.
23992400
filter_level : str, default "always"
24002401
Specifies whether warnings are ignored, displayed, or turned
@@ -2441,6 +2442,11 @@ class for all warnings. To check that no warning is returned,
24412442
24422443
..warn:: This is *not* thread-safe.
24432444
"""
2445+
# Ensure a tuple
2446+
if (isinstance(expected_warning, type) and
2447+
issubclass(expected_warning, Warning)):
2448+
expected_warning = (expected_warning,)
2449+
24442450
with warnings.catch_warnings(record=True) as w:
24452451

24462452
if clear is not None:
@@ -2455,15 +2461,17 @@ class for all warnings. To check that no warning is returned,
24552461
except Exception:
24562462
pass
24572463

2458-
saw_warning = False
2464+
saw_warning = set()
24592465
warnings.simplefilter(filter_level)
24602466
yield w
24612467
extra_warnings = []
24622468

24632469
for actual_warning in w:
24642470
if (expected_warning and issubclass(actual_warning.category,
24652471
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)))
24672475

24682476
if check_stacklevel and issubclass(actual_warning.category,
24692477
(FutureWarning,
@@ -2480,9 +2488,10 @@ class for all warnings. To check that no warning is returned,
24802488
else:
24812489
extra_warnings.append(actual_warning.category.__name__)
24822490
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
24862495
assert not extra_warnings, ("Caused unexpected warning(s): {extra!r}."
24872496
).format(extra=extra_warnings)
24882497

0 commit comments

Comments
 (0)