Skip to content

Commit 14f1982

Browse files
gdex1jreback
authored andcommitted
TST: Remove bare pytest.raises (#31079)
1 parent f6ef10a commit 14f1982

File tree

5 files changed

+97
-54
lines changed

5 files changed

+97
-54
lines changed

pandas/core/ops/array_ops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def masked_arith_op(x, y, op):
9595

9696
else:
9797
if not is_scalar(y):
98-
raise TypeError(type(y))
98+
raise TypeError(
99+
f"Cannot broadcast np.ndarray with operand of type { type(y) }"
100+
)
99101

100102
# mask is only meaningful for x
101103
result = np.empty(x.size, dtype=x.dtype)

pandas/tests/arithmetic/test_numeric.py

+34-17
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ def test_div_td64arr(self, left, box_cls):
135135
result = right // left
136136
tm.assert_equal(result, expected)
137137

138-
with pytest.raises(TypeError):
138+
msg = "Cannot divide"
139+
with pytest.raises(TypeError, match=msg):
139140
left / right
140141

141-
with pytest.raises(TypeError):
142+
with pytest.raises(TypeError, match=msg):
142143
left // right
143144

144145
# TODO: de-duplicate with test_numeric_arr_mul_tdscalar
@@ -187,7 +188,8 @@ def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box):
187188
result = three_days / index
188189
tm.assert_equal(result, expected)
189190

190-
with pytest.raises(TypeError):
191+
msg = "cannot use operands with types dtype"
192+
with pytest.raises(TypeError, match=msg):
191193
index / three_days
192194

193195
@pytest.mark.parametrize(
@@ -205,13 +207,19 @@ def test_numeric_arr_rdiv_tdscalar(self, three_days, numeric_idx, box):
205207
)
206208
def test_add_sub_timedeltalike_invalid(self, numeric_idx, other, box):
207209
left = tm.box_expected(numeric_idx, box)
208-
with pytest.raises(TypeError):
210+
msg = (
211+
"unsupported operand type|"
212+
"Addition/subtraction of integers and integer-arrays|"
213+
"Instead of adding/subtracting|"
214+
"cannot use operands with types dtype"
215+
)
216+
with pytest.raises(TypeError, match=msg):
209217
left + other
210-
with pytest.raises(TypeError):
218+
with pytest.raises(TypeError, match=msg):
211219
other + left
212-
with pytest.raises(TypeError):
220+
with pytest.raises(TypeError, match=msg):
213221
left - other
214-
with pytest.raises(TypeError):
222+
with pytest.raises(TypeError, match=msg):
215223
other - left
216224

217225
@pytest.mark.parametrize(
@@ -229,13 +237,18 @@ def test_add_sub_datetimelike_invalid(self, numeric_idx, other, box):
229237
# NullFrequencyError instead of TypeError so is excluded.
230238
left = tm.box_expected(numeric_idx, box)
231239

232-
with pytest.raises(TypeError):
240+
msg = (
241+
"unsupported operand type|"
242+
"Cannot (add|subtract) NaT (to|from) ndarray|"
243+
"Addition/subtraction of integers and integer-arrays"
244+
)
245+
with pytest.raises(TypeError, match=msg):
233246
left + other
234-
with pytest.raises(TypeError):
247+
with pytest.raises(TypeError, match=msg):
235248
other + left
236-
with pytest.raises(TypeError):
249+
with pytest.raises(TypeError, match=msg):
237250
left - other
238-
with pytest.raises(TypeError):
251+
with pytest.raises(TypeError, match=msg):
239252
other - left
240253

241254

@@ -607,14 +620,16 @@ def test_mul_index(self, numeric_idx):
607620

608621
def test_mul_datelike_raises(self, numeric_idx):
609622
idx = numeric_idx
610-
with pytest.raises(TypeError):
623+
msg = "cannot perform __rmul__ with this index type"
624+
with pytest.raises(TypeError, match=msg):
611625
idx * pd.date_range("20130101", periods=5)
612626

613627
def test_mul_size_mismatch_raises(self, numeric_idx):
614628
idx = numeric_idx
615-
with pytest.raises(ValueError):
629+
msg = "operands could not be broadcast together"
630+
with pytest.raises(ValueError, match=msg):
616631
idx * idx[0:3]
617-
with pytest.raises(ValueError):
632+
with pytest.raises(ValueError, match=msg):
618633
idx * np.array([1, 2])
619634

620635
@pytest.mark.parametrize("op", [operator.pow, ops.rpow])
@@ -792,10 +807,11 @@ def test_series_frame_radd_bug(self):
792807

793808
# really raise this time
794809
now = pd.Timestamp.now().to_pydatetime()
795-
with pytest.raises(TypeError):
810+
msg = "unsupported operand type"
811+
with pytest.raises(TypeError, match=msg):
796812
now + ts
797813

798-
with pytest.raises(TypeError):
814+
with pytest.raises(TypeError, match=msg):
799815
ts + now
800816

801817
# TODO: This came from series.test.test_operators, needs cleanup
@@ -816,7 +832,8 @@ def test_datetime64_with_index(self):
816832
result = ser - ser.index
817833
tm.assert_series_equal(result, expected)
818834

819-
with pytest.raises(TypeError):
835+
msg = "cannot subtract period"
836+
with pytest.raises(TypeError, match=msg):
820837
# GH#18850
821838
result = ser - ser.index.to_period()
822839

pandas/tests/arithmetic/test_object.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ def test_objarr_add_invalid(self, op, box_with_array):
155155
obj_ser.name = "objects"
156156

157157
obj_ser = tm.box_expected(obj_ser, box)
158-
with pytest.raises(Exception):
158+
msg = "can only concatenate str|unsupported operand type|must be str"
159+
with pytest.raises(Exception, match=msg):
159160
op(obj_ser, 1)
160-
with pytest.raises(Exception):
161+
with pytest.raises(Exception, match=msg):
161162
op(obj_ser, np.array(1, dtype=np.int64))
162163

163164
# TODO: Moved from tests.series.test_operators; needs cleanup
@@ -281,13 +282,15 @@ def test_add(self):
281282

282283
def test_sub_fail(self):
283284
index = tm.makeStringIndex(100)
284-
with pytest.raises(TypeError):
285+
286+
msg = "unsupported operand type|Cannot broadcast"
287+
with pytest.raises(TypeError, match=msg):
285288
index - "a"
286-
with pytest.raises(TypeError):
289+
with pytest.raises(TypeError, match=msg):
287290
index - index
288-
with pytest.raises(TypeError):
291+
with pytest.raises(TypeError, match=msg):
289292
index - index.tolist()
290-
with pytest.raises(TypeError):
293+
with pytest.raises(TypeError, match=msg):
291294
index.tolist() - index
292295

293296
def test_sub_object(self):
@@ -301,10 +304,11 @@ def test_sub_object(self):
301304
result = index - pd.Index([Decimal(1), Decimal(1)])
302305
tm.assert_index_equal(result, expected)
303306

304-
with pytest.raises(TypeError):
307+
msg = "unsupported operand type"
308+
with pytest.raises(TypeError, match=msg):
305309
index - "foo"
306310

307-
with pytest.raises(TypeError):
311+
with pytest.raises(TypeError, match=msg):
308312
index - np.array([2, "foo"])
309313

310314
def test_rsub_object(self):
@@ -318,8 +322,9 @@ def test_rsub_object(self):
318322
result = np.array([Decimal(2), Decimal(2)]) - index
319323
tm.assert_index_equal(result, expected)
320324

321-
with pytest.raises(TypeError):
325+
msg = "unsupported operand type"
326+
with pytest.raises(TypeError, match=msg):
322327
"foo" - index
323328

324-
with pytest.raises(TypeError):
329+
with pytest.raises(TypeError, match=msg):
325330
np.array([True, pd.Timestamp.now()]) - index

pandas/tests/arithmetic/test_timedelta64.py

+44-25
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ def test_compare_timedelta64_zerodim(self, box_with_array):
4848
expected = tm.box_expected(expected, xbox)
4949
tm.assert_equal(res, expected)
5050

51-
with pytest.raises(TypeError):
51+
msg = "Invalid comparison between dtype"
52+
with pytest.raises(TypeError, match=msg):
5253
# zero-dim of wrong dtype should still raise
5354
tdi >= np.array(4)
5455

@@ -442,7 +443,8 @@ def test_addition_ops(self):
442443
tdi[0:1] + dti
443444

444445
# random indexes
445-
with pytest.raises(TypeError):
446+
msg = "Addition/subtraction of integers and integer-arrays"
447+
with pytest.raises(TypeError, match=msg):
446448
tdi + pd.Int64Index([1, 2, 3])
447449

448450
# this is a union!
@@ -604,6 +606,7 @@ def test_tdi_add_timestamp_nat_masking(self):
604606
def test_tdi_add_overflow(self):
605607
# See GH#14068
606608
# preliminary test scalar analogue of vectorized tests below
609+
# TODO: Make raised error message more informative and test
607610
with pytest.raises(OutOfBoundsDatetime):
608611
pd.to_timedelta(106580, "D") + Timestamp("2000")
609612
with pytest.raises(OutOfBoundsDatetime):
@@ -700,13 +703,14 @@ def test_timedelta_ops_with_missing_values(self):
700703
actual = -timedelta_NaT + s1
701704
tm.assert_series_equal(actual, sn)
702705

703-
with pytest.raises(TypeError):
706+
msg = "unsupported operand type"
707+
with pytest.raises(TypeError, match=msg):
704708
s1 + np.nan
705-
with pytest.raises(TypeError):
709+
with pytest.raises(TypeError, match=msg):
706710
np.nan + s1
707-
with pytest.raises(TypeError):
711+
with pytest.raises(TypeError, match=msg):
708712
s1 - np.nan
709-
with pytest.raises(TypeError):
713+
with pytest.raises(TypeError, match=msg):
710714
-np.nan + s1
711715

712716
actual = s1 + pd.NaT
@@ -738,9 +742,10 @@ def test_timedelta_ops_with_missing_values(self):
738742
actual = df1 - timedelta_NaT
739743
tm.assert_frame_equal(actual, dfn)
740744

741-
with pytest.raises(TypeError):
745+
msg = "cannot subtract a datelike from|unsupported operand type"
746+
with pytest.raises(TypeError, match=msg):
742747
df1 + np.nan
743-
with pytest.raises(TypeError):
748+
with pytest.raises(TypeError, match=msg):
744749
df1 - np.nan
745750

746751
actual = df1 + pd.NaT # NaT is datetime, not timedelta
@@ -957,7 +962,8 @@ def test_td64arr_add_sub_datetimelike_scalar(self, ts, box_with_array):
957962
tm.assert_equal(ts - tdarr, expected2)
958963
tm.assert_equal(ts + (-tdarr), expected2)
959964

960-
with pytest.raises(TypeError):
965+
msg = "cannot subtract a datelike"
966+
with pytest.raises(TypeError, match=msg):
961967
tdarr - ts
962968

963969
def test_tdi_sub_dt64_array(self, box_with_array):
@@ -969,7 +975,8 @@ def test_tdi_sub_dt64_array(self, box_with_array):
969975
tdi = tm.box_expected(tdi, box_with_array)
970976
expected = tm.box_expected(expected, box_with_array)
971977

972-
with pytest.raises(TypeError):
978+
msg = "cannot subtract a datelike from"
979+
with pytest.raises(TypeError, match=msg):
973980
tdi - dtarr
974981

975982
# TimedeltaIndex.__rsub__
@@ -1025,7 +1032,8 @@ def test_td64arr_sub_periodlike(self, box_with_array, tdi_freq, pi_freq):
10251032

10261033
# TODO: parametrize over box for pi?
10271034
tdi = tm.box_expected(tdi, box_with_array)
1028-
with pytest.raises(TypeError):
1035+
msg = "cannot subtract|unsupported operand type"
1036+
with pytest.raises(TypeError, match=msg):
10291037
tdi - pi
10301038

10311039
# FIXME: don't leave commented-out
@@ -1034,9 +1042,9 @@ def test_td64arr_sub_periodlike(self, box_with_array, tdi_freq, pi_freq):
10341042
# pi - tdi
10351043

10361044
# GH#13078 subtraction of Period scalar not supported
1037-
with pytest.raises(TypeError):
1045+
with pytest.raises(TypeError, match=msg):
10381046
tdi - pi[0]
1039-
with pytest.raises(TypeError):
1047+
with pytest.raises(TypeError, match=msg):
10401048
pi[0] - tdi
10411049

10421050
@pytest.mark.parametrize(
@@ -1499,16 +1507,17 @@ def test_td64arr_addsub_anchored_offset_arraylike(self, obox, box_with_array):
14991507

15001508
# addition/subtraction ops with anchored offsets should issue
15011509
# a PerformanceWarning and _then_ raise a TypeError.
1502-
with pytest.raises(TypeError):
1510+
msg = "has incorrect type|cannot add the type MonthEnd"
1511+
with pytest.raises(TypeError, match=msg):
15031512
with tm.assert_produces_warning(PerformanceWarning):
15041513
tdi + anchored
1505-
with pytest.raises(TypeError):
1514+
with pytest.raises(TypeError, match=msg):
15061515
with tm.assert_produces_warning(PerformanceWarning):
15071516
anchored + tdi
1508-
with pytest.raises(TypeError):
1517+
with pytest.raises(TypeError, match=msg):
15091518
with tm.assert_produces_warning(PerformanceWarning):
15101519
tdi - anchored
1511-
with pytest.raises(TypeError):
1520+
with pytest.raises(TypeError, match=msg):
15121521
with tm.assert_produces_warning(PerformanceWarning):
15131522
anchored - tdi
15141523

@@ -1533,7 +1542,8 @@ def test_td64arr_add_sub_object_array(self, box_with_array):
15331542
expected = tm.box_expected(expected, box_with_array)
15341543
tm.assert_equal(result, expected)
15351544

1536-
with pytest.raises(TypeError):
1545+
msg = "unsupported operand type|cannot subtract a datelike"
1546+
with pytest.raises(TypeError, match=msg):
15371547
with tm.assert_produces_warning(warn):
15381548
tdarr - other
15391549

@@ -1588,7 +1598,8 @@ def test_td64arr_mul_int(self, box_with_array):
15881598
def test_td64arr_mul_tdlike_scalar_raises(self, two_hours, box_with_array):
15891599
rng = timedelta_range("1 days", "10 days", name="foo")
15901600
rng = tm.box_expected(rng, box_with_array)
1591-
with pytest.raises(TypeError):
1601+
msg = "argument must be an integer|cannot use operands with types dtype"
1602+
with pytest.raises(TypeError, match=msg):
15921603
rng * two_hours
15931604

15941605
def test_tdi_mul_int_array_zerodim(self, box_with_array):
@@ -1777,12 +1788,13 @@ def test_tdarr_div_length_mismatch(self, box_with_array):
17771788
mismatched = [1, 2, 3, 4]
17781789

17791790
rng = tm.box_expected(rng, box_with_array)
1791+
msg = "Cannot divide vectors|Unable to coerce to Series"
17801792
for obj in [mismatched, mismatched[:2]]:
17811793
# one shorter, one longer
17821794
for other in [obj, np.array(obj), pd.Index(obj)]:
1783-
with pytest.raises(ValueError):
1795+
with pytest.raises(ValueError, match=msg):
17841796
rng / other
1785-
with pytest.raises(ValueError):
1797+
with pytest.raises(ValueError, match=msg):
17861798
other / rng
17871799

17881800
# ------------------------------------------------------------------
@@ -1908,7 +1920,8 @@ def test_td64arr_mod_int(self, box_with_array):
19081920
result = tdarr % 2
19091921
tm.assert_equal(result, expected)
19101922

1911-
with pytest.raises(TypeError):
1923+
msg = "Cannot divide int by"
1924+
with pytest.raises(TypeError, match=msg):
19121925
2 % tdarr
19131926

19141927
if box_with_array is pd.DataFrame:
@@ -1957,15 +1970,21 @@ def test_td64arr_mul_tdscalar_invalid(self, box_with_array, scalar_td):
19571970
def test_td64arr_mul_too_short_raises(self, box_with_array):
19581971
idx = TimedeltaIndex(np.arange(5, dtype="int64"))
19591972
idx = tm.box_expected(idx, box_with_array)
1960-
with pytest.raises(TypeError):
1973+
msg = (
1974+
"cannot use operands with types dtype|"
1975+
"Cannot multiply with unequal lengths|"
1976+
"Unable to coerce to Series"
1977+
)
1978+
with pytest.raises(TypeError, match=msg):
19611979
idx * idx[:3]
1962-
with pytest.raises(ValueError):
1980+
with pytest.raises(ValueError, match=msg):
19631981
idx * np.array([1, 2])
19641982

19651983
def test_td64arr_mul_td64arr_raises(self, box_with_array):
19661984
idx = TimedeltaIndex(np.arange(5, dtype="int64"))
19671985
idx = tm.box_expected(idx, box_with_array)
1968-
with pytest.raises(TypeError):
1986+
msg = "cannot use operands with types dtype"
1987+
with pytest.raises(TypeError, match=msg):
19691988
idx * idx
19701989

19711990
# ------------------------------------------------------------------

pandas/tests/arrays/interval/test_interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def test_arrow_array():
152152
assert result.equals(expected)
153153

154154
# unsupported conversions
155-
with pytest.raises(TypeError):
155+
with pytest.raises(TypeError, match="Not supported to convert IntervalArray"):
156156
pa.array(intervals, type="float64")
157157

158158
with pytest.raises(TypeError, match="different 'subtype'"):

0 commit comments

Comments
 (0)