Skip to content

Commit 383d052

Browse files
gfyoungjreback
authored andcommitted
MAINT: tm.assert_raises_regex --> pytest.raises (#23592)
* MAINT: tm.assert_raises_regex --> pytest.raises pytest.raises has all of the functionality that we need from tm.assert_raises_regex. Closes gh-16521. * Don't remove, just deprecate assert_raises_regex * CLN: Test cleanups and follow-ups
1 parent b930303 commit 383d052

File tree

224 files changed

+1973
-2032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

224 files changed

+1973
-2032
lines changed

pandas/tests/arithmetic/test_datetime64.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def check(get_ser, test_ser):
687687
# with 'operate' (from core/ops.py) for the ops that are not
688688
# defined
689689
op = getattr(get_ser, op_str, None)
690-
with tm.assert_raises_regex(TypeError, 'operate|cannot'):
690+
with pytest.raises(TypeError, match='operate|cannot'):
691691
op(test_ser)
692692

693693
# ## timedelta64 ###
@@ -1042,9 +1042,9 @@ def test_dti_add_timestamp_raises(self, box_with_datetime):
10421042
idx = DatetimeIndex(['2011-01-01', '2011-01-02'])
10431043
idx = tm.box_expected(idx, box_with_datetime)
10441044
msg = "cannot add"
1045-
with tm.assert_raises_regex(TypeError, msg):
1045+
with pytest.raises(TypeError, match=msg):
10461046
idx + Timestamp('2011-01-01')
1047-
with tm.assert_raises_regex(TypeError, msg):
1047+
with pytest.raises(TypeError, match=msg):
10481048
Timestamp('2011-01-01') + idx
10491049

10501050
# -------------------------------------------------------------
@@ -1268,15 +1268,15 @@ def test_dti_sub_tdi(self, tz_naive_fixture):
12681268
tm.assert_index_equal(result, expected)
12691269

12701270
msg = 'cannot subtract .*TimedeltaIndex'
1271-
with tm.assert_raises_regex(TypeError, msg):
1271+
with pytest.raises(TypeError, match=msg):
12721272
tdi - dti
12731273

12741274
# sub with timedelta64 array
12751275
result = dti - tdi.values
12761276
tm.assert_index_equal(result, expected)
12771277

12781278
msg = 'cannot subtract DatetimeIndex from'
1279-
with tm.assert_raises_regex(TypeError, msg):
1279+
with pytest.raises(TypeError, match=msg):
12801280
tdi.values - dti
12811281

12821282
def test_dti_isub_tdi(self, tz_naive_fixture):
@@ -1292,7 +1292,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture):
12921292
tm.assert_index_equal(result, expected)
12931293

12941294
msg = 'cannot subtract .*TimedeltaIndex'
1295-
with tm.assert_raises_regex(TypeError, msg):
1295+
with pytest.raises(TypeError, match=msg):
12961296
tdi -= dti
12971297

12981298
# isub with timedelta64 array
@@ -1303,7 +1303,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture):
13031303
msg = '|'.join(['cannot perform __neg__ with this index type:',
13041304
'ufunc subtract cannot use operands with types',
13051305
'cannot subtract DatetimeIndex from'])
1306-
with tm.assert_raises_regex(TypeError, msg):
1306+
with pytest.raises(TypeError, match=msg):
13071307
tdi.values -= dti
13081308

13091309
# -------------------------------------------------------------
@@ -1323,9 +1323,9 @@ def test_add_datetimelike_and_dti(self, addend, tz):
13231323
# GH#9631
13241324
dti = DatetimeIndex(['2011-01-01', '2011-01-02']).tz_localize(tz)
13251325
msg = 'cannot add DatetimeIndex and {0}'.format(type(addend).__name__)
1326-
with tm.assert_raises_regex(TypeError, msg):
1326+
with pytest.raises(TypeError, match=msg):
13271327
dti + addend
1328-
with tm.assert_raises_regex(TypeError, msg):
1328+
with pytest.raises(TypeError, match=msg):
13291329
addend + dti
13301330

13311331
# -------------------------------------------------------------

pandas/tests/arithmetic/test_period.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,27 @@ def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box_df_fail):
118118
base = tm.box_expected(base, box)
119119

120120
msg = "Input has different freq=A-DEC from "
121-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
121+
with pytest.raises(period.IncompatibleFrequency, match=msg):
122122
base <= Period('2011', freq='A')
123123

124-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
124+
with pytest.raises(period.IncompatibleFrequency, match=msg):
125125
Period('2011', freq='A') >= base
126126

127127
# TODO: Could parametrize over boxes for idx?
128128
idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='A')
129-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
129+
with pytest.raises(period.IncompatibleFrequency, match=msg):
130130
base <= idx
131131

132132
# Different frequency
133133
msg = "Input has different freq=4M from "
134-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
134+
with pytest.raises(period.IncompatibleFrequency, match=msg):
135135
base <= Period('2011', freq='4M')
136136

137-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
137+
with pytest.raises(period.IncompatibleFrequency, match=msg):
138138
Period('2011', freq='4M') >= base
139139

140140
idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='4M')
141-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
141+
with pytest.raises(period.IncompatibleFrequency, match=msg):
142142
base <= idx
143143

144144
@pytest.mark.parametrize('freq', ['M', '2M', '3M'])
@@ -190,10 +190,10 @@ def test_pi_cmp_nat_mismatched_freq_raises(self, freq):
190190

191191
diff = PeriodIndex(['2011-02', '2011-01', '2011-04', 'NaT'], freq='4M')
192192
msg = "Input has different freq=4M from PeriodIndex"
193-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
193+
with pytest.raises(period.IncompatibleFrequency, match=msg):
194194
idx1 > diff
195195

196-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
196+
with pytest.raises(period.IncompatibleFrequency, match=msg):
197197
idx1 == diff
198198

199199
# TODO: De-duplicate with test_pi_cmp_nat
@@ -708,13 +708,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily):
708708
other = not_daily
709709
rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
710710
msg = 'Input has different freq(=.+)? from Period.*?\\(freq=D\\)'
711-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
711+
with pytest.raises(period.IncompatibleFrequency, match=msg):
712712
rng + other
713-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
713+
with pytest.raises(period.IncompatibleFrequency, match=msg):
714714
rng += other
715-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
715+
with pytest.raises(period.IncompatibleFrequency, match=msg):
716716
rng - other
717-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
717+
with pytest.raises(period.IncompatibleFrequency, match=msg):
718718
rng -= other
719719

720720
def test_pi_add_iadd_timedeltalike_hourly(self, two_hours):
@@ -734,10 +734,10 @@ def test_pi_add_timedeltalike_mismatched_freq_hourly(self, not_hourly):
734734
rng = pd.period_range('2014-01-01 10:00', '2014-01-05 10:00', freq='H')
735735
msg = 'Input has different freq(=.+)? from Period.*?\\(freq=H\\)'
736736

737-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
737+
with pytest.raises(period.IncompatibleFrequency, match=msg):
738738
rng + other
739739

740-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
740+
with pytest.raises(period.IncompatibleFrequency, match=msg):
741741
rng += other
742742

743743
def test_pi_sub_isub_timedeltalike_hourly(self, two_hours):
@@ -768,13 +768,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_annual(self,
768768
rng = pd.period_range('2014', '2024', freq='A')
769769
msg = ('Input has different freq(=.+)? '
770770
'from Period.*?\\(freq=A-DEC\\)')
771-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
771+
with pytest.raises(period.IncompatibleFrequency, match=msg):
772772
rng + other
773-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
773+
with pytest.raises(period.IncompatibleFrequency, match=msg):
774774
rng += other
775-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
775+
with pytest.raises(period.IncompatibleFrequency, match=msg):
776776
rng - other
777-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
777+
with pytest.raises(period.IncompatibleFrequency, match=msg):
778778
rng -= other
779779

780780
def test_pi_add_iadd_timedeltalike_M(self):
@@ -792,13 +792,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self,
792792
other = mismatched_freq
793793
rng = pd.period_range('2014-01', '2016-12', freq='M')
794794
msg = 'Input has different freq(=.+)? from Period.*?\\(freq=M\\)'
795-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
795+
with pytest.raises(period.IncompatibleFrequency, match=msg):
796796
rng + other
797-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
797+
with pytest.raises(period.IncompatibleFrequency, match=msg):
798798
rng += other
799-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
799+
with pytest.raises(period.IncompatibleFrequency, match=msg):
800800
rng - other
801-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
801+
with pytest.raises(period.IncompatibleFrequency, match=msg):
802802
rng -= other
803803

804804
def test_parr_add_sub_td64_nat(self, box):
@@ -907,14 +907,14 @@ def test_pi_ops_errors(self, ng, box_with_period):
907907
obj = tm.box_expected(idx, box_with_period)
908908

909909
msg = r"unsupported operand type\(s\)"
910-
with tm.assert_raises_regex(TypeError, msg):
910+
with pytest.raises(TypeError, match=msg):
911911
obj + ng
912912

913913
with pytest.raises(TypeError):
914914
# error message differs between PY2 and 3
915915
ng + obj
916916

917-
with tm.assert_raises_regex(TypeError, msg):
917+
with pytest.raises(TypeError, match=msg):
918918
obj - ng
919919

920920
with pytest.raises(TypeError):
@@ -1009,13 +1009,13 @@ def test_pi_offset_errors(self):
10091009
# from Period
10101010
msg = r"Input has different freq from Period.*?\(freq=D\)"
10111011
for obj in [idx, ser]:
1012-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
1012+
with pytest.raises(period.IncompatibleFrequency, match=msg):
10131013
obj + pd.offsets.Hour(2)
10141014

1015-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
1015+
with pytest.raises(period.IncompatibleFrequency, match=msg):
10161016
pd.offsets.Hour(2) + obj
10171017

1018-
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
1018+
with pytest.raises(period.IncompatibleFrequency, match=msg):
10191019
obj - pd.offsets.Hour(2)
10201020

10211021
def test_pi_sub_period(self):

pandas/tests/arithmetic/test_timedelta64.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,22 @@ def test_tdi_add_timestamp_nat_masking(self):
161161
def test_tdi_add_overflow(self):
162162
# See GH#14068
163163
msg = "too (big|large) to convert"
164-
with tm.assert_raises_regex(OverflowError, msg):
164+
with pytest.raises(OverflowError, match=msg):
165165
pd.to_timedelta(106580, 'D') + Timestamp('2000')
166-
with tm.assert_raises_regex(OverflowError, msg):
166+
with pytest.raises(OverflowError, match=msg):
167167
Timestamp('2000') + pd.to_timedelta(106580, 'D')
168168

169169
_NaT = int(pd.NaT) + 1
170170
msg = "Overflow in int64 addition"
171-
with tm.assert_raises_regex(OverflowError, msg):
171+
with pytest.raises(OverflowError, match=msg):
172172
pd.to_timedelta([106580], 'D') + Timestamp('2000')
173-
with tm.assert_raises_regex(OverflowError, msg):
173+
with pytest.raises(OverflowError, match=msg):
174174
Timestamp('2000') + pd.to_timedelta([106580], 'D')
175-
with tm.assert_raises_regex(OverflowError, msg):
175+
with pytest.raises(OverflowError, match=msg):
176176
pd.to_timedelta([_NaT]) - Timedelta('1 days')
177-
with tm.assert_raises_regex(OverflowError, msg):
177+
with pytest.raises(OverflowError, match=msg):
178178
pd.to_timedelta(['5 days', _NaT]) - Timedelta('1 days')
179-
with tm.assert_raises_regex(OverflowError, msg):
179+
with pytest.raises(OverflowError, match=msg):
180180
(pd.to_timedelta([_NaT, '5 days', '1 hours']) -
181181
pd.to_timedelta(['7 seconds', _NaT, '4 hours']))
182182

@@ -415,7 +415,7 @@ def test_td64arr_sub_timestamp_raises(self, box):
415415
msg = ("cannot subtract a datelike from|"
416416
"Could not operate|"
417417
"cannot perform operation")
418-
with tm.assert_raises_regex(TypeError, msg):
418+
with pytest.raises(TypeError, match=msg):
419419
idx - Timestamp('2011-01-01')
420420

421421
def test_td64arr_add_timestamp(self, box, tz_naive_fixture):
@@ -1217,9 +1217,9 @@ def test_td64arr_mul_tdscalar_invalid(self, box, scalar_td):
12171217
# with 'operate' (from core/ops.py) for the ops that are not
12181218
# defined
12191219
pattern = 'operate|unsupported|cannot|not supported'
1220-
with tm.assert_raises_regex(TypeError, pattern):
1220+
with pytest.raises(TypeError, match=pattern):
12211221
td1 * scalar_td
1222-
with tm.assert_raises_regex(TypeError, pattern):
1222+
with pytest.raises(TypeError, match=pattern):
12231223
scalar_td * td1
12241224

12251225
def test_td64arr_mul_too_short_raises(self, box):
@@ -1399,8 +1399,8 @@ def test_td64arr_pow_invalid(self, scalar_td, box):
13991399
# with 'operate' (from core/ops.py) for the ops that are not
14001400
# defined
14011401
pattern = 'operate|unsupported|cannot|not supported'
1402-
with tm.assert_raises_regex(TypeError, pattern):
1402+
with pytest.raises(TypeError, match=pattern):
14031403
scalar_td ** td1
14041404

1405-
with tm.assert_raises_regex(TypeError, pattern):
1405+
with pytest.raises(TypeError, match=pattern):
14061406
td1 ** scalar_td

pandas/tests/arrays/categorical/test_algos.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ def test_take_fill_value_new_raises(self):
138138
# https://github.com/pandas-dev/pandas/issues/23296
139139
cat = pd.Categorical(['a', 'b', 'c'])
140140
xpr = r"'fill_value' \('d'\) is not in this Categorical's categories."
141-
with tm.assert_raises_regex(TypeError, xpr):
141+
with pytest.raises(TypeError, match=xpr):
142142
cat.take([0, 1, -1], fill_value='d', allow_fill=True)

pandas/tests/arrays/categorical/test_analytics.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ def test_numpy_repeat(self):
305305
tm.assert_categorical_equal(np.repeat(cat, 2), exp)
306306

307307
msg = "the 'axis' parameter is not supported"
308-
tm.assert_raises_regex(ValueError, msg, np.repeat, cat, 2, axis=1)
308+
with pytest.raises(ValueError, match=msg):
309+
np.repeat(cat, 2, axis=1)
309310

310311
def test_isna(self):
311312
exp = np.array([False, False, True])

pandas/tests/arrays/categorical/test_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def test_set_ordered(self):
5050

5151
# removed in 0.19.0
5252
msg = "can\'t set attribute"
53-
with tm.assert_raises_regex(AttributeError, msg):
53+
with pytest.raises(AttributeError, match=msg):
5454
cat.ordered = True
55-
with tm.assert_raises_regex(AttributeError, msg):
55+
with pytest.raises(AttributeError, match=msg):
5656
cat.ordered = False
5757

5858
def test_rename_categories(self):

pandas/tests/arrays/categorical/test_constructors.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def test_validate_ordered(self):
2626
# This should be a boolean.
2727
ordered = np.array([0, 1, 2])
2828

29-
with tm.assert_raises_regex(exp_err, exp_msg):
29+
with pytest.raises(exp_err, match=exp_msg):
3030
Categorical([1, 2, 3], ordered=ordered)
3131

32-
with tm.assert_raises_regex(exp_err, exp_msg):
32+
with pytest.raises(exp_err, match=exp_msg):
3333
Categorical.from_codes([0, 0, 1], categories=['a', 'b', 'c'],
3434
ordered=ordered)
3535

@@ -351,13 +351,13 @@ def test_constructor_with_dtype(self, ordered):
351351

352352
def test_constructor_dtype_and_others_raises(self):
353353
dtype = CategoricalDtype(['a', 'b'], ordered=True)
354-
with tm.assert_raises_regex(ValueError, "Cannot"):
354+
with pytest.raises(ValueError, match="Cannot"):
355355
Categorical(['a', 'b'], categories=['a', 'b'], dtype=dtype)
356356

357-
with tm.assert_raises_regex(ValueError, "Cannot"):
357+
with pytest.raises(ValueError, match="Cannot"):
358358
Categorical(['a', 'b'], ordered=True, dtype=dtype)
359359

360-
with tm.assert_raises_regex(ValueError, "Cannot"):
360+
with pytest.raises(ValueError, match="Cannot"):
361361
Categorical(['a', 'b'], ordered=False, dtype=dtype)
362362

363363
@pytest.mark.parametrize('categories', [
@@ -372,7 +372,7 @@ def test_constructor_str_category(self, categories, ordered):
372372
tm.assert_categorical_equal(result, expected)
373373

374374
def test_constructor_str_unknown(self):
375-
with tm.assert_raises_regex(ValueError, "Unknown `dtype`"):
375+
with pytest.raises(ValueError, match="Unknown `dtype`"):
376376
Categorical([1, 2], dtype="foo")
377377

378378
def test_constructor_from_categorical_with_dtype(self):

pandas/tests/arrays/categorical/test_dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def test_astype(self, ordered):
120120
tm.assert_numpy_array_equal(result, expected)
121121

122122
msg = 'could not convert string to float'
123-
with tm.assert_raises_regex(ValueError, msg):
123+
with pytest.raises(ValueError, match=msg):
124124
cat.astype(float)
125125

126126
# numeric

pandas/tests/arrays/categorical/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,5 @@ def test_mask_with_boolean_raises(index):
143143
if index:
144144
idx = CategoricalIndex(idx)
145145

146-
with tm.assert_raises_regex(ValueError, 'NA / NaN'):
146+
with pytest.raises(ValueError, match='NA / NaN'):
147147
s[idx]

pandas/tests/arrays/categorical/test_missing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_fillna_raises(self, fillna_kwargs, msg):
7070
# https://github.com/pandas-dev/pandas/issues/19682
7171
cat = Categorical([1, 2, 3])
7272

73-
with tm.assert_raises_regex(ValueError, msg):
73+
with pytest.raises(ValueError, match=msg):
7474
cat.fillna(**fillna_kwargs)
7575

7676
@pytest.mark.parametrize("named", [True, False])

pandas/tests/arrays/categorical/test_operators.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,17 @@ def test_unordered_different_order_equal(self, ctor):
238238
def test_unordered_different_categories_raises(self):
239239
c1 = Categorical(['a', 'b'], categories=['a', 'b'], ordered=False)
240240
c2 = Categorical(['a', 'c'], categories=['c', 'a'], ordered=False)
241-
with tm.assert_raises_regex(TypeError,
242-
"Categoricals can only be compared"):
241+
242+
with pytest.raises(TypeError, match=("Categoricals can "
243+
"only be compared")):
243244
c1 == c2
244245

245246
def test_compare_different_lengths(self):
246247
c1 = Categorical([], categories=['a', 'b'])
247248
c2 = Categorical([], categories=['a'])
249+
248250
msg = "Categories are different lengths"
249-
with tm.assert_raises_regex(TypeError, msg):
251+
with pytest.raises(TypeError, match=msg):
250252
c1 == c2
251253

252254
def test_compare_unordered_different_order(self):

0 commit comments

Comments
 (0)