Skip to content

Commit 592db7d

Browse files
30999 fix bare pytest raises (#33925)
1 parent 32dd55c commit 592db7d

8 files changed

+53
-38
lines changed

pandas/tests/series/methods/test_asof.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ def test_errors(self):
148148

149149
# non-monotonic
150150
assert not s.index.is_monotonic
151-
with pytest.raises(ValueError):
151+
with pytest.raises(ValueError, match="requires a sorted index"):
152152
s.asof(s.index[0])
153153

154154
# subset with Series
155155
N = 10
156156
rng = date_range("1/1/1990", periods=N, freq="53s")
157157
s = Series(np.random.randn(N), index=rng)
158-
with pytest.raises(ValueError):
158+
with pytest.raises(ValueError, match="not valid for Series"):
159159
s.asof(s.index[0], subset="foo")
160160

161161
def test_all_nans(self):

pandas/tests/series/methods/test_droplevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ def test_droplevel(self):
1515
result = ser.droplevel("b", axis="index")
1616
tm.assert_series_equal(result, expected)
1717
# test that droplevel raises ValueError on axis != 0
18-
with pytest.raises(ValueError):
18+
with pytest.raises(ValueError, match="No axis named columns"):
1919
ser.droplevel(1, axis="columns")

pandas/tests/series/methods/test_tz_localize.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_series_tz_localize_ambiguous_bool(self):
3535
expected0 = Series([expected0])
3636
expected1 = Series([expected1])
3737

38-
with pytest.raises(pytz.AmbiguousTimeError):
38+
with tm.external_error_raised(pytz.AmbiguousTimeError):
3939
ser.dt.tz_localize("US/Central")
4040

4141
result = ser.dt.tz_localize("US/Central", ambiguous=True)
@@ -66,10 +66,10 @@ def test_series_tz_localize_nonexistent(self, tz, method, exp):
6666
dti = date_range(start="2015-03-29 02:00:00", periods=n, freq="min")
6767
s = Series(1, dti)
6868
if method == "raise":
69-
with pytest.raises(pytz.NonExistentTimeError):
69+
with tm.external_error_raised(pytz.NonExistentTimeError):
7070
s.tz_localize(tz, nonexistent=method)
7171
elif exp == "invalid":
72-
with pytest.raises(ValueError):
72+
with pytest.raises(ValueError, match="argument must be one of"):
7373
dti.tz_localize(tz, nonexistent=method)
7474
else:
7575
result = s.tz_localize(tz, nonexistent=method)

pandas/tests/series/test_apply.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,21 @@ def test_transform(self, string_series):
248248

249249
def test_transform_and_agg_error(self, string_series):
250250
# we are trying to transform with an aggregator
251-
with pytest.raises(ValueError):
251+
msg = "transforms cannot produce aggregated results"
252+
with pytest.raises(ValueError, match=msg):
252253
string_series.transform(["min", "max"])
253254

254-
with pytest.raises(ValueError):
255+
msg = "cannot combine transform and aggregation"
256+
with pytest.raises(ValueError, match=msg):
255257
with np.errstate(all="ignore"):
256258
string_series.agg(["sqrt", "max"])
257259

258-
with pytest.raises(ValueError):
260+
with pytest.raises(ValueError, match=msg):
259261
with np.errstate(all="ignore"):
260262
string_series.transform(["sqrt", "max"])
261263

262-
with pytest.raises(ValueError):
264+
msg = "cannot perform both aggregation and transformation"
265+
with pytest.raises(ValueError, match=msg):
263266
with np.errstate(all="ignore"):
264267
string_series.agg({"foo": np.sqrt, "bar": "sum"})
265268

pandas/tests/series/test_arithmetic.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,13 @@ class TestSeriesComparison:
335335
def test_comparison_different_length(self):
336336
a = Series(["a", "b", "c"])
337337
b = Series(["b", "a"])
338-
with pytest.raises(ValueError):
338+
msg = "only compare identically-labeled Series"
339+
with pytest.raises(ValueError, match=msg):
339340
a < b
340341

341342
a = Series([1, 2])
342343
b = Series([2, 3, 4])
343-
with pytest.raises(ValueError):
344+
with pytest.raises(ValueError, match=msg):
344345
a == b
345346

346347
@pytest.mark.parametrize("opname", ["eq", "ne", "gt", "lt", "ge", "le"])
@@ -474,37 +475,40 @@ def test_categorical_comparisons(self):
474475
assert (~(f == a) == (f != a)).all()
475476

476477
# non-equality is not comparable
477-
with pytest.raises(TypeError):
478+
msg = "can only compare equality or not"
479+
with pytest.raises(TypeError, match=msg):
478480
a < b
479-
with pytest.raises(TypeError):
481+
with pytest.raises(TypeError, match=msg):
480482
b < a
481-
with pytest.raises(TypeError):
483+
with pytest.raises(TypeError, match=msg):
482484
a > b
483-
with pytest.raises(TypeError):
485+
with pytest.raises(TypeError, match=msg):
484486
b > a
485487

486488
def test_unequal_categorical_comparison_raises_type_error(self):
487489
# unequal comparison should raise for unordered cats
488490
cat = Series(Categorical(list("abc")))
489-
with pytest.raises(TypeError):
491+
msg = "can only compare equality or not"
492+
with pytest.raises(TypeError, match=msg):
490493
cat > "b"
491494

492495
cat = Series(Categorical(list("abc"), ordered=False))
493-
with pytest.raises(TypeError):
496+
with pytest.raises(TypeError, match=msg):
494497
cat > "b"
495498

496499
# https://github.com/pandas-dev/pandas/issues/9836#issuecomment-92123057
497500
# and following comparisons with scalars not in categories should raise
498501
# for unequal comps, but not for equal/not equal
499502
cat = Series(Categorical(list("abc"), ordered=True))
500503

501-
with pytest.raises(TypeError):
504+
msg = "Cannot compare a Categorical for op.+with a scalar"
505+
with pytest.raises(TypeError, match=msg):
502506
cat < "d"
503-
with pytest.raises(TypeError):
507+
with pytest.raises(TypeError, match=msg):
504508
cat > "d"
505-
with pytest.raises(TypeError):
509+
with pytest.raises(TypeError, match=msg):
506510
"d" < cat
507-
with pytest.raises(TypeError):
511+
with pytest.raises(TypeError, match=msg):
508512
"d" > cat
509513

510514
tm.assert_series_equal(cat == "d", Series([False, False, False]))
@@ -668,10 +672,11 @@ def test_series_add_aware_naive_raises(self):
668672

669673
ser_utc = ser.tz_localize("utc")
670674

671-
with pytest.raises(Exception):
675+
msg = "Cannot join tz-naive with tz-aware DatetimeIndex"
676+
with pytest.raises(Exception, match=msg):
672677
ser + ser_utc
673678

674-
with pytest.raises(Exception):
679+
with pytest.raises(Exception, match=msg):
675680
ser_utc + ser
676681

677682
def test_datetime_understood(self):

pandas/tests/series/test_datetime_values.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,9 @@ def get_dir(s):
244244
s.dt.hour = 5
245245

246246
# trying to set a copy
247+
msg = "modifications to a property of a datetimelike.+not supported"
247248
with pd.option_context("chained_assignment", "raise"):
248-
with pytest.raises(com.SettingWithCopyError):
249+
with pytest.raises(com.SettingWithCopyError, match=msg):
249250
s.dt.hour[0] = 5
250251

251252
@pytest.mark.parametrize(
@@ -311,7 +312,7 @@ def test_dt_round_tz_ambiguous(self, method):
311312
tm.assert_series_equal(result, expected)
312313

313314
# raise
314-
with pytest.raises(pytz.AmbiguousTimeError):
315+
with tm.external_error_raised(pytz.AmbiguousTimeError):
315316
getattr(df1.date.dt, method)("H", ambiguous="raise")
316317

317318
@pytest.mark.parametrize(

pandas/tests/series/test_operators.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,25 @@ def test_logical_operators_int_dtype_with_float(self):
121121
# GH#9016: support bitwise op for integer types
122122
s_0123 = Series(range(4), dtype="int64")
123123

124-
with pytest.raises(TypeError):
124+
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
125+
with pytest.raises(TypeError, match=msg):
125126
s_0123 & np.NaN
126-
with pytest.raises(TypeError):
127+
with pytest.raises(TypeError, match=msg):
127128
s_0123 & 3.14
128-
with pytest.raises(TypeError):
129+
msg = "unsupported operand type.+for &:"
130+
with pytest.raises(TypeError, match=msg):
129131
s_0123 & [0.1, 4, 3.14, 2]
130-
with pytest.raises(TypeError):
132+
with pytest.raises(TypeError, match=msg):
131133
s_0123 & np.array([0.1, 4, 3.14, 2])
132-
with pytest.raises(TypeError):
134+
with pytest.raises(TypeError, match=msg):
133135
s_0123 & Series([0.1, 4, -3.14, 2])
134136

135137
def test_logical_operators_int_dtype_with_str(self):
136138
s_1111 = Series([1] * 4, dtype="int8")
137-
138-
with pytest.raises(TypeError):
139+
msg = "Cannot perform 'and_' with a dtyped.+array and scalar of type"
140+
with pytest.raises(TypeError, match=msg):
139141
s_1111 & "a"
140-
with pytest.raises(TypeError):
142+
with pytest.raises(TypeError, match="unsupported operand.+for &"):
141143
s_1111 & ["a", "b", "c", "d"]
142144

143145
def test_logical_operators_int_dtype_with_bool(self):
@@ -255,7 +257,8 @@ def test_logical_operators_int_dtype_with_bool_dtype_and_reindex(self):
255257
def test_scalar_na_logical_ops_corners(self):
256258
s = Series([2, 3, 4, 5, 6, 7, 8, 9, 10])
257259

258-
with pytest.raises(TypeError):
260+
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
261+
with pytest.raises(TypeError, match=msg):
259262
s & datetime(2005, 1, 1)
260263

261264
s = Series([2, 3, 4, 5, 6, 7, 8, 9, datetime(2005, 1, 1)])
@@ -451,8 +454,9 @@ def test_logical_ops_label_based(self):
451454
expected = Series([True, True, True], index=index)
452455
tm.assert_series_equal(result, expected)
453456

457+
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
454458
for v in [np.nan, "foo"]:
455-
with pytest.raises(TypeError):
459+
with pytest.raises(TypeError, match=msg):
456460
t | v
457461

458462
for v in [False, 0]:
@@ -469,8 +473,9 @@ def test_logical_ops_label_based(self):
469473
result = Series([True, False, True], index=index) & v
470474
expected = Series([False, False, False], index=index)
471475
tm.assert_series_equal(result, expected)
476+
msg = "Cannot perform.+with a dtyped.+array and scalar of type"
472477
for v in [np.nan]:
473-
with pytest.raises(TypeError):
478+
with pytest.raises(TypeError, match=msg):
474479
t & v
475480

476481
def test_logical_ops_df_compat(self):

pandas/tests/tools/test_to_time.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def test_parsers_time(self):
4646
res = to_time(arg, format="%I:%M%p", errors="ignore")
4747
tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))
4848

49-
with pytest.raises(ValueError):
49+
msg = "Cannot convert.+to a time with given format"
50+
with pytest.raises(ValueError, match=msg):
5051
to_time(arg, format="%I:%M%p", errors="raise")
5152

5253
tm.assert_series_equal(

0 commit comments

Comments
 (0)