Skip to content

Commit

Permalink
MAINT: Rename assertRaisesRegexp to assert_raises_regex (#16119)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung authored and jreback committed Apr 26, 2017
1 parent a940868 commit 1a93759
Show file tree
Hide file tree
Showing 132 changed files with 1,397 additions and 1,283 deletions.
46 changes: 24 additions & 22 deletions pandas/tests/computation/test_eval.py
Expand Up @@ -29,9 +29,8 @@
import pandas.core.computation.expr as expr
import pandas.util.testing as tm
from pandas.util.testing import (assert_frame_equal, randbool,
assertRaisesRegexp, assert_numpy_array_equal,
assert_produces_warning, assert_series_equal,
slow)
assert_numpy_array_equal, assert_series_equal,
assert_produces_warning, slow)
from pandas.compat import PY3, reduce

_series_frame_incompatible = _bool_ops_syms
Expand Down Expand Up @@ -1677,17 +1676,17 @@ def test_result_types2(self):

def test_undefined_func(self):
df = DataFrame({'a': np.random.randn(10)})
with tm.assertRaisesRegexp(ValueError,
"\"mysin\" is not a supported function"):
with tm.assert_raises_regex(
ValueError, "\"mysin\" is not a supported function"):
df.eval("mysin(a)",
engine=self.engine,
parser=self.parser)

def test_keyword_arg(self):
df = DataFrame({'a': np.random.randn(10)})
with tm.assertRaisesRegexp(TypeError,
"Function \"sin\" does not support "
"keyword arguments"):
with tm.assert_raises_regex(TypeError,
"Function \"sin\" does not support "
"keyword arguments"):
df.eval("sin(x=a)",
engine=self.engine,
parser=self.parser)
Expand Down Expand Up @@ -1748,16 +1747,16 @@ def test_no_new_globals(self, engine, parser):

def test_invalid_engine():
tm.skip_if_no_ne()
assertRaisesRegexp(KeyError, 'Invalid engine \'asdf\' passed',
pd.eval, 'x + y', local_dict={'x': 1, 'y': 2},
engine='asdf')
tm.assert_raises_regex(KeyError, 'Invalid engine \'asdf\' passed',
pd.eval, 'x + y', local_dict={'x': 1, 'y': 2},
engine='asdf')


def test_invalid_parser():
tm.skip_if_no_ne()
assertRaisesRegexp(KeyError, 'Invalid parser \'asdf\' passed',
pd.eval, 'x + y', local_dict={'x': 1, 'y': 2},
parser='asdf')
tm.assert_raises_regex(KeyError, 'Invalid parser \'asdf\' passed',
pd.eval, 'x + y', local_dict={'x': 1, 'y': 2},
parser='asdf')


_parsers = {'python': PythonExprVisitor, 'pytables': pytables.ExprVisitor,
Expand Down Expand Up @@ -1795,18 +1794,20 @@ def test_invalid_local_variable_reference(engine, parser):

for _expr in exprs:
if parser != 'pandas':
with tm.assertRaisesRegexp(SyntaxError, "The '@' prefix is only"):
with tm.assert_raises_regex(SyntaxError,
"The '@' prefix is only"):
pd.eval(_expr, engine=engine, parser=parser)
else:
with tm.assertRaisesRegexp(SyntaxError, "The '@' prefix is not"):
with tm.assert_raises_regex(SyntaxError,
"The '@' prefix is not"):
pd.eval(_expr, engine=engine, parser=parser)


def test_numexpr_builtin_raises(engine, parser):
sin, dotted_line = 1, 2
if engine == 'numexpr':
with tm.assertRaisesRegexp(NumExprClobberingError,
'Variables in expression .+'):
with tm.assert_raises_regex(NumExprClobberingError,
'Variables in expression .+'):
pd.eval('sin + dotted_line', engine=engine, parser=parser)
else:
res = pd.eval('sin + dotted_line', engine=engine, parser=parser)
Expand All @@ -1815,20 +1816,21 @@ def test_numexpr_builtin_raises(engine, parser):

def test_bad_resolver_raises(engine, parser):
cannot_resolve = 42, 3.0
with tm.assertRaisesRegexp(TypeError, 'Resolver of type .+'):
with tm.assert_raises_regex(TypeError, 'Resolver of type .+'):
pd.eval('1 + 2', resolvers=cannot_resolve, engine=engine,
parser=parser)


def test_empty_string_raises(engine, parser):
# GH 13139
with tm.assertRaisesRegexp(ValueError, 'expr cannot be an empty string'):
with tm.assert_raises_regex(ValueError,
'expr cannot be an empty string'):
pd.eval('', engine=engine, parser=parser)


def test_more_than_one_expression_raises(engine, parser):
with tm.assertRaisesRegexp(SyntaxError,
'only a single expression is allowed'):
with tm.assert_raises_regex(SyntaxError,
'only a single expression is allowed'):
pd.eval('1 + 1; 2 + 2', engine=engine, parser=parser)


Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/dtypes/test_inference.py
Expand Up @@ -257,7 +257,7 @@ def test_maybe_convert_numeric_infinities(self):
tm.assert_numpy_array_equal(out, pos)

# too many characters
with tm.assertRaisesRegexp(ValueError, msg):
with tm.assert_raises_regex(ValueError, msg):
lib.maybe_convert_numeric(
np.array(['foo_' + infinity], dtype=object),
na_values, maybe_int)
Expand Down Expand Up @@ -320,7 +320,7 @@ def test_convert_numeric_uint64_nan(self):
for coerce in (True, False):
for arr, na_values in cases:
if coerce:
with tm.assertRaisesRegexp(ValueError, msg):
with tm.assert_raises_regex(ValueError, msg):
lib.maybe_convert_numeric(arr, na_values,
coerce_numeric=coerce)
else:
Expand All @@ -339,7 +339,7 @@ def test_convert_numeric_int64_uint64(self):
for coerce in (True, False):
for case in cases:
if coerce:
with tm.assertRaisesRegexp(ValueError, msg):
with tm.assert_raises_regex(ValueError, msg):
lib.maybe_convert_numeric(case, set(),
coerce_numeric=coerce)
else:
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/frame/test_alter_axes.py
Expand Up @@ -18,9 +18,7 @@
is_interval_dtype)
import pandas as pd

from pandas.util.testing import (assert_series_equal,
assert_frame_equal,
assertRaisesRegexp)
from pandas.util.testing import assert_series_equal, assert_frame_equal

import pandas.util.testing as tm

Expand All @@ -36,7 +34,7 @@ def test_set_index(self):
_ = self.mixed_frame['foo'] # noqa
self.mixed_frame.index = idx
assert self.mixed_frame['foo'].index is idx
with assertRaisesRegexp(ValueError, 'Length mismatch'):
with tm.assert_raises_regex(ValueError, 'Length mismatch'):
self.mixed_frame.index = idx[::2]

def test_set_index_cast(self):
Expand Down Expand Up @@ -111,7 +109,8 @@ def test_set_index2(self):
assert_frame_equal(df3, expected_nodrop)

# corner case
with assertRaisesRegexp(ValueError, 'Index has duplicate keys'):
with tm.assert_raises_regex(ValueError,
'Index has duplicate keys'):
df.set_index('A', verify_integrity=True)

# append
Expand All @@ -136,7 +135,8 @@ def test_set_index_nonuniq(self):
'C': ['a', 'b', 'c', 'd', 'e'],
'D': np.random.randn(5),
'E': np.random.randn(5)})
with assertRaisesRegexp(ValueError, 'Index has duplicate keys'):
with tm.assert_raises_regex(ValueError,
'Index has duplicate keys'):
df.set_index('A', verify_integrity=True, inplace=True)
assert 'A' in df

Expand Down Expand Up @@ -338,7 +338,7 @@ def test_set_index_empty_column(self):
def test_set_columns(self):
cols = Index(np.arange(len(self.mixed_frame.columns)))
self.mixed_frame.columns = cols
with assertRaisesRegexp(ValueError, 'Length mismatch'):
with tm.assert_raises_regex(ValueError, 'Length mismatch'):
self.mixed_frame.columns = cols[::2]

def test_dti_set_index_reindex(self):
Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/frame/test_analytics.py
Expand Up @@ -779,7 +779,7 @@ def wrapper(x):
# assert_series_equal(result, comp)

# bad axis
tm.assertRaisesRegexp(ValueError, 'No axis named 2', f, axis=2)
tm.assert_raises_regex(ValueError, 'No axis named 2', f, axis=2)
# make sure works on mixed-type frame
getattr(self.mixed_frame, name)(axis=0)
getattr(self.mixed_frame, name)(axis=1)
Expand Down Expand Up @@ -1749,7 +1749,7 @@ def test_numpy_round(self):
tm.assert_frame_equal(out, expected)

msg = "the 'out' parameter is not supported"
with tm.assertRaisesRegexp(ValueError, msg):
with tm.assert_raises_regex(ValueError, msg):
np.round(df, decimals=0, out=df)

def test_round_mixed_type(self):
Expand Down Expand Up @@ -1897,7 +1897,8 @@ def test_dot(self):
exp = a.dot(a.iloc[0])
tm.assert_series_equal(result, exp)

with tm.assertRaisesRegexp(ValueError, 'Dot product shape mismatch'):
with tm.assert_raises_regex(ValueError,
'Dot product shape mismatch'):
a.dot(row[:-1])

a = np.random.rand(1, 5)
Expand All @@ -1914,7 +1915,7 @@ def test_dot(self):
df = DataFrame(randn(3, 4), index=[1, 2, 3], columns=lrange(4))
df2 = DataFrame(randn(5, 3), index=lrange(5), columns=[1, 2, 3])

with tm.assertRaisesRegexp(ValueError, 'aligned'):
with tm.assert_raises_regex(ValueError, 'aligned'):
df.dot(df2)


Expand Down Expand Up @@ -1986,7 +1987,7 @@ def test_n(self, df_strings, method, n, order):

error_msg = self.dtype_error_msg_template.format(
column='b', method=method, dtype='object')
with tm.assertRaisesRegexp(TypeError, error_msg):
with tm.assert_raises_regex(TypeError, error_msg):
getattr(df, method)(n, order)
else:
ascending = method == 'nsmallest'
Expand All @@ -2003,7 +2004,7 @@ def test_n_error(self, df_main_dtypes, method, columns):
df = df_main_dtypes
error_msg = self.dtype_error_msg_template.format(
column=columns[1], method=method, dtype=df[columns[1]].dtype)
with tm.assertRaisesRegexp(TypeError, error_msg):
with tm.assert_raises_regex(TypeError, error_msg):
getattr(df, method)(2, columns)

def test_n_all_dtypes(self, df_main_dtypes):
Expand Down
16 changes: 9 additions & 7 deletions pandas/tests/frame/test_api.py
Expand Up @@ -20,8 +20,7 @@

from pandas.util.testing import (assert_almost_equal,
assert_series_equal,
assert_frame_equal,
assertRaisesRegexp)
assert_frame_equal)

import pandas.util.testing as tm

Expand Down Expand Up @@ -91,11 +90,14 @@ def test_get_axis(self):
assert f._get_axis(0) is f.index
assert f._get_axis(1) is f.columns

assertRaisesRegexp(ValueError, 'No axis named', f._get_axis_number, 2)
assertRaisesRegexp(ValueError, 'No axis.*foo', f._get_axis_name, 'foo')
assertRaisesRegexp(ValueError, 'No axis.*None', f._get_axis_name, None)
assertRaisesRegexp(ValueError, 'No axis named', f._get_axis_number,
None)
tm.assert_raises_regex(
ValueError, 'No axis named', f._get_axis_number, 2)
tm.assert_raises_regex(
ValueError, 'No axis.*foo', f._get_axis_name, 'foo')
tm.assert_raises_regex(
ValueError, 'No axis.*None', f._get_axis_name, None)
tm.assert_raises_regex(ValueError, 'No axis named',
f._get_axis_number, None)

def test_keys(self):
getkeys = self.frame.keys
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/frame/test_axis_select_reindex.py
Expand Up @@ -699,23 +699,23 @@ def test_filter(self):
tm.assert_frame_equal(filtered, expected)

# pass in None
with tm.assertRaisesRegexp(TypeError, 'Must pass'):
with tm.assert_raises_regex(TypeError, 'Must pass'):
self.frame.filter()
with tm.assertRaisesRegexp(TypeError, 'Must pass'):
with tm.assert_raises_regex(TypeError, 'Must pass'):
self.frame.filter(items=None)
with tm.assertRaisesRegexp(TypeError, 'Must pass'):
with tm.assert_raises_regex(TypeError, 'Must pass'):
self.frame.filter(axis=1)

# test mutually exclusive arguments
with tm.assertRaisesRegexp(TypeError, 'mutually exclusive'):
with tm.assert_raises_regex(TypeError, 'mutually exclusive'):
self.frame.filter(items=['one', 'three'], regex='e$', like='bbi')
with tm.assertRaisesRegexp(TypeError, 'mutually exclusive'):
with tm.assert_raises_regex(TypeError, 'mutually exclusive'):
self.frame.filter(items=['one', 'three'], regex='e$', axis=1)
with tm.assertRaisesRegexp(TypeError, 'mutually exclusive'):
with tm.assert_raises_regex(TypeError, 'mutually exclusive'):
self.frame.filter(items=['one', 'three'], regex='e$')
with tm.assertRaisesRegexp(TypeError, 'mutually exclusive'):
with tm.assert_raises_regex(TypeError, 'mutually exclusive'):
self.frame.filter(items=['one', 'three'], like='bbi', axis=0)
with tm.assertRaisesRegexp(TypeError, 'mutually exclusive'):
with tm.assert_raises_regex(TypeError, 'mutually exclusive'):
self.frame.filter(items=['one', 'three'], like='bbi')

# objects
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_block_internals.py
Expand Up @@ -17,8 +17,7 @@

from pandas.util.testing import (assert_almost_equal,
assert_series_equal,
assert_frame_equal,
assertRaisesRegexp)
assert_frame_equal)

import pandas.util.testing as tm

Expand Down Expand Up @@ -481,7 +480,7 @@ def test_convert_objects(self):

# via astype, but errors
converted = self.mixed_frame.copy()
with assertRaisesRegexp(ValueError, 'invalid literal'):
with tm.assert_raises_regex(ValueError, 'invalid literal'):
converted['H'].astype('int32')

# mixed in a single column
Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/frame/test_combine_concat.py
Expand Up @@ -15,9 +15,7 @@
from pandas.tests.frame.common import TestData

import pandas.util.testing as tm
from pandas.util.testing import (assertRaisesRegexp,
assert_frame_equal,
assert_series_equal)
from pandas.util.testing import assert_frame_equal, assert_series_equal


class TestDataFrameConcatCommon(tm.TestCase, TestData):
Expand Down Expand Up @@ -78,11 +76,13 @@ def test_append_series_dict(self):
columns=['foo', 'bar', 'baz', 'qux'])

series = df.loc[4]
with assertRaisesRegexp(ValueError, 'Indexes have overlapping values'):
with tm.assert_raises_regex(ValueError,
'Indexes have overlapping values'):
df.append(series, verify_integrity=True)
series.name = None
with assertRaisesRegexp(TypeError, 'Can only append a Series if '
'ignore_index=True'):
with tm.assert_raises_regex(TypeError,
'Can only append a Series if '
'ignore_index=True'):
df.append(series, verify_integrity=True)

result = df.append(series[::-1], ignore_index=True)
Expand Down Expand Up @@ -270,7 +270,7 @@ def test_update_raise(self):

other = DataFrame([[2., nan],
[nan, 7]], index=[1, 3], columns=[1, 2])
with assertRaisesRegexp(ValueError, "Data overlaps"):
with tm.assert_raises_regex(ValueError, "Data overlaps"):
df.update(other, raise_conflict=True)

def test_update_from_non_df(self):
Expand Down Expand Up @@ -419,7 +419,7 @@ def test_concat_axis_parameter(self):
assert_frame_equal(concatted_1_series, expected_columns_series)

# Testing ValueError
with assertRaisesRegexp(ValueError, 'No axis named'):
with tm.assert_raises_regex(ValueError, 'No axis named'):
pd.concat([series1, series2], axis='something')

def test_concat_numerical_names(self):
Expand Down

0 comments on commit 1a93759

Please sign in to comment.