Skip to content

Commit ba60432

Browse files
authored
TST: Added match argument for most uses of tm.assert_produces_warning (#58396)
* Fix for all FutureWarnings * Add match for most warnings * Cleaner code --------- Co-authored-by: Abdulaziz Aloqeely <[email protected]>
1 parent 9e7565a commit ba60432

39 files changed

+130
-116
lines changed

doc/source/development/contributing_codebase.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,12 @@ is being raised, using ``pytest.raises`` instead.
557557
Testing a warning
558558
^^^^^^^^^^^^^^^^^
559559

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

562563
.. code-block:: python
563564
564-
with tm.assert_produces_warning(DeprecationWarning):
565+
with tm.assert_produces_warning(DeprecationWarning, match="the warning message"):
565566
pd.deprecated_function()
566567
567568
If a warning should specifically not happen in a block of code, pass ``False`` into the context manager.

pandas/tests/arrays/sparse/test_constructors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ def test_constructor_warns_when_losing_timezone(self):
9090
dti = pd.date_range("2016-01-01", periods=3, tz="US/Pacific")
9191

9292
expected = SparseArray(np.asarray(dti, dtype="datetime64[ns]"))
93-
94-
with tm.assert_produces_warning(UserWarning):
93+
msg = "loses timezone information"
94+
with tm.assert_produces_warning(UserWarning, match=msg):
9595
result = SparseArray(dti)
9696

9797
tm.assert_sp_array_equal(result, expected)
9898

99-
with tm.assert_produces_warning(UserWarning):
99+
with tm.assert_produces_warning(UserWarning, match=msg):
100100
result = SparseArray(pd.Series(dti))
101101

102102
tm.assert_sp_array_equal(result, expected)

pandas/tests/arrays/test_datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ def test_to_period_2d(self, arr1d):
778778
arr2d = arr1d.reshape(1, -1)
779779

780780
warn = None if arr1d.tz is None else UserWarning
781-
with tm.assert_produces_warning(warn):
781+
with tm.assert_produces_warning(warn, match="will drop timezone information"):
782782
result = arr2d.to_period("D")
783783
expected = arr1d.to_period("D").reshape(1, -1)
784784
tm.assert_period_array_equal(result, expected)

pandas/tests/computation/test_eval.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,8 @@ def test_performance_warning_for_poor_alignment(
10141014
else:
10151015
seen = False
10161016

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

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

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

10421043
if not is_python_engine and performance_warning:

pandas/tests/dtypes/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,5 +797,5 @@ def test_pandas_dtype_numpy_warning():
797797

798798
def test_pandas_dtype_ea_not_instance():
799799
# GH 31356 GH 54592
800-
with tm.assert_produces_warning(UserWarning):
800+
with tm.assert_produces_warning(UserWarning, match="without any arguments"):
801801
assert pandas_dtype(CategoricalDtype) == CategoricalDtype()

pandas/tests/dtypes/test_generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_setattr_warnings():
124124
# this should not raise a warning
125125
df.two.not_an_index = [1, 2]
126126

127-
with tm.assert_produces_warning(UserWarning):
127+
with tm.assert_produces_warning(UserWarning, match="doesn't allow columns"):
128128
# warn when setting column to nonexistent name
129129
df.four = df.two + 2
130130
assert df.four.sum() > df.two.sum()

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_getitem_boolean(self, mixed_float_frame, mixed_int_frame, datetime_fram
145145
# we are producing a warning that since the passed boolean
146146
# key is not the same as the given index, we will reindex
147147
# not sure this is really necessary
148-
with tm.assert_produces_warning(UserWarning):
148+
with tm.assert_produces_warning(UserWarning, match="will be reindexed"):
149149
indexer_obj = indexer_obj.reindex(datetime_frame.index[::-1])
150150
subframe_obj = datetime_frame[indexer_obj]
151151
tm.assert_frame_equal(subframe_obj, subframe)

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,10 @@ def test_setitem_npmatrix_2d(self):
711711
df["np-array"] = a
712712

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

717720
tm.assert_frame_equal(df, expected)

pandas/tests/frame/methods/test_to_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_to_dict_not_unique_warning(self):
166166
# GH#16927: When converting to a dict, if a column has a non-unique name
167167
# it will be dropped, throwing a warning.
168168
df = DataFrame([[1, 2, 3]], columns=["a", "a", "b"])
169-
with tm.assert_produces_warning(UserWarning):
169+
with tm.assert_produces_warning(UserWarning, match="columns will be omitted"):
170170
df.to_dict()
171171

172172
@pytest.mark.filterwarnings("ignore::UserWarning")

pandas/tests/frame/test_arithmetic.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,25 +1097,24 @@ def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements):
10971097
and expr.USE_NUMEXPR
10981098
and switch_numexpr_min_elements == 0
10991099
):
1100-
warn = UserWarning # "evaluating in Python space because ..."
1100+
warn = UserWarning
11011101
else:
11021102
msg = (
11031103
f"cannot perform __{op.__name__}__ with this "
11041104
"index type: (DatetimeArray|TimedeltaArray)"
11051105
)
11061106

11071107
with pytest.raises(TypeError, match=msg):
1108-
with tm.assert_produces_warning(warn):
1108+
with tm.assert_produces_warning(warn, match="evaluating in Python"):
11091109
op(df, elem.value)
11101110

11111111
elif (op, dtype) in skip:
11121112
if op in [operator.add, operator.mul]:
11131113
if expr.USE_NUMEXPR and switch_numexpr_min_elements == 0:
1114-
# "evaluating in Python space because ..."
11151114
warn = UserWarning
11161115
else:
11171116
warn = None
1118-
with tm.assert_produces_warning(warn):
1117+
with tm.assert_produces_warning(warn, match="evaluating in Python"):
11191118
op(df, elem.value)
11201119

11211120
else:

0 commit comments

Comments
 (0)