Skip to content

Commit fd5422f

Browse files
authored
TST: GH30999 Add match=msg to all pytest.raises in tests/indexes (#38697)
1 parent e989e31 commit fd5422f

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

pandas/tests/indexes/multi/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def test_get_loc_duplicates(self):
526526
xp = 0
527527
assert rs == xp
528528

529-
with pytest.raises(KeyError):
529+
with pytest.raises(KeyError, match="2"):
530530
index.get_loc(2)
531531

532532
def test_get_loc_level(self):

pandas/tests/indexes/multi/test_setops.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,12 @@ def test_difference_sort_incomparable():
221221
tm.assert_index_equal(result, idx)
222222

223223

224-
@pytest.mark.xfail(reason="Not implemented.")
225224
def test_difference_sort_incomparable_true():
226-
# TODO decide on True behaviour
227-
# # sort=True, raises
228225
idx = pd.MultiIndex.from_product([[1, pd.Timestamp("2000"), 2], ["a", "b"]])
229226
other = pd.MultiIndex.from_product([[3, pd.Timestamp("2000"), 4], ["c", "d"]])
230227

231-
with pytest.raises(TypeError):
228+
msg = "The 'sort' keyword only takes the values of None or False; True was passed."
229+
with pytest.raises(ValueError, match=msg):
232230
idx.difference(other, sort=True)
233231

234232

pandas/tests/indexes/period/test_indexing.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,15 @@ def test_get_indexer_mismatched_dtype_with_method(self, non_comparable_idx, meth
487487
other2 = other.astype(dtype)
488488
if dtype == "object" and isinstance(other, PeriodIndex):
489489
continue
490-
# For object dtype we are liable to get a different exception message
491-
with pytest.raises(TypeError):
490+
# Two different error message patterns depending on dtypes
491+
msg = "|".join(
492+
re.escape(msg)
493+
for msg in (
494+
f"Cannot compare dtypes {pi.dtype} and {other.dtype}",
495+
" not supported between instances of ",
496+
)
497+
)
498+
with pytest.raises(TypeError, match=msg):
492499
pi.get_indexer(other2, method=method)
493500

494501
def test_get_indexer_non_unique(self):

pandas/tests/indexes/test_common.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ def test_droplevel(self, index):
3939
if isinstance(index.name, tuple) and level is index.name:
4040
# GH 21121 : droplevel with tuple name
4141
continue
42-
with pytest.raises(ValueError):
42+
msg = (
43+
"Cannot remove 1 levels from an index with 1 levels: at least one "
44+
"level must be left."
45+
)
46+
with pytest.raises(ValueError, match=msg):
4347
index.droplevel(level)
4448

4549
for level in "wrong", ["wrong"]:
@@ -77,7 +81,11 @@ def test_constructor_unwraps_index(self, index):
7781
# FutureWarning from non-tuple sequence of nd indexing
7882
@pytest.mark.filterwarnings("ignore::FutureWarning")
7983
def test_getitem_error(self, index, itm):
80-
with pytest.raises(IndexError):
84+
msg = r"index 101 is out of bounds for axis 0 with size [\d]+|" + re.escape(
85+
"only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) "
86+
"and integer or boolean arrays are valid indices"
87+
)
88+
with pytest.raises(IndexError, match=msg):
8189
index[itm]
8290

8391
def test_to_flat_index(self, index):
@@ -249,7 +257,8 @@ def test_searchsorted_monotonic(self, index):
249257
assert expected_right == ssm_right
250258
else:
251259
# non-monotonic should raise.
252-
with pytest.raises(ValueError):
260+
msg = "index must be monotonic increasing or decreasing"
261+
with pytest.raises(ValueError, match=msg):
253262
index._searchsorted_monotonic(value, side="left")
254263

255264
def test_pickle(self, index):

pandas/tests/indexes/test_numpy_compat.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def test_numpy_ufuncs_basic(index, func):
4949
# https://numpy.org/doc/stable/reference/ufuncs.html
5050

5151
if isinstance(index, DatetimeIndexOpsMixin):
52-
# raise TypeError or ValueError (PeriodIndex)
53-
with pytest.raises(Exception):
52+
with tm.external_error_raised((TypeError, AttributeError)):
5453
with np.errstate(all="ignore"):
5554
func(index)
5655
elif isinstance(index, (Float64Index, Int64Index, UInt64Index)):
@@ -66,7 +65,7 @@ def test_numpy_ufuncs_basic(index, func):
6665
if len(index) == 0:
6766
pass
6867
else:
69-
with pytest.raises(Exception):
68+
with tm.external_error_raised((TypeError, AttributeError)):
7069
with np.errstate(all="ignore"):
7170
func(index)
7271

@@ -77,7 +76,6 @@ def test_numpy_ufuncs_basic(index, func):
7776
def test_numpy_ufuncs_other(index, func, request):
7877
# test ufuncs of numpy, see:
7978
# https://numpy.org/doc/stable/reference/ufuncs.html
80-
8179
if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
8280
if isinstance(index, DatetimeIndex) and index.tz is not None:
8381
if func in [np.isfinite, np.isnan, np.isinf]:
@@ -96,13 +94,11 @@ def test_numpy_ufuncs_other(index, func, request):
9694
result = func(index)
9795
assert isinstance(result, np.ndarray)
9896
else:
99-
# raise TypeError or ValueError (PeriodIndex)
100-
with pytest.raises(Exception):
97+
with tm.external_error_raised(TypeError):
10198
func(index)
10299

103100
elif isinstance(index, PeriodIndex):
104-
# raise TypeError or ValueError (PeriodIndex)
105-
with pytest.raises(Exception):
101+
with tm.external_error_raised(TypeError):
106102
func(index)
107103

108104
elif isinstance(index, (Float64Index, Int64Index, UInt64Index)):
@@ -114,5 +110,5 @@ def test_numpy_ufuncs_other(index, func, request):
114110
if len(index) == 0:
115111
pass
116112
else:
117-
with pytest.raises(Exception):
113+
with tm.external_error_raised(TypeError):
118114
func(index)

0 commit comments

Comments
 (0)