Skip to content

Commit 7fac8c8

Browse files
DOC: added type annotations and tests to check for multiple warning match (#47833)
* Testing * TST: added a test for multiple warnings * TYP: added type annotation Tuple[Type[Warning], ...] in assert_produces_warning function * TST: added a test for multiple warnings * TYP: Fixed Annotations * TST: Test for catching multiple warnings * TST: Test for catching multiple warnings * TST: Test for catching multiple warnings * TYP : fixed type annotations in _assert_caught_no_extra_warnings * TYP : updated type annotations * DOC : added issue as inline comment * DOC : added some documentation regarding type annotations * DOC : fixed pre-commit errors * fix missing whitespace Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent a046792 commit 7fac8c8

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

pandas/_testing/_warnings.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
@contextmanager
1919
def assert_produces_warning(
20-
expected_warning: type[Warning] | bool | None = Warning,
20+
expected_warning: type[Warning] | bool | tuple[type[Warning], ...] | None = Warning,
2121
filter_level: Literal[
2222
"error", "ignore", "always", "default", "module", "once"
2323
] = "always",
@@ -26,16 +26,17 @@ def assert_produces_warning(
2626
match: str | None = None,
2727
):
2828
"""
29-
Context manager for running code expected to either raise a specific
30-
warning, or not raise any warnings. Verifies that the code raises the
31-
expected warning, and that it does not raise any other unexpected
29+
Context manager for running code expected to either raise a specific warning,
30+
multiple specific warnings, or not raise any warnings. Verifies that the code
31+
raises the expected warning(s), and that it does not raise any other unexpected
3232
warnings. It is basically a wrapper around ``warnings.catch_warnings``.
3333
3434
Parameters
3535
----------
36-
expected_warning : {Warning, False, None}, default Warning
36+
expected_warning : {Warning, False, tuple[Warning, ...], None}, default Warning
3737
The type of Exception raised. ``exception.Warning`` is the base
38-
class for all warnings. To check that no warning is returned,
38+
class for all warnings. To raise multiple types of exceptions,
39+
pass them as a tuple. To check that no warning is returned,
3940
specify ``False`` or ``None``.
4041
filter_level : str or None, default "always"
4142
Specifies whether warnings are ignored, displayed, or turned
@@ -157,7 +158,7 @@ def _assert_caught_expected_warning(
157158
def _assert_caught_no_extra_warnings(
158159
*,
159160
caught_warnings: Sequence[warnings.WarningMessage],
160-
expected_warning: type[Warning] | bool | None,
161+
expected_warning: type[Warning] | bool | tuple[type[Warning], ...] | None,
161162
) -> None:
162163
"""Assert that no extra warnings apart from the expected ones are caught."""
163164
extra_warnings = []
@@ -195,7 +196,7 @@ def _assert_caught_no_extra_warnings(
195196

196197
def _is_unexpected_warning(
197198
actual_warning: warnings.WarningMessage,
198-
expected_warning: type[Warning] | bool | None,
199+
expected_warning: type[Warning] | bool | tuple[type[Warning], ...] | None,
199200
) -> bool:
200201
"""Check if the actual warning issued is unexpected."""
201202
if actual_warning and not expected_warning:

pandas/tests/util/test_assert_produces_warning.py

+8
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ def test_same_category_different_messages_last_match():
179179
warnings.warn("Match this", category)
180180

181181

182+
def test_match_multiple_warnings():
183+
# https://github.com/pandas-dev/pandas/issues/47829
184+
category = (FutureWarning, UserWarning)
185+
with tm.assert_produces_warning(category, match=r"^Match this"):
186+
warnings.warn("Match this", FutureWarning)
187+
warnings.warn("Match this too", UserWarning)
188+
189+
182190
def test_right_category_wrong_match_raises(pair_different_warnings):
183191
target_category, other_category = pair_different_warnings
184192
with pytest.raises(AssertionError, match="Did not see warning.*matching"):

0 commit comments

Comments
 (0)