Skip to content

TST: Added match argument for most uses of tm.assert_produces_warning #58396

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

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions doc/source/development/contributing_codebase.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,12 @@ is being raised, using ``pytest.raises`` instead.
Testing a warning
^^^^^^^^^^^^^^^^^

Use ``tm.assert_produces_warning`` as a context manager to check that a block of code raises a warning.
Use ``tm.assert_produces_warning`` as a context manager to check that a block of code raises a warning
and specify the warning message using the ``match`` argument.

.. code-block:: python

with tm.assert_produces_warning(DeprecationWarning):
with tm.assert_produces_warning(DeprecationWarning, match="the warning message"):
pd.deprecated_function()

If a warning should specifically not happen in a block of code, pass ``False`` into the context manager.
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/arrays/sparse/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def test_constructor_warns_when_losing_timezone(self):
dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific")

expected = SparseArray(np.asarray(dti, dtype="datetime64[ns]"))

with tm.assert_produces_warning(UserWarning):
msg = "loses timezone information"
with tm.assert_produces_warning(UserWarning, match=msg):
result = SparseArray(dti)

tm.assert_sp_array_equal(result, expected)

with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match=msg):
result = SparseArray(pd.Series(dti))

tm.assert_sp_array_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def test_to_period_2d(self, arr1d):
arr2d = arr1d.reshape(1, -1)

warn = None if arr1d.tz is None else UserWarning
with tm.assert_produces_warning(warn):
with tm.assert_produces_warning(warn, match="will drop timezone information"):
result = arr2d.to_period("D")
expected = arr1d.to_period("D").reshape(1, -1)
tm.assert_period_array_equal(result, expected)
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,8 @@ def test_performance_warning_for_poor_alignment(
else:
seen = False

with tm.assert_produces_warning(seen):
msg = "Alignment difference on axis 1 is larger than an order of magnitude"
with tm.assert_produces_warning(seen, match=msg):
pd.eval("df + s", engine=engine, parser=parser)

s = Series(np.random.default_rng(2).standard_normal(1000))
Expand All @@ -1036,7 +1037,7 @@ def test_performance_warning_for_poor_alignment(
else:
wrn = False

with tm.assert_produces_warning(wrn) as w:
with tm.assert_produces_warning(wrn, match=msg) as w:
pd.eval("df + s", engine=engine, parser=parser)

if not is_python_engine and performance_warning:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,5 +797,5 @@ def test_pandas_dtype_numpy_warning():

def test_pandas_dtype_ea_not_instance():
# GH 31356 GH 54592
with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="without any arguments"):
assert pandas_dtype(CategoricalDtype) == CategoricalDtype()
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_setattr_warnings():
# this should not raise a warning
df.two.not_an_index = [1, 2]

with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="doesn't allow columns"):
# warn when setting column to nonexistent name
df.four = df.two + 2
assert df.four.sum() > df.two.sum()
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def test_getitem_boolean(self, mixed_float_frame, mixed_int_frame, datetime_fram
# we are producing a warning that since the passed boolean
# key is not the same as the given index, we will reindex
# not sure this is really necessary
with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="will be reindexed"):
indexer_obj = indexer_obj.reindex(datetime_frame.index[::-1])
subframe_obj = datetime_frame[indexer_obj]
tm.assert_frame_equal(subframe_obj, subframe)
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ def test_setitem_npmatrix_2d(self):
df["np-array"] = a

# Instantiation of `np.matrix` gives PendingDeprecationWarning
with tm.assert_produces_warning(PendingDeprecationWarning):
with tm.assert_produces_warning(
PendingDeprecationWarning,
match="matrix subclass is not the recommended way to represent matrices",
):
df["np-matrix"] = np.matrix(a)

tm.assert_frame_equal(df, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_to_dict_not_unique_warning(self):
# GH#16927: When converting to a dict, if a column has a non-unique name
# it will be dropped, throwing a warning.
df = DataFrame([[1, 2, 3]], columns=["a", "a", "b"])
with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="columns will be omitted"):
df.to_dict()

@pytest.mark.filterwarnings("ignore::UserWarning")
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,25 +1097,24 @@ def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements):
and expr.USE_NUMEXPR
and switch_numexpr_min_elements == 0
):
warn = UserWarning # "evaluating in Python space because ..."
warn = UserWarning
else:
msg = (
f"cannot perform __{op.__name__}__ with this "
"index type: (DatetimeArray|TimedeltaArray)"
)

with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(warn):
with tm.assert_produces_warning(warn, match="evaluating in Python"):
op(df, elem.value)

elif (op, dtype) in skip:
if op in [operator.add, operator.mul]:
if expr.USE_NUMEXPR and switch_numexpr_min_elements == 0:
# "evaluating in Python space because ..."
warn = UserWarning
else:
warn = None
with tm.assert_produces_warning(warn):
with tm.assert_produces_warning(warn, match="evaluating in Python"):
op(df, elem.value)

else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def test_mode_sortwarning(self, using_infer_string):
expected = DataFrame({"A": ["a", np.nan]})

warning = None if using_infer_string else UserWarning
with tm.assert_produces_warning(warning):
with tm.assert_produces_warning(warning, match="Unable to sort modes"):
result = df.mode(dropna=False)
result = result.sort_values(by="A").reset_index(drop=True)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/base_class/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ def test_union_sort_other_incomparable(self):
# https://github.com/pandas-dev/pandas/issues/24959
idx = Index([1, pd.Timestamp("2000")])
# default (sort=None)
with tm.assert_produces_warning(RuntimeWarning):
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
result = idx.union(idx[:1])

tm.assert_index_equal(result, idx)

# sort=None
with tm.assert_produces_warning(RuntimeWarning):
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
result = idx.union(idx[:1], sort=None)
tm.assert_index_equal(result, idx)

Expand Down
20 changes: 7 additions & 13 deletions pandas/tests/indexes/datetimes/methods/test_to_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ def test_to_period_infer(self):
freq="5min",
)

with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
pi1 = rng.to_period("5min")

with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
pi2 = rng.to_period()

tm.assert_index_equal(pi1, pi2)
Expand All @@ -143,8 +143,7 @@ def test_to_period_millisecond(self):
]
)

with tm.assert_produces_warning(UserWarning):
# warning that timezone info will be lost
with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
period = index.to_period(freq="ms")
assert 2 == len(period)
assert period[0] == Period("2007-01-01 10:11:12.123Z", "ms")
Expand All @@ -158,8 +157,7 @@ def test_to_period_microsecond(self):
]
)

with tm.assert_produces_warning(UserWarning):
# warning that timezone info will be lost
with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
period = index.to_period(freq="us")
assert 2 == len(period)
assert period[0] == Period("2007-01-01 10:11:12.123456Z", "us")
Expand All @@ -172,19 +170,15 @@ def test_to_period_microsecond(self):
def test_to_period_tz(self, tz):
ts = date_range("1/1/2000", "2/1/2000", tz=tz)

with tm.assert_produces_warning(UserWarning):
# GH#21333 warning that timezone info will be lost
# filter warning about freq deprecation

with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
result = ts.to_period()[0]
expected = ts[0].to_period(ts.freq)

assert result == expected

expected = date_range("1/1/2000", "2/1/2000").to_period()

with tm.assert_produces_warning(UserWarning):
# GH#21333 warning that timezone info will be lost
with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
result = ts.to_period(ts.freq)

tm.assert_index_equal(result, expected)
Expand All @@ -193,7 +187,7 @@ def test_to_period_tz(self, tz):
def test_to_period_tz_utc_offset_consistency(self, tz):
# GH#22905
ts = date_range("1/1/2000", "2/1/2000", tz="Etc/GMT-1")
with tm.assert_produces_warning(UserWarning):
with tm.assert_produces_warning(UserWarning, match="drop timezone info"):
result = ts.to_period()[0]
expected = ts[0].to_period(ts.freq)
assert result == expected
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/multi/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def test_union_sort_other_incomparable():
idx = MultiIndex.from_product([[1, pd.Timestamp("2000")], ["a", "b"]])

# default, sort=None
with tm.assert_produces_warning(RuntimeWarning):
with tm.assert_produces_warning(RuntimeWarning, match="are unorderable"):
result = idx.union(idx[:1])
tm.assert_index_equal(result, idx)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,10 +1065,10 @@ def test_outer_join_sort(self):
left_index = Index(np.random.default_rng(2).permutation(15))
right_index = date_range("2020-01-01", periods=10)

with tm.assert_produces_warning(RuntimeWarning):
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
result = left_index.join(right_index, how="outer")

with tm.assert_produces_warning(RuntimeWarning):
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
expected = left_index.astype(object).union(right_index.astype(object))

tm.assert_index_equal(result, expected)
Expand Down
11 changes: 2 additions & 9 deletions pandas/tests/indexes/test_index_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,18 @@ def test_constructor_infer_nat_dt_like(
data = [ctor]
data.insert(pos, nulls_fixture)

warn = None
if nulls_fixture is NA:
expected = Index([NA, NaT])
mark = pytest.mark.xfail(reason="Broken with np.NaT ctor; see GH 31884")
request.applymarker(mark)
# GH#35942 numpy will emit a DeprecationWarning within the
# assert_index_equal calls. Since we can't do anything
# about it until GH#31884 is fixed, we suppress that warning.
warn = DeprecationWarning

result = Index(data)

with tm.assert_produces_warning(warn):
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected)

result = Index(np.array(data, dtype=object))

with tm.assert_produces_warning(warn):
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("swap_objs", [True, False])
def test_constructor_mixed_nat_objs_infers_object(self, swap_objs):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ def test_difference_incomparable(self, opname):
b = Index([2, Timestamp("1999"), 1])
op = operator.methodcaller(opname, b)

with tm.assert_produces_warning(RuntimeWarning):
with tm.assert_produces_warning(RuntimeWarning, match="not supported between"):
# sort=None, the default
result = op(a)
expected = Index([3, Timestamp("2000"), 2, Timestamp("1999")])
Expand Down
12 changes: 7 additions & 5 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,19 +1280,20 @@ def test_interval_can_hold_element(self, dtype, element):
# `elem` to not have the same length as `arr`
ii2 = IntervalIndex.from_breaks(arr[:-1], closed="neither")
elem = element(ii2)
with tm.assert_produces_warning(FutureWarning):
msg = "Setting an item of incompatible dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
self.check_series_setitem(elem, ii, False)
assert not blk._can_hold_element(elem)

ii3 = IntervalIndex.from_breaks([Timestamp(1), Timestamp(3), Timestamp(4)])
elem = element(ii3)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, match=msg):
self.check_series_setitem(elem, ii, False)
assert not blk._can_hold_element(elem)

ii4 = IntervalIndex.from_breaks([Timedelta(1), Timedelta(3), Timedelta(4)])
elem = element(ii4)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, match=msg):
self.check_series_setitem(elem, ii, False)
assert not blk._can_hold_element(elem)

Expand All @@ -1312,12 +1313,13 @@ def test_period_can_hold_element(self, element):
# `elem` to not have the same length as `arr`
pi2 = pi.asfreq("D")[:-1]
elem = element(pi2)
with tm.assert_produces_warning(FutureWarning):
msg = "Setting an item of incompatible dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
self.check_series_setitem(elem, pi, False)

dti = pi.to_timestamp("s")[:-1]
elem = element(dti)
with tm.assert_produces_warning(FutureWarning):
with tm.assert_produces_warning(FutureWarning, match=msg):
self.check_series_setitem(elem, pi, False)

def check_can_hold_element(self, obj, elem, inplace: bool):
Expand Down
29 changes: 15 additions & 14 deletions pandas/tests/io/formats/test_css.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,31 @@ def test_css_parse_normalisation(name, norm, abnorm):


@pytest.mark.parametrize(
"invalid_css,remainder",
"invalid_css,remainder,msg",
[
# No colon
("hello-world", ""),
("border-style: solid; hello-world", "border-style: solid"),
("hello-world", "", "expected a colon"),
("border-style: solid; hello-world", "border-style: solid", "expected a colon"),
(
"border-style: solid; hello-world; font-weight: bold",
"border-style: solid; font-weight: bold",
"expected a colon",
),
# Unclosed string fail
# Invalid size
("font-size: blah", "font-size: 1em"),
("font-size: 1a2b", "font-size: 1em"),
("font-size: 1e5pt", "font-size: 1em"),
("font-size: 1+6pt", "font-size: 1em"),
("font-size: 1unknownunit", "font-size: 1em"),
("font-size: 10", "font-size: 1em"),
("font-size: 10 pt", "font-size: 1em"),
("font-size: blah", "font-size: 1em", "Unhandled size"),
("font-size: 1a2b", "font-size: 1em", "Unhandled size"),
("font-size: 1e5pt", "font-size: 1em", "Unhandled size"),
("font-size: 1+6pt", "font-size: 1em", "Unhandled size"),
("font-size: 1unknownunit", "font-size: 1em", "Unhandled size"),
("font-size: 10", "font-size: 1em", "Unhandled size"),
("font-size: 10 pt", "font-size: 1em", "Unhandled size"),
# Too many args
("border-top: 1pt solid red green", "border-top: 1pt solid green"),
("border-top: 1pt solid red green", "border-top: 1pt solid green", "Too many"),
],
)
def test_css_parse_invalid(invalid_css, remainder):
with tm.assert_produces_warning(CSSWarning):
def test_css_parse_invalid(invalid_css, remainder, msg):
with tm.assert_produces_warning(CSSWarning, match=msg):
assert_same_resolution(invalid_css, remainder)


Expand Down Expand Up @@ -120,7 +121,7 @@ def test_css_side_shorthands(shorthand, expansions):
{top: "1pt", right: "4pt", bottom: "2pt", left: "0pt"},
)

with tm.assert_produces_warning(CSSWarning):
with tm.assert_produces_warning(CSSWarning, match="Could not expand"):
assert_resolves(f"{shorthand}: 1pt 1pt 1pt 1pt 1pt", {})


Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/test_to_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def test_css_to_excel_bad_colors(input_color):
if input_color is not None:
expected["fill"] = {"patternType": "solid"}

with tm.assert_produces_warning(CSSWarning):
with tm.assert_produces_warning(CSSWarning, match="Unhandled color format"):
convert = CSSToExcelConverter()
assert expected == convert(css)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/json/test_json_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def test_warns_non_roundtrippable_names(self, idx):
# GH 19130
df = DataFrame(index=idx)
df.index.name = "index"
with tm.assert_produces_warning():
with tm.assert_produces_warning(UserWarning, match="not round-trippable"):
set_default_names(df)

def test_timestamp_in_columns(self):
Expand Down
Loading
Loading