Skip to content

Commit 37022be

Browse files
authored
add match message for pytest.raises() (#33144)
1 parent 9590c5b commit 37022be

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

pandas/tests/groupby/test_categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ def test_bins_unequal_len():
609609
bins = pd.cut(series.dropna().values, 4)
610610

611611
# len(bins) != len(series) here
612-
with pytest.raises(ValueError):
612+
msg = r"Length of grouper \(8\) and axis \(10\) must be same length"
613+
with pytest.raises(ValueError, match=msg):
613614
series.groupby(bins).mean()
614615

615616

pandas/tests/indexes/common.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ def test_take(self, indices):
349349

350350
if not isinstance(indices, (DatetimeIndex, PeriodIndex, TimedeltaIndex)):
351351
# GH 10791
352-
with pytest.raises(AttributeError):
352+
msg = r"'(.*Index)' object has no attribute 'freq'"
353+
with pytest.raises(AttributeError, match=msg):
353354
indices.freq
354355

355356
def test_take_invalid_kwargs(self):
@@ -537,9 +538,10 @@ def test_delete_base(self, indices):
537538
assert result.equals(expected)
538539
assert result.name == expected.name
539540

540-
with pytest.raises((IndexError, ValueError)):
541-
# either depending on numpy version
542-
indices.delete(len(indices))
541+
length = len(indices)
542+
msg = f"index {length} is out of bounds for axis 0 with size {length}"
543+
with pytest.raises(IndexError, match=msg):
544+
indices.delete(length)
543545

544546
def test_equals(self, indices):
545547
if isinstance(indices, IntervalIndex):
@@ -787,13 +789,14 @@ def test_putmask_with_wrong_mask(self):
787789
# GH18368
788790
index = self.create_index()
789791

790-
with pytest.raises(ValueError):
792+
msg = "putmask: mask and data must be the same size"
793+
with pytest.raises(ValueError, match=msg):
791794
index.putmask(np.ones(len(index) + 1, np.bool), 1)
792795

793-
with pytest.raises(ValueError):
796+
with pytest.raises(ValueError, match=msg):
794797
index.putmask(np.ones(len(index) - 1, np.bool), 1)
795798

796-
with pytest.raises(ValueError):
799+
with pytest.raises(ValueError, match=msg):
797800
index.putmask("foo", 1)
798801

799802
@pytest.mark.parametrize("copy", [True, False])
@@ -861,10 +864,21 @@ def test_getitem_2d_deprecated(self):
861864

862865
def test_contains_requires_hashable_raises(self):
863866
idx = self.create_index()
864-
with pytest.raises(TypeError, match="unhashable type"):
867+
868+
msg = "unhashable type: 'list'"
869+
with pytest.raises(TypeError, match=msg):
865870
[] in idx
866871

867-
with pytest.raises(TypeError):
872+
msg = "|".join(
873+
[
874+
r"unhashable type: 'dict'",
875+
r"must be real number, not dict",
876+
r"an integer is required",
877+
r"\{\}",
878+
r"pandas\._libs\.interval\.IntervalTree' is not iterable",
879+
]
880+
)
881+
with pytest.raises(TypeError, match=msg):
868882
{} in idx._engine
869883

870884
def test_copy_copies_cache(self):

pandas/tests/indexes/datetimelike.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
class DatetimeLike(Base):
1212
def test_argmax_axis_invalid(self):
1313
# GH#23081
14+
msg = r"`axis` must be fewer than the number of dimensions \(1\)"
1415
rng = self.create_index()
15-
with pytest.raises(ValueError):
16+
with pytest.raises(ValueError, match=msg):
1617
rng.argmax(axis=1)
17-
with pytest.raises(ValueError):
18+
with pytest.raises(ValueError, match=msg):
1819
rng.argmin(axis=2)
19-
with pytest.raises(ValueError):
20+
with pytest.raises(ValueError, match=msg):
2021
rng.min(axis=-2)
21-
with pytest.raises(ValueError):
22+
with pytest.raises(ValueError, match=msg):
2223
rng.max(axis=-3)
2324

2425
def test_can_hold_identifiers(self):

pandas/tests/indexes/multi/test_compat.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ def test_boolean_context_compat2():
5353
i2 = MultiIndex.from_tuples([("A", 1), ("A", 3)])
5454
common = i1.intersection(i2)
5555

56-
with pytest.raises(ValueError):
56+
msg = (
57+
r"The truth value of a MultiIndex is ambiguous\. "
58+
r"Use a\.empty, a\.bool\(\), a\.item\(\), a\.any\(\) or a\.all\(\)\."
59+
)
60+
with pytest.raises(ValueError, match=msg):
5761
bool(common)
5862

5963

pandas/tests/indexes/multi/test_reshape.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,6 @@ def test_delete_base(idx):
175175
assert result.equals(expected)
176176
assert result.name == expected.name
177177

178-
with pytest.raises((IndexError, ValueError)):
179-
# Exception raised depends on NumPy version.
178+
msg = "index 6 is out of bounds for axis 0 with size 6"
179+
with pytest.raises(IndexError, match=msg):
180180
idx.delete(len(idx))

pandas/tests/indexes/multi/test_sorting.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ def test_unsortedindex():
105105
expected = df.iloc[0]
106106
tm.assert_series_equal(result, expected)
107107

108-
with pytest.raises(UnsortedIndexError):
108+
msg = (
109+
"MultiIndex slicing requires the index to be lexsorted: "
110+
r"slicing on levels \[1\], lexsort depth 0"
111+
)
112+
with pytest.raises(UnsortedIndexError, match=msg):
109113
df.loc(axis=0)["z", slice("a")]
110114
df.sort_index(inplace=True)
111115
assert len(df.loc(axis=0)["z", :]) == 2
@@ -124,7 +128,8 @@ def test_unsortedindex_doc_examples():
124128
with tm.assert_produces_warning(PerformanceWarning):
125129
dfm.loc[(1, "z")]
126130

127-
with pytest.raises(UnsortedIndexError):
131+
msg = r"Key length \(2\) was greater than MultiIndex lexsort depth \(1\)"
132+
with pytest.raises(UnsortedIndexError, match=msg):
128133
dfm.loc[(0, "y"):(1, "z")]
129134

130135
assert not dfm.index.is_lexsorted()

0 commit comments

Comments
 (0)