Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix _assert_caught_expected_warning typing not including tuple of w…
…arnings
  • Loading branch information
Jorewin committed Feb 24, 2024
commit a3b18abdb73807a8383231d624b48dee9102320f
19 changes: 12 additions & 7 deletions pandas/_testing/_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ class for all warnings. To raise multiple types of exceptions,
else tuple(match for i in range(len(expected_warning)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use (match,) * len(expected_warning) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

)
for warning_type, warning_match in zip(expected_warning, match):
_assert_caught_expected_warning(
_assert_caught_expected_warnings(
caught_warnings=w,
expected_warning=warning_type,
match=warning_match,
check_stacklevel=check_stacklevel,
)
else:
expected_warning = cast(type[Warning], expected_warning)
expected_warning = cast(type[Warning] | tuple[type[Warning], ...], expected_warning)
match = (
"|".join(m for m in match if m)
if isinstance(match, tuple)
else match
)
_assert_caught_expected_warning(
_assert_caught_expected_warnings(
caught_warnings=w,
expected_warning=expected_warning,
match=match,
Expand All @@ -150,17 +150,22 @@ def maybe_produces_warning(
return nullcontext()


def _assert_caught_expected_warning(
def _assert_caught_expected_warnings(
*,
caught_warnings: Sequence[warnings.WarningMessage],
expected_warning: type[Warning],
expected_warning: type[Warning] | tuple[type[Warning], ...],
match: str | None,
check_stacklevel: bool,
) -> None:
"""Assert that there was the expected warning among the caught warnings."""
saw_warning = False
matched_message = False
unmatched_messages = []
warning_name = (
tuple(x.__name__ for x in expected_warning)
if isinstance(expected_warning, tuple)
else expected_warning.__name__
)

for actual_warning in caught_warnings:
if issubclass(actual_warning.category, expected_warning):
Expand All @@ -177,12 +182,12 @@ def _assert_caught_expected_warning(

if not saw_warning:
raise AssertionError(
f"Did not see expected warning of class {expected_warning.__name__!r}"
f"Did not see expected warning of class {warning_name!r}"
)

if match and not matched_message:
raise AssertionError(
f"Did not see warning {expected_warning.__name__!r} "
f"Did not see warning {warning_name!r} "
f"matching '{match}'. The emitted warning messages are "
f"{unmatched_messages}"
)
Expand Down