Skip to content

Commit

Permalink
MAINT: tm.assert_raises_regex --> pytest.raises
Browse files Browse the repository at this point in the history
pytest.raises has all of the functionality that
we need from tm.assert_raises_regex.

Closes pandas-devgh-16521.
  • Loading branch information
gfyoung committed Nov 10, 2018
1 parent 8ed92ef commit da8ab9b
Show file tree
Hide file tree
Showing 224 changed files with 1,943 additions and 2,139 deletions.
18 changes: 9 additions & 9 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ def check(get_ser, test_ser):
# with 'operate' (from core/ops.py) for the ops that are not
# defined
op = getattr(get_ser, op_str, None)
with tm.assert_raises_regex(TypeError, 'operate|cannot'):
with pytest.raises(TypeError, match='operate|cannot'):
op(test_ser)

# ## timedelta64 ###
Expand Down Expand Up @@ -1042,9 +1042,9 @@ def test_dti_add_timestamp_raises(self, box_with_datetime):
idx = DatetimeIndex(['2011-01-01', '2011-01-02'])
idx = tm.box_expected(idx, box_with_datetime)
msg = "cannot add"
with tm.assert_raises_regex(TypeError, msg):
with pytest.raises(TypeError, match=msg):
idx + Timestamp('2011-01-01')
with tm.assert_raises_regex(TypeError, msg):
with pytest.raises(TypeError, match=msg):
Timestamp('2011-01-01') + idx

# -------------------------------------------------------------
Expand Down Expand Up @@ -1268,15 +1268,15 @@ def test_dti_sub_tdi(self, tz_naive_fixture):
tm.assert_index_equal(result, expected)

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

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

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

def test_dti_isub_tdi(self, tz_naive_fixture):
Expand All @@ -1292,7 +1292,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture):
tm.assert_index_equal(result, expected)

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

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

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

# -------------------------------------------------------------
Expand Down
54 changes: 27 additions & 27 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,27 @@ def test_parr_cmp_pi_mismatched_freq_raises(self, freq, box_df_fail):
base = tm.box_expected(base, box)

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

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

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

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

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

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

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

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

with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
idx1 == diff

# TODO: De-duplicate with test_pi_cmp_nat
Expand Down Expand Up @@ -708,13 +708,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_daily(self, not_daily):
other = not_daily
rng = pd.period_range('2014-05-01', '2014-05-15', freq='D')
msg = 'Input has different freq(=.+)? from Period.*?\\(freq=D\\)'
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng + other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng += other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng - other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng -= other

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

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

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

def test_pi_sub_isub_timedeltalike_hourly(self, two_hours):
Expand Down Expand Up @@ -768,13 +768,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_annual(self,
rng = pd.period_range('2014', '2024', freq='A')
msg = ('Input has different freq(=.+)? '
'from Period.*?\\(freq=A-DEC\\)')
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng + other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng += other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng - other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng -= other

def test_pi_add_iadd_timedeltalike_M(self):
Expand All @@ -792,13 +792,13 @@ def test_pi_add_sub_timedeltalike_freq_mismatch_monthly(self,
other = mismatched_freq
rng = pd.period_range('2014-01', '2016-12', freq='M')
msg = 'Input has different freq(=.+)? from Period.*?\\(freq=M\\)'
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng + other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng += other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng - other
with tm.assert_raises_regex(period.IncompatibleFrequency, msg):
with pytest.raises(period.IncompatibleFrequency, match=msg):
rng -= other

def test_parr_add_sub_td64_nat(self, box):
Expand Down Expand Up @@ -907,14 +907,14 @@ def test_pi_ops_errors(self, ng, box_with_period):
obj = tm.box_expected(idx, box_with_period)

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

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

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

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

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

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

def test_pi_sub_period(self):
Expand Down
24 changes: 12 additions & 12 deletions pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,22 @@ def test_tdi_add_timestamp_nat_masking(self):
def test_tdi_add_overflow(self):
# See GH#14068
msg = "too (big|large) to convert"
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
pd.to_timedelta(106580, 'D') + Timestamp('2000')
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
Timestamp('2000') + pd.to_timedelta(106580, 'D')

_NaT = int(pd.NaT) + 1
msg = "Overflow in int64 addition"
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
pd.to_timedelta([106580], 'D') + Timestamp('2000')
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
Timestamp('2000') + pd.to_timedelta([106580], 'D')
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
pd.to_timedelta([_NaT]) - Timedelta('1 days')
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
pd.to_timedelta(['5 days', _NaT]) - Timedelta('1 days')
with tm.assert_raises_regex(OverflowError, msg):
with pytest.raises(OverflowError, match=msg):
(pd.to_timedelta([_NaT, '5 days', '1 hours']) -
pd.to_timedelta(['7 seconds', _NaT, '4 hours']))

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

def test_td64arr_add_timestamp(self, box, tz_naive_fixture):
Expand Down Expand Up @@ -1217,9 +1217,9 @@ def test_td64arr_mul_tdscalar_invalid(self, box, scalar_td):
# with 'operate' (from core/ops.py) for the ops that are not
# defined
pattern = 'operate|unsupported|cannot|not supported'
with tm.assert_raises_regex(TypeError, pattern):
with pytest.raises(TypeError, match=pattern):
td1 * scalar_td
with tm.assert_raises_regex(TypeError, pattern):
with pytest.raises(TypeError, match=pattern):
scalar_td * td1

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

with tm.assert_raises_regex(TypeError, pattern):
with pytest.raises(TypeError, match=pattern):
td1 ** scalar_td
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ def test_take_fill_value_new_raises(self):
# https://github.com/pandas-dev/pandas/issues/23296
cat = pd.Categorical(['a', 'b', 'c'])
xpr = r"'fill_value' \('d'\) is not in this Categorical's categories."
with tm.assert_raises_regex(TypeError, xpr):
with pytest.raises(TypeError, match=xpr):
cat.take([0, 1, -1], fill_value='d', allow_fill=True)
3 changes: 2 additions & 1 deletion pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ def test_numpy_repeat(self):
tm.assert_categorical_equal(np.repeat(cat, 2), exp)

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

def test_isna(self):
exp = np.array([False, False, True])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/arrays/categorical/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def test_set_ordered(self):

# removed in 0.19.0
msg = "can\'t set attribute"
with tm.assert_raises_regex(AttributeError, msg):
with pytest.raises(AttributeError, match=msg):
cat.ordered = True
with tm.assert_raises_regex(AttributeError, msg):
with pytest.raises(AttributeError, match=msg):
cat.ordered = False

def test_rename_categories(self):
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def test_validate_ordered(self):
# This should be a boolean.
ordered = np.array([0, 1, 2])

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

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

Expand Down Expand Up @@ -351,13 +351,13 @@ def test_constructor_with_dtype(self, ordered):

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

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

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

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

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

def test_constructor_from_categorical_with_dtype(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_astype(self, ordered):
tm.assert_numpy_array_equal(result, expected)

msg = 'could not convert string to float'
with tm.assert_raises_regex(ValueError, msg):
with pytest.raises(ValueError, match=msg):
cat.astype(float)

# numeric
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@ def test_mask_with_boolean_raises(index):
if index:
idx = CategoricalIndex(idx)

with tm.assert_raises_regex(ValueError, 'NA / NaN'):
with pytest.raises(ValueError, match='NA / NaN'):
s[idx]
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_fillna_raises(self, fillna_kwargs, msg):
# https://github.com/pandas-dev/pandas/issues/19682
cat = Categorical([1, 2, 3])

with tm.assert_raises_regex(ValueError, msg):
with pytest.raises(ValueError, match=msg):
cat.fillna(**fillna_kwargs)

@pytest.mark.parametrize("named", [True, False])
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,17 @@ def test_unordered_different_order_equal(self, ctor):
def test_unordered_different_categories_raises(self):
c1 = Categorical(['a', 'b'], categories=['a', 'b'], ordered=False)
c2 = Categorical(['a', 'c'], categories=['c', 'a'], ordered=False)
with tm.assert_raises_regex(TypeError,
"Categoricals can only be compared"):

with pytest.raises(TypeError, match=("Categoricals can "
"only be compared")):
c1 == c2

def test_compare_different_lengths(self):
c1 = Categorical([], categories=['a', 'b'])
c2 = Categorical([], categories=['a'])

msg = "Categories are different lengths"
with tm.assert_raises_regex(TypeError, msg):
with pytest.raises(TypeError, match=msg):
c1 == c2

def test_compare_unordered_different_order(self):
Expand Down
Loading

0 comments on commit da8ab9b

Please sign in to comment.