Skip to content

Commit bbfa77b

Browse files
proostpull[bot]
authored andcommitted
TST: add messages to pytest.raises (#30999) (#33650)
1 parent ea60c49 commit bbfa77b

File tree

6 files changed

+32
-17
lines changed

6 files changed

+32
-17
lines changed

pandas/tests/arrays/boolean/test_function.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def test_ufuncs_binary(ufunc):
5454
tm.assert_extension_array_equal(result, expected)
5555

5656
# not handled types
57-
with pytest.raises(TypeError):
57+
msg = r"operand type\(s\) all returned NotImplemented from __array_ufunc__"
58+
with pytest.raises(TypeError, match=msg):
5859
ufunc(a, "test")
5960

6061

@@ -76,7 +77,8 @@ def test_ufuncs_unary(ufunc):
7677
@pytest.mark.parametrize("values", [[True, False], [True, None]])
7778
def test_ufunc_reduce_raises(values):
7879
a = pd.array(values, dtype="boolean")
79-
with pytest.raises(NotImplementedError):
80+
msg = "The 'reduce' method is not supported"
81+
with pytest.raises(NotImplementedError, match=msg):
8082
np.add.reduce(a)
8183

8284

pandas/tests/indexes/ranges/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ def test_constructor_corner(self):
149149
tm.assert_index_equal(index, Index(arr))
150150

151151
# non-int raise Exception
152-
with pytest.raises(TypeError):
152+
with pytest.raises(TypeError, match=r"Wrong type \<class 'str'\>"):
153153
RangeIndex("1", "10", "1")
154-
with pytest.raises(TypeError):
154+
with pytest.raises(TypeError, match=r"Wrong type \<class 'float'\>"):
155155
RangeIndex(1.1, 10.2, 1.3)
156156

157157
# invalid passed type

pandas/tests/indexes/ranges/test_range.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ def test_delete(self):
117117
tm.assert_index_equal(result, expected)
118118
assert result.name == expected.name
119119

120-
with pytest.raises((IndexError, ValueError)):
120+
msg = "index 5 is out of bounds for axis 0 with size 5"
121+
with pytest.raises((IndexError, ValueError), match=msg):
121122
# either depending on numpy version
122123
result = idx.delete(len(idx))
123124

pandas/tests/indexing/multiindex/test_chaining_and_caching.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def test_detect_chained_assignment():
2323
multiind = MultiIndex.from_tuples(tuples, names=["part", "side"])
2424
zed = DataFrame(events, index=["a", "b"], columns=multiind)
2525

26-
with pytest.raises(com.SettingWithCopyError):
26+
msg = "A value is trying to be set on a copy of a slice from a DataFrame"
27+
with pytest.raises(com.SettingWithCopyError, match=msg):
2728
zed["eyes"]["right"].fillna(value=555, inplace=True)
2829

2930

pandas/tests/indexing/test_partial.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ def test_partial_setting(self):
4343
# iloc/iat raise
4444
s = s_orig.copy()
4545

46-
with pytest.raises(IndexError):
46+
msg = "iloc cannot enlarge its target object"
47+
with pytest.raises(IndexError, match=msg):
4748
s.iloc[3] = 5.0
4849

49-
with pytest.raises(IndexError):
50+
msg = "index 3 is out of bounds for axis 0 with size 3"
51+
with pytest.raises(IndexError, match=msg):
5052
s.iat[3] = 5.0
5153

5254
# ## frame ##
@@ -58,10 +60,12 @@ def test_partial_setting(self):
5860
# iloc/iat raise
5961
df = df_orig.copy()
6062

61-
with pytest.raises(IndexError):
63+
msg = "iloc cannot enlarge its target object"
64+
with pytest.raises(IndexError, match=msg):
6265
df.iloc[4, 2] = 5.0
6366

64-
with pytest.raises(IndexError):
67+
msg = "index 2 is out of bounds for axis 0 with size 2"
68+
with pytest.raises(IndexError, match=msg):
6569
df.iat[4, 2] = 5.0
6670

6771
# row setting where it exists
@@ -162,7 +166,8 @@ def test_partial_setting_mixed_dtype(self):
162166
# list-like must conform
163167
df = DataFrame(columns=["A", "B"])
164168

165-
with pytest.raises(ValueError):
169+
msg = "cannot set a row with mismatched columns"
170+
with pytest.raises(ValueError, match=msg):
166171
df.loc[0] = [1, 2, 3]
167172

168173
# TODO: #15657, these are left as object and not coerced
@@ -330,10 +335,12 @@ def test_partial_set_invalid(self):
330335
df = orig.copy()
331336

332337
# don't allow not string inserts
333-
with pytest.raises(TypeError):
338+
msg = "cannot insert DatetimeIndex with incompatible label"
339+
340+
with pytest.raises(TypeError, match=msg):
334341
df.loc[100.0, :] = df.iloc[0]
335342

336-
with pytest.raises(TypeError):
343+
with pytest.raises(TypeError, match=msg):
337344
df.loc[100, :] = df.iloc[0]
338345

339346
# allow object conversion here
@@ -375,13 +382,16 @@ def test_partial_set_empty_frame(self):
375382
# frame
376383
df = DataFrame()
377384

378-
with pytest.raises(ValueError):
385+
msg = "cannot set a frame with no defined columns"
386+
387+
with pytest.raises(ValueError, match=msg):
379388
df.loc[1] = 1
380389

381-
with pytest.raises(ValueError):
390+
with pytest.raises(ValueError, match=msg):
382391
df.loc[1] = Series([1], index=["foo"])
383392

384-
with pytest.raises(ValueError):
393+
msg = "cannot set a frame with no defined index and a scalar"
394+
with pytest.raises(ValueError, match=msg):
385395
df.loc[:, 1] = 1
386396

387397
# these work as they don't really change

pandas/tests/series/indexing/test_alter_index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ def test_reindex_datetimeindexes_tz_naive_and_aware():
283283
idx = date_range("20131101", tz="America/Chicago", periods=7)
284284
newidx = date_range("20131103", periods=10, freq="H")
285285
s = Series(range(7), index=idx)
286-
with pytest.raises(TypeError):
286+
msg = "Cannot compare tz-naive and tz-aware timestamps"
287+
with pytest.raises(TypeError, match=msg):
287288
s.reindex(newidx, method="ffill")
288289

289290

0 commit comments

Comments
 (0)