Skip to content

Commit 2ef8fd1

Browse files
authored
TST: bare pytest raises in tests/scalar (#32929)
1 parent 2c2ba45 commit 2ef8fd1

13 files changed

+348
-180
lines changed

pandas/tests/scalar/interval/test_interval.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def test_equal(self):
4949
assert Interval(0, 1) != 0
5050

5151
def test_comparison(self):
52-
with pytest.raises(TypeError, match="unorderable types"):
52+
msg = "unorderable types"
53+
with pytest.raises(TypeError, match=msg):
5354
Interval(0, 1) < 2
5455

5556
assert Interval(0, 1) < Interval(1, 2)
@@ -254,6 +255,12 @@ def test_constructor_errors_tz(self, tz_left, tz_right):
254255
# GH 18538
255256
left = Timestamp("2017-01-01", tz=tz_left)
256257
right = Timestamp("2017-01-02", tz=tz_right)
257-
error = TypeError if com.any_none(tz_left, tz_right) else ValueError
258-
with pytest.raises(error):
258+
259+
if com.any_none(tz_left, tz_right):
260+
error = TypeError
261+
msg = "Cannot compare tz-naive and tz-aware timestamps"
262+
else:
263+
error = ValueError
264+
msg = "left and right must have the same time zone"
265+
with pytest.raises(error, match=msg):
259266
Interval(left, right)

pandas/tests/scalar/period/test_asfreq.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def test_asfreq_near_zero_weekly(self):
3333
def test_to_timestamp_out_of_bounds(self):
3434
# GH#19643, used to incorrectly give Timestamp in 1754
3535
per = Period("0001-01-01", freq="B")
36-
with pytest.raises(OutOfBoundsDatetime):
36+
msg = "Out of bounds nanosecond timestamp"
37+
with pytest.raises(OutOfBoundsDatetime, match=msg):
3738
per.to_timestamp()
3839

3940
def test_asfreq_corner(self):
@@ -668,9 +669,10 @@ def test_conv_microsecond(self):
668669
assert start.value == per.ordinal * 1000
669670

670671
per2 = Period("2300-01-01", "us")
671-
with pytest.raises(OutOfBoundsDatetime, match="2300-01-01"):
672+
msg = "2300-01-01"
673+
with pytest.raises(OutOfBoundsDatetime, match=msg):
672674
per2.start_time
673-
with pytest.raises(OutOfBoundsDatetime, match="2300-01-01"):
675+
with pytest.raises(OutOfBoundsDatetime, match=msg):
674676
per2.end_time
675677

676678
def test_asfreq_mult(self):

pandas/tests/scalar/period/test_period.py

+88-37
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def test_construction(self):
7979
with pytest.raises(ValueError, match=msg):
8080
Period(ordinal=200701)
8181

82-
with pytest.raises(ValueError, match="Invalid frequency: X"):
82+
msg = "Invalid frequency: X"
83+
with pytest.raises(ValueError, match=msg):
8384
Period("2007-1-1", freq="X")
8485

8586
def test_construction_bday(self):
@@ -235,26 +236,34 @@ def test_period_constructor_offsets(self):
235236
assert i1 == expected
236237

237238
def test_invalid_arguments(self):
238-
with pytest.raises(ValueError):
239+
msg = "Must supply freq for datetime value"
240+
with pytest.raises(ValueError, match=msg):
239241
Period(datetime.now())
240-
with pytest.raises(ValueError):
242+
with pytest.raises(ValueError, match=msg):
241243
Period(datetime.now().date())
242244

243-
with pytest.raises(ValueError):
245+
msg = "Value must be Period, string, integer, or datetime"
246+
with pytest.raises(ValueError, match=msg):
244247
Period(1.6, freq="D")
245-
with pytest.raises(ValueError):
248+
msg = "Ordinal must be an integer"
249+
with pytest.raises(ValueError, match=msg):
246250
Period(ordinal=1.6, freq="D")
247-
with pytest.raises(ValueError):
251+
msg = "Only value or ordinal but not both should be given but not both"
252+
with pytest.raises(ValueError, match=msg):
248253
Period(ordinal=2, value=1, freq="D")
249254

250-
with pytest.raises(ValueError):
255+
msg = "If value is None, freq cannot be None"
256+
with pytest.raises(ValueError, match=msg):
251257
Period(month=1)
252258

253-
with pytest.raises(ValueError):
259+
msg = "Given date string not likely a datetime"
260+
with pytest.raises(ValueError, match=msg):
254261
Period("-2000", "A")
255-
with pytest.raises(DateParseError):
262+
msg = "day is out of range for month"
263+
with pytest.raises(DateParseError, match=msg):
256264
Period("0", "A")
257-
with pytest.raises(DateParseError):
265+
msg = "Unknown datetime string format, unable to parse"
266+
with pytest.raises(DateParseError, match=msg):
258267
Period("1/1/-2000", "A")
259268

260269
def test_constructor_corner(self):
@@ -1030,7 +1039,8 @@ def test_sub_delta(self):
10301039
result = left - right
10311040
assert result == 4 * right.freq
10321041

1033-
with pytest.raises(IncompatibleFrequency):
1042+
msg = r"Input has different freq=M from Period\(freq=A-DEC\)"
1043+
with pytest.raises(IncompatibleFrequency, match=msg):
10341044
left - Period("2007-01", freq="M")
10351045

10361046
def test_add_integer(self):
@@ -1072,10 +1082,14 @@ def test_add_timestamp_raises(self, rbox, lbox):
10721082

10731083
# We may get a different message depending on which class raises
10741084
# the error.
1075-
msg = (
1076-
r"cannot add|unsupported operand|"
1077-
r"can only operate on a|incompatible type|"
1078-
r"ufunc add cannot use operands"
1085+
msg = "|".join(
1086+
[
1087+
"cannot add",
1088+
"unsupported operand",
1089+
"can only operate on a",
1090+
"incompatible type",
1091+
"ufunc add cannot use operands",
1092+
]
10791093
)
10801094
with pytest.raises(TypeError, match=msg):
10811095
lbox(ts) + rbox(per)
@@ -1148,14 +1162,22 @@ def test_add_offset(self):
11481162
np.timedelta64(365, "D"),
11491163
timedelta(365),
11501164
]:
1151-
with pytest.raises(IncompatibleFrequency):
1165+
msg = "Input has different freq|Input cannot be converted to Period"
1166+
with pytest.raises(IncompatibleFrequency, match=msg):
11521167
p + o
11531168

11541169
if isinstance(o, np.timedelta64):
1155-
with pytest.raises(TypeError):
1170+
msg = "cannot use operands with types"
1171+
with pytest.raises(TypeError, match=msg):
11561172
o + p
11571173
else:
1158-
with pytest.raises(IncompatibleFrequency):
1174+
msg = "|".join(
1175+
[
1176+
"Input has different freq",
1177+
"Input cannot be converted to Period",
1178+
]
1179+
)
1180+
with pytest.raises(IncompatibleFrequency, match=msg):
11591181
o + p
11601182

11611183
for freq in ["M", "2M", "3M"]:
@@ -1175,14 +1197,22 @@ def test_add_offset(self):
11751197
np.timedelta64(365, "D"),
11761198
timedelta(365),
11771199
]:
1178-
with pytest.raises(IncompatibleFrequency):
1200+
msg = "Input has different freq|Input cannot be converted to Period"
1201+
with pytest.raises(IncompatibleFrequency, match=msg):
11791202
p + o
11801203

11811204
if isinstance(o, np.timedelta64):
1182-
with pytest.raises(TypeError):
1205+
msg = "cannot use operands with types"
1206+
with pytest.raises(TypeError, match=msg):
11831207
o + p
11841208
else:
1185-
with pytest.raises(IncompatibleFrequency):
1209+
msg = "|".join(
1210+
[
1211+
"Input has different freq",
1212+
"Input cannot be converted to Period",
1213+
]
1214+
)
1215+
with pytest.raises(IncompatibleFrequency, match=msg):
11861216
o + p
11871217

11881218
# freq is Tick
@@ -1199,12 +1229,13 @@ def test_add_offset(self):
11991229

12001230
exp = Period("2011-04-03", freq=freq)
12011231
assert p + np.timedelta64(2, "D") == exp
1202-
with pytest.raises(TypeError):
1232+
msg = "cannot use operands with types"
1233+
with pytest.raises(TypeError, match=msg):
12031234
np.timedelta64(2, "D") + p
12041235

12051236
exp = Period("2011-04-02", freq=freq)
12061237
assert p + np.timedelta64(3600 * 24, "s") == exp
1207-
with pytest.raises(TypeError):
1238+
with pytest.raises(TypeError, match=msg):
12081239
np.timedelta64(3600 * 24, "s") + p
12091240

12101241
exp = Period("2011-03-30", freq=freq)
@@ -1222,14 +1253,22 @@ def test_add_offset(self):
12221253
np.timedelta64(4, "h"),
12231254
timedelta(hours=23),
12241255
]:
1225-
with pytest.raises(IncompatibleFrequency):
1256+
msg = "Input has different freq|Input cannot be converted to Period"
1257+
with pytest.raises(IncompatibleFrequency, match=msg):
12261258
p + o
12271259

12281260
if isinstance(o, np.timedelta64):
1229-
with pytest.raises(TypeError):
1261+
msg = "cannot use operands with types"
1262+
with pytest.raises(TypeError, match=msg):
12301263
o + p
12311264
else:
1232-
with pytest.raises(IncompatibleFrequency):
1265+
msg = "|".join(
1266+
[
1267+
"Input has different freq",
1268+
"Input cannot be converted to Period",
1269+
]
1270+
)
1271+
with pytest.raises(IncompatibleFrequency, match=msg):
12331272
o + p
12341273

12351274
for freq in ["H", "2H", "3H"]:
@@ -1243,14 +1282,15 @@ def test_add_offset(self):
12431282
assert p + offsets.Hour(3) == exp
12441283
assert offsets.Hour(3) + p == exp
12451284

1285+
msg = "cannot use operands with types"
12461286
exp = Period("2011-04-01 12:00", freq=freq)
12471287
assert p + np.timedelta64(3, "h") == exp
1248-
with pytest.raises(TypeError):
1288+
with pytest.raises(TypeError, match=msg):
12491289
np.timedelta64(3, "h") + p
12501290

12511291
exp = Period("2011-04-01 10:00", freq=freq)
12521292
assert p + np.timedelta64(3600, "s") == exp
1253-
with pytest.raises(TypeError):
1293+
with pytest.raises(TypeError, match=msg):
12541294
np.timedelta64(3600, "s") + p
12551295

12561296
exp = Period("2011-04-01 11:00", freq=freq)
@@ -1268,18 +1308,27 @@ def test_add_offset(self):
12681308
np.timedelta64(3200, "s"),
12691309
timedelta(hours=23, minutes=30),
12701310
]:
1271-
with pytest.raises(IncompatibleFrequency):
1311+
msg = "Input has different freq|Input cannot be converted to Period"
1312+
with pytest.raises(IncompatibleFrequency, match=msg):
12721313
p + o
12731314

12741315
if isinstance(o, np.timedelta64):
1275-
with pytest.raises(TypeError):
1316+
msg = "cannot use operands with types"
1317+
with pytest.raises(TypeError, match=msg):
12761318
o + p
12771319
else:
1278-
with pytest.raises(IncompatibleFrequency):
1320+
msg = "|".join(
1321+
[
1322+
"Input has different freq",
1323+
"Input cannot be converted to Period",
1324+
]
1325+
)
1326+
with pytest.raises(IncompatibleFrequency, match=msg):
12791327
o + p
12801328

12811329
def test_sub_offset(self):
12821330
# freq is DateOffset
1331+
msg = "Input has different freq|Input cannot be converted to Period"
12831332
for freq in ["A", "2A", "3A"]:
12841333
p = Period("2011", freq=freq)
12851334
assert p - offsets.YearEnd(2) == Period("2009", freq=freq)
@@ -1291,7 +1340,7 @@ def test_sub_offset(self):
12911340
np.timedelta64(365, "D"),
12921341
timedelta(365),
12931342
]:
1294-
with pytest.raises(IncompatibleFrequency):
1343+
with pytest.raises(IncompatibleFrequency, match=msg):
12951344
p - o
12961345

12971346
for freq in ["M", "2M", "3M"]:
@@ -1306,7 +1355,7 @@ def test_sub_offset(self):
13061355
np.timedelta64(365, "D"),
13071356
timedelta(365),
13081357
]:
1309-
with pytest.raises(IncompatibleFrequency):
1358+
with pytest.raises(IncompatibleFrequency, match=msg):
13101359
p - o
13111360

13121361
# freq is Tick
@@ -1326,7 +1375,7 @@ def test_sub_offset(self):
13261375
np.timedelta64(4, "h"),
13271376
timedelta(hours=23),
13281377
]:
1329-
with pytest.raises(IncompatibleFrequency):
1378+
with pytest.raises(IncompatibleFrequency, match=msg):
13301379
p - o
13311380

13321381
for freq in ["H", "2H", "3H"]:
@@ -1349,7 +1398,7 @@ def test_sub_offset(self):
13491398
np.timedelta64(3200, "s"),
13501399
timedelta(hours=23, minutes=30),
13511400
]:
1352-
with pytest.raises(IncompatibleFrequency):
1401+
with pytest.raises(IncompatibleFrequency, match=msg):
13531402
p - o
13541403

13551404
@pytest.mark.parametrize("freq", ["M", "2M", "3M"])
@@ -1377,12 +1426,14 @@ def test_period_ops_offset(self):
13771426

13781427
def test_period_immutable():
13791428
# see gh-17116
1429+
msg = "not writable"
1430+
13801431
per = Period("2014Q1")
1381-
with pytest.raises(AttributeError):
1432+
with pytest.raises(AttributeError, match=msg):
13821433
per.ordinal = 14
13831434

13841435
freq = per.freq
1385-
with pytest.raises(AttributeError):
1436+
with pytest.raises(AttributeError, match=msg):
13861437
per.freq = 2 * freq
13871438

13881439

pandas/tests/scalar/test_na_scalar.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ def test_repr():
2323

2424

2525
def test_truthiness():
26-
with pytest.raises(TypeError):
26+
msg = "boolean value of NA is ambiguous"
27+
28+
with pytest.raises(TypeError, match=msg):
2729
bool(NA)
2830

29-
with pytest.raises(TypeError):
31+
with pytest.raises(TypeError, match=msg):
3032
not NA
3133

3234

@@ -145,7 +147,8 @@ def test_logical_and():
145147
assert False & NA is False
146148
assert NA & NA is NA
147149

148-
with pytest.raises(TypeError):
150+
msg = "unsupported operand type"
151+
with pytest.raises(TypeError, match=msg):
149152
NA & 5
150153

151154

@@ -157,7 +160,8 @@ def test_logical_or():
157160
assert False | NA is NA
158161
assert NA | NA is NA
159162

160-
with pytest.raises(TypeError):
163+
msg = "unsupported operand type"
164+
with pytest.raises(TypeError, match=msg):
161165
NA | 5
162166

163167

@@ -169,7 +173,8 @@ def test_logical_xor():
169173
assert False ^ NA is NA
170174
assert NA ^ NA is NA
171175

172-
with pytest.raises(TypeError):
176+
msg = "unsupported operand type"
177+
with pytest.raises(TypeError, match=msg):
173178
NA ^ 5
174179

175180

@@ -216,7 +221,8 @@ def test_ufunc():
216221

217222

218223
def test_ufunc_raises():
219-
with pytest.raises(ValueError, match="ufunc method 'at'"):
224+
msg = "ufunc method 'at'"
225+
with pytest.raises(ValueError, match=msg):
220226
np.log.at(pd.NA, 0)
221227

222228

0 commit comments

Comments
 (0)