Skip to content

Commit 199fe5d

Browse files
ivanovmgluckyvs1
authored andcommitted
REF: simplify logic in assert_produces_warning (pandas-dev#38637)
1 parent a8489a1 commit 199fe5d

File tree

1 file changed

+77
-40
lines changed

1 file changed

+77
-40
lines changed

pandas/_testing.py

+77-40
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ContextManager,
1717
List,
1818
Optional,
19+
Sequence,
1920
Tuple,
2021
Type,
2122
Union,
@@ -2715,65 +2716,101 @@ class for all warnings. To check that no warning is returned,
27152716
__tracebackhide__ = True
27162717

27172718
with warnings.catch_warnings(record=True) as w:
2718-
2719-
saw_warning = False
2720-
matched_message = False
2721-
27222719
warnings.simplefilter(filter_level)
27232720
yield w
2724-
extra_warnings = []
27252721

2726-
for actual_warning in w:
2722+
if expected_warning:
27272723
expected_warning = cast(Type[Warning], expected_warning)
2728-
if expected_warning and issubclass(
2729-
actual_warning.category, expected_warning
2724+
_assert_caught_expected_warning(
2725+
caught_warnings=w,
2726+
expected_warning=expected_warning,
2727+
match=match,
2728+
check_stacklevel=check_stacklevel,
2729+
)
2730+
2731+
if raise_on_extra_warnings:
2732+
_assert_caught_no_extra_warnings(
2733+
caught_warnings=w,
2734+
expected_warning=expected_warning,
2735+
)
2736+
2737+
2738+
def _assert_caught_expected_warning(
2739+
*,
2740+
caught_warnings: Sequence[warnings.WarningMessage],
2741+
expected_warning: Type[Warning],
2742+
match: Optional[str],
2743+
check_stacklevel: bool,
2744+
) -> None:
2745+
"""Assert that there was the expected warning among the caught warnings."""
2746+
saw_warning = False
2747+
matched_message = False
2748+
2749+
for actual_warning in caught_warnings:
2750+
if issubclass(actual_warning.category, expected_warning):
2751+
saw_warning = True
2752+
2753+
if check_stacklevel and issubclass(
2754+
actual_warning.category, (FutureWarning, DeprecationWarning)
27302755
):
2731-
saw_warning = True
2756+
_assert_raised_with_correct_stacklevel(actual_warning)
27322757

2733-
if check_stacklevel and issubclass(
2734-
actual_warning.category, (FutureWarning, DeprecationWarning)
2735-
):
2736-
_assert_raised_with_correct_stacklevel(actual_warning)
2758+
if match is not None and re.search(match, str(actual_warning.message)):
2759+
matched_message = True
27372760

2738-
if match is not None and re.search(match, str(actual_warning.message)):
2739-
matched_message = True
2761+
if not saw_warning:
2762+
raise AssertionError(
2763+
f"Did not see expected warning of class "
2764+
f"{repr(expected_warning.__name__)}"
2765+
)
27402766

2741-
else:
2742-
extra_warnings.append(
2743-
(
2744-
actual_warning.category.__name__,
2745-
actual_warning.message,
2746-
actual_warning.filename,
2747-
actual_warning.lineno,
2748-
)
2749-
)
2767+
if match and not matched_message:
2768+
raise AssertionError(
2769+
f"Did not see warning {repr(expected_warning.__name__)} "
2770+
f"matching {match}"
2771+
)
27502772

2751-
if expected_warning:
2752-
expected_warning = cast(Type[Warning], expected_warning)
2753-
if not saw_warning:
2754-
raise AssertionError(
2755-
f"Did not see expected warning of class "
2756-
f"{repr(expected_warning.__name__)}"
2757-
)
27582773

2759-
if match and not matched_message:
2760-
raise AssertionError(
2761-
f"Did not see warning {repr(expected_warning.__name__)} "
2762-
f"matching {match}"
2774+
def _assert_caught_no_extra_warnings(
2775+
*,
2776+
caught_warnings: Sequence[warnings.WarningMessage],
2777+
expected_warning: Optional[Union[Type[Warning], bool]],
2778+
) -> None:
2779+
"""Assert that no extra warnings apart from the expected ones are caught."""
2780+
extra_warnings = []
2781+
2782+
for actual_warning in caught_warnings:
2783+
if _is_unexpected_warning(actual_warning, expected_warning):
2784+
extra_warnings.append(
2785+
(
2786+
actual_warning.category.__name__,
2787+
actual_warning.message,
2788+
actual_warning.filename,
2789+
actual_warning.lineno,
27632790
)
2764-
2765-
if raise_on_extra_warnings and extra_warnings:
2766-
raise AssertionError(
2767-
f"Caused unexpected warning(s): {repr(extra_warnings)}"
27682791
)
27692792

2793+
if extra_warnings:
2794+
raise AssertionError(f"Caused unexpected warning(s): {repr(extra_warnings)}")
2795+
2796+
2797+
def _is_unexpected_warning(
2798+
actual_warning: warnings.WarningMessage,
2799+
expected_warning: Optional[Union[Type[Warning], bool]],
2800+
) -> bool:
2801+
"""Check if the actual warning issued is unexpected."""
2802+
if actual_warning and not expected_warning:
2803+
return True
2804+
expected_warning = cast(Type[Warning], expected_warning)
2805+
return bool(not issubclass(actual_warning.category, expected_warning))
2806+
27702807

27712808
def _assert_raised_with_correct_stacklevel(
27722809
actual_warning: warnings.WarningMessage,
27732810
) -> None:
27742811
from inspect import getframeinfo, stack
27752812

2776-
caller = getframeinfo(stack()[3][0])
2813+
caller = getframeinfo(stack()[4][0])
27772814
msg = (
27782815
"Warning not set with correct stacklevel. "
27792816
f"File where warning is raised: {actual_warning.filename} != "

0 commit comments

Comments
 (0)