Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add all warnings check to the assert_produces_warnings, and separate messages for each warning. #57222

Merged
merged 18 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions pandas/_testing/_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def assert_produces_warning(
] = "always",
check_stacklevel: bool = True,
raise_on_extra_warnings: bool = True,
match: str | None = None,
match: str | tuple[str | None, ...] | None = None,
must_find_all_warnings: bool = True,
) -> Generator[list[warnings.WarningMessage], None, None]:
"""
Context manager for running code expected to either raise a specific warning,
Expand Down Expand Up @@ -68,8 +69,15 @@ class for all warnings. To raise multiple types of exceptions,
raise_on_extra_warnings : bool, default True
Whether extra warnings not of the type `expected_warning` should
cause the test to fail.
match : str, optional
Match warning message.
match : {str, tuple[str, ...]}, optional
Match warning message. If it's a tuple, it has to be the size of
`expected_warning`. If additionally `must_find_all_warnings` is
True, each expected warning's message gets matched with a respective
match. Otherwise, multiple values get treated as an alternative.
must_find_all_warnings : bool, default True
If True and `expected_warning` is a tuple, each expected warning
type must get encountered. Otherwise, even one expected warning
results in success.

Examples
--------
Expand Down Expand Up @@ -97,13 +105,32 @@ class for all warnings. To raise multiple types of exceptions,
yield w
finally:
if expected_warning:
expected_warning = cast(type[Warning], expected_warning)
_assert_caught_expected_warning(
caught_warnings=w,
expected_warning=expected_warning,
match=match,
check_stacklevel=check_stacklevel,
)
if isinstance(expected_warning, tuple) and must_find_all_warnings:
match = (
match
if isinstance(match, tuple)
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(
caught_warnings=w,
expected_warning=warning_type,
match=warning_match,
check_stacklevel=check_stacklevel,
)
else:
expected_warning = cast(type[Warning], 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.

This can still be a tuple, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you're right. I believe this was originally done, to suppress _assert_caught_expected_warning not accepting tuples. I changed that and fixed some issues that arose because of that now.

match = (
"|".join(m for m in match if m)
if isinstance(match, tuple)
else match
)
_assert_caught_expected_warning(
caught_warnings=w,
expected_warning=expected_warning,
match=match,
check_stacklevel=check_stacklevel,
)
if raise_on_extra_warnings:
_assert_caught_no_extra_warnings(
caught_warnings=w,
Expand Down
4 changes: 2 additions & 2 deletions pandas/_testing/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()
elif PYPY and extra_warnings:
return assert_produces_warning(
extra_warnings,
match="|".join(extra_match),
match=extra_match,
)
else:
if using_copy_on_write():
Expand All @@ -226,5 +226,5 @@ def raises_chained_assignment_error(warn=True, extra_warnings=(), extra_match=()
warning = (warning, *extra_warnings) # type: ignore[assignment]
return assert_produces_warning(
warning,
match="|".join((match, *extra_match)),
match=(match, *extra_match),
)
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def test_isna(self, data_missing):
tm.assert_equal(sarr.isna(), expected)

def test_fillna_limit_backfill(self, data_missing):
warns = (PerformanceWarning, FutureWarning)
warns = FutureWarning
with tm.assert_produces_warning(warns, check_stacklevel=False):
super().test_fillna_limit_backfill(data_missing)

Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/indexing/test_chaining_and_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ def test_detect_chained_assignment_changing_dtype(self):
with tm.raises_chained_assignment_error():
df.loc[2]["C"] = "foo"
tm.assert_frame_equal(df, df_original)
with tm.raises_chained_assignment_error(extra_warnings=(FutureWarning,)):
with tm.raises_chained_assignment_error(
extra_warnings=(FutureWarning,), extra_match=(None,)
):
df["C"][2] = "foo"
tm.assert_frame_equal(df, df_original)

Expand Down
2 changes: 0 additions & 2 deletions pandas/tests/io/parser/common/test_read_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ def test_warn_bad_lines(all_parsers):
expected_warning = ParserWarning
if parser.engine == "pyarrow":
match_msg = "Expected 1 columns, but found 3: 1,2,3"
expected_warning = (ParserWarning, DeprecationWarning)

with tm.assert_produces_warning(
expected_warning, match=match_msg, check_stacklevel=False
Expand Down Expand Up @@ -311,7 +310,6 @@ def test_on_bad_lines_warn_correct_formatting(all_parsers):
expected_warning = ParserWarning
if parser.engine == "pyarrow":
match_msg = "Expected 2 columns, but found 3: a,b,c"
expected_warning = (ParserWarning, DeprecationWarning)

with tm.assert_produces_warning(
expected_warning, match=match_msg, check_stacklevel=False
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def test_multiple_date_col(all_parsers, keep_date_col, request):
"names": ["X0", "X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8"],
}
with tm.assert_produces_warning(
(DeprecationWarning, FutureWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(StringIO(data), **kwds)

Expand Down Expand Up @@ -724,7 +724,7 @@ def test_multiple_date_col_name_collision(all_parsers, data, parse_dates, msg):
)
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
parser.read_csv(StringIO(data), parse_dates=parse_dates)

Expand Down Expand Up @@ -1248,14 +1248,14 @@ def test_multiple_date_col_named_index_compat(all_parsers):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
with_indices = parser.read_csv(
StringIO(data), parse_dates={"nominal": [1, 2]}, index_col="nominal"
)

with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
with_names = parser.read_csv(
StringIO(data),
Expand All @@ -1280,13 +1280,13 @@ def test_multiple_date_col_multiple_index_compat(all_parsers):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(data), index_col=["nominal", "ID"], parse_dates={"nominal": [1, 2]}
)
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
expected = parser.read_csv(StringIO(data), parse_dates={"nominal": [1, 2]})

Expand Down Expand Up @@ -2267,7 +2267,7 @@ def test_parse_dates_dict_format_two_columns(all_parsers, key, parse_dates):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(data), date_format={key: "%d- %m-%Y"}, parse_dates=parse_dates
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/parser/usecols/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_usecols_with_parse_dates4(all_parsers):
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(data),
Expand Down Expand Up @@ -186,7 +186,7 @@ def test_usecols_with_parse_dates_and_names(all_parsers, usecols, names, request
"Support for nested sequences for 'parse_dates' in pd.read_csv is deprecated"
)
with tm.assert_produces_warning(
(FutureWarning, DeprecationWarning), match=depr_msg, check_stacklevel=False
FutureWarning, match=depr_msg, check_stacklevel=False
):
result = parser.read_csv(
StringIO(s), names=names, parse_dates=parse_dates, usecols=usecols
Expand Down
Loading