Skip to content

Commit

Permalink
MAINT: Remove assertRaises from testing (pandas-dev#16089)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyoung authored and pcluo committed May 22, 2017
1 parent de4c6c5 commit 5aefcb6
Show file tree
Hide file tree
Showing 174 changed files with 2,633 additions and 2,498 deletions.
12 changes: 6 additions & 6 deletions pandas/tests/api/test_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-

import pytest

from warnings import catch_warnings
import numpy as np

Expand Down Expand Up @@ -41,13 +43,11 @@ def check_deprecation(self, fold, fnew):
try:
result = fold('foo')
expected = fnew('foo')
self.assertEqual(result, expected)
assert result == expected
except TypeError:
self.assertRaises(TypeError,
lambda: fnew('foo'))
pytest.raises(TypeError, lambda: fnew('foo'))
except AttributeError:
self.assertRaises(AttributeError,
lambda: fnew('foo'))
pytest.raises(AttributeError, lambda: fnew('foo'))

def test_deprecation_core_common(self):

Expand Down Expand Up @@ -83,7 +83,7 @@ def test_removed_from_core_common(self):

for t in ['is_null_datelike_scalar',
'ensure_float']:
self.assertRaises(AttributeError, lambda: getattr(com, t))
pytest.raises(AttributeError, lambda: getattr(com, t))


def test_moved_infer_dtype():
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/computation/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from distutils.version import LooseVersion

import pandas as pd
from pandas.util import testing as tm

from pandas.core.computation.engines import _engines
import pandas.core.computation.expr as expr
Expand Down Expand Up @@ -39,7 +38,7 @@ def testit():
pytest.skip("no numexpr")
else:
if ne.__version__ < LooseVersion(_MIN_NUMEXPR_VERSION):
with tm.assertRaises(ImportError):
with pytest.raises(ImportError):
testit()
else:
testit()
Expand Down
147 changes: 73 additions & 74 deletions pandas/tests/computation/test_eval.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pandas/tests/dtypes/test_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def test_numpy_dtypes(self):
for src, common in testcases:
self.assertEqual(find_common_type(src), common)

with tm.assertRaises(ValueError):
with pytest.raises(ValueError):
# empty
find_common_type([])

Expand Down
53 changes: 25 additions & 28 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import pytest

from itertools import product

import numpy as np
Expand All @@ -25,23 +27,18 @@ def test_hash(self):
hash(self.dtype)

def test_equality_invalid(self):
self.assertRaises(self.dtype == 'foo')
self.assertFalse(is_dtype_equal(self.dtype, np.int64))
assert not self.dtype == 'foo'
assert not is_dtype_equal(self.dtype, np.int64)

def test_numpy_informed(self):
pytest.raises(TypeError, np.dtype, self.dtype)

# np.dtype doesn't know about our new dtype
def f():
np.dtype(self.dtype)

self.assertRaises(TypeError, f)

self.assertNotEqual(self.dtype, np.str_)
self.assertNotEqual(np.str_, self.dtype)
assert not self.dtype == np.str_
assert not np.str_ == self.dtype

def test_pickle(self):
result = tm.round_trip_pickle(self.dtype)
self.assertEqual(result, self.dtype)
assert result == self.dtype


class TestCategoricalDtype(Base, tm.TestCase):
Expand All @@ -67,7 +64,7 @@ def test_equality(self):
def test_construction_from_string(self):
result = CategoricalDtype.construct_from_string('category')
self.assertTrue(is_dtype_equal(self.dtype, result))
self.assertRaises(
pytest.raises(
TypeError, lambda: CategoricalDtype.construct_from_string('foo'))

def test_is_dtype(self):
Expand Down Expand Up @@ -116,8 +113,8 @@ def test_hash_vs_equality(self):
self.assertTrue(hash(dtype) == hash(dtype3))

def test_construction(self):
self.assertRaises(ValueError,
lambda: DatetimeTZDtype('ms', 'US/Eastern'))
pytest.raises(ValueError,
lambda: DatetimeTZDtype('ms', 'US/Eastern'))

def test_subclass(self):
a = DatetimeTZDtype('datetime64[ns, US/Eastern]')
Expand Down Expand Up @@ -148,8 +145,8 @@ def test_construction_from_string(self):
result = DatetimeTZDtype.construct_from_string(
'datetime64[ns, US/Eastern]')
self.assertTrue(is_dtype_equal(self.dtype, result))
self.assertRaises(TypeError,
lambda: DatetimeTZDtype.construct_from_string('foo'))
pytest.raises(TypeError,
lambda: DatetimeTZDtype.construct_from_string('foo'))

def test_is_dtype(self):
self.assertFalse(DatetimeTZDtype.is_dtype(None))
Expand Down Expand Up @@ -215,7 +212,7 @@ def test_parser(self):

def test_empty(self):
dt = DatetimeTZDtype()
with tm.assertRaises(AttributeError):
with pytest.raises(AttributeError):
str(dt)


Expand All @@ -225,7 +222,7 @@ def setUp(self):
self.dtype = PeriodDtype('D')

def test_construction(self):
with tm.assertRaises(ValueError):
with pytest.raises(ValueError):
PeriodDtype('xx')

for s in ['period[D]', 'Period[D]', 'D']:
Expand Down Expand Up @@ -284,16 +281,16 @@ def test_construction_from_string(self):
self.assertTrue(is_dtype_equal(self.dtype, result))
result = PeriodDtype.construct_from_string('period[D]')
self.assertTrue(is_dtype_equal(self.dtype, result))
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
PeriodDtype.construct_from_string('foo')
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
PeriodDtype.construct_from_string('period[foo]')
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
PeriodDtype.construct_from_string('foo[D]')

with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
PeriodDtype.construct_from_string('datetime64[ns]')
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
PeriodDtype.construct_from_string('datetime64[ns, US/Eastern]')

def test_is_dtype(self):
Expand Down Expand Up @@ -348,7 +345,7 @@ def test_basic(self):

def test_empty(self):
dt = PeriodDtype()
with tm.assertRaises(AttributeError):
with pytest.raises(AttributeError):
str(dt)

def test_not_string(self):
Expand All @@ -363,7 +360,7 @@ def setUp(self):
self.dtype = IntervalDtype('int64')

def test_construction(self):
with tm.assertRaises(ValueError):
with pytest.raises(ValueError):
IntervalDtype('xx')

for s in ['interval[int64]', 'Interval[int64]', 'int64']:
Expand Down Expand Up @@ -419,11 +416,11 @@ def test_construction_from_string(self):
self.assertTrue(is_dtype_equal(self.dtype, result))
result = IntervalDtype.construct_from_string('interval[int64]')
self.assertTrue(is_dtype_equal(self.dtype, result))
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
IntervalDtype.construct_from_string('foo')
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
IntervalDtype.construct_from_string('interval[foo]')
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
IntervalDtype.construct_from_string('foo[int64]')

def test_equality(self):
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import print_function

import pytest

from datetime import datetime, timedelta

import numpy as np
Expand Down Expand Up @@ -396,7 +398,7 @@ def test_rename(self):
tm.assert_index_equal(renamed.index, pd.Index(['BAR', 'FOO']))

# have to pass something
self.assertRaises(TypeError, self.frame.rename)
pytest.raises(TypeError, self.frame.rename)

# partial columns
renamed = self.frame.rename(columns={'C': 'foo', 'D': 'bar'})
Expand Down
46 changes: 23 additions & 23 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,10 @@ def test_numeric_only_flag(self):
tm.assert_series_equal(expected, result)

# df1 has all numbers, df2 has a letter inside
self.assertRaises(TypeError, lambda: getattr(df1, meth)
(axis=1, numeric_only=False))
self.assertRaises(TypeError, lambda: getattr(df2, meth)
(axis=1, numeric_only=False))
pytest.raises(TypeError, lambda: getattr(df1, meth)(
axis=1, numeric_only=False))
pytest.raises(TypeError, lambda: getattr(df2, meth)(
axis=1, numeric_only=False))

def test_cumsum(self):
self.tsframe.loc[5:10, 0] = nan
Expand Down Expand Up @@ -998,7 +998,7 @@ def test_idxmin(self):
skipna=skipna)
tm.assert_series_equal(result, expected)

self.assertRaises(ValueError, frame.idxmin, axis=2)
pytest.raises(ValueError, frame.idxmin, axis=2)

def test_idxmax(self):
frame = self.frame
Expand All @@ -1012,7 +1012,7 @@ def test_idxmax(self):
skipna=skipna)
tm.assert_series_equal(result, expected)

self.assertRaises(ValueError, frame.idxmax, axis=2)
pytest.raises(ValueError, frame.idxmax, axis=2)

# ----------------------------------------------------------------------
# Logical reductions
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def wrapper(x):
# assert_series_equal(result, comp)

# bad axis
self.assertRaises(ValueError, f, axis=2)
pytest.raises(ValueError, f, axis=2)

# make sure works on mixed-type frame
mixed = self.mixed_frame
Expand Down Expand Up @@ -1163,10 +1163,10 @@ def test_isin_with_string_scalar(self):
df = DataFrame({'vals': [1, 2, 3, 4], 'ids': ['a', 'b', 'f', 'n'],
'ids2': ['a', 'n', 'c', 'n']},
index=['foo', 'bar', 'baz', 'qux'])
with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
df.isin('a')

with tm.assertRaises(TypeError):
with pytest.raises(TypeError):
df.isin('aaa')

def test_isin_df(self):
Expand All @@ -1189,18 +1189,18 @@ def test_isin_df_dupe_values(self):
# just cols duped
df2 = DataFrame([[0, 2], [12, 4], [2, np.nan], [4, 5]],
columns=['B', 'B'])
with tm.assertRaises(ValueError):
with pytest.raises(ValueError):
df1.isin(df2)

# just index duped
df2 = DataFrame([[0, 2], [12, 4], [2, np.nan], [4, 5]],
columns=['A', 'B'], index=[0, 0, 1, 1])
with tm.assertRaises(ValueError):
with pytest.raises(ValueError):
df1.isin(df2)

# cols and index:
df2.columns = ['B', 'B']
with tm.assertRaises(ValueError):
with pytest.raises(ValueError):
df1.isin(df2)

def test_isin_dupe_self(self):
Expand Down Expand Up @@ -1629,7 +1629,7 @@ def test_round(self):

# Round with a list
round_list = [1, 2]
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(round_list)

# Round with a dictionary
Expand All @@ -1652,34 +1652,34 @@ def test_round(self):

# float input to `decimals`
non_int_round_dict = {'col1': 1, 'col2': 0.5}
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_dict)

# String input
non_int_round_dict = {'col1': 1, 'col2': 'foo'}
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_dict)

non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_Series)

# List input
non_int_round_dict = {'col1': 1, 'col2': [1, 2]}
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_dict)

non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_Series)

# Non integer Series inputs
non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_Series)

non_int_round_Series = Series(non_int_round_dict)
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(non_int_round_Series)

# Negative numbers
Expand All @@ -1700,10 +1700,10 @@ def test_round(self):

if sys.version < LooseVersion('2.7'):
# Rounding with decimal is a ValueError in Python < 2.7
with self.assertRaises(ValueError):
with pytest.raises(ValueError):
df.round(nan_round_Series)
else:
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
df.round(nan_round_Series)

# Make sure this doesn't break existing Series.round
Expand Down Expand Up @@ -1761,7 +1761,7 @@ def test_round_issue(self):
tm.assert_index_equal(rounded.index, dfs.index)

decimals = pd.Series([1, 0, 2], index=['A', 'B', 'A'])
self.assertRaises(ValueError, df.round, decimals)
pytest.raises(ValueError, df.round, decimals)

def test_built_in_round(self):
if not compat.PY3:
Expand Down
Loading

0 comments on commit 5aefcb6

Please sign in to comment.