Skip to content

Commit d528a10

Browse files
gfyoungjreback
authored andcommitted
MAINT: Remove assertRaises from testing (#16089)
1 parent 70ae817 commit d528a10

File tree

174 files changed

+2633
-2498
lines changed

Some content is hidden

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

174 files changed

+2633
-2498
lines changed

pandas/tests/api/test_types.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
import pytest
4+
35
from warnings import catch_warnings
46
import numpy as np
57

@@ -41,13 +43,11 @@ def check_deprecation(self, fold, fnew):
4143
try:
4244
result = fold('foo')
4345
expected = fnew('foo')
44-
self.assertEqual(result, expected)
46+
assert result == expected
4547
except TypeError:
46-
self.assertRaises(TypeError,
47-
lambda: fnew('foo'))
48+
pytest.raises(TypeError, lambda: fnew('foo'))
4849
except AttributeError:
49-
self.assertRaises(AttributeError,
50-
lambda: fnew('foo'))
50+
pytest.raises(AttributeError, lambda: fnew('foo'))
5151

5252
def test_deprecation_core_common(self):
5353

@@ -83,7 +83,7 @@ def test_removed_from_core_common(self):
8383

8484
for t in ['is_null_datelike_scalar',
8585
'ensure_float']:
86-
self.assertRaises(AttributeError, lambda: getattr(com, t))
86+
pytest.raises(AttributeError, lambda: getattr(com, t))
8787

8888

8989
def test_moved_infer_dtype():

pandas/tests/computation/test_compat.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from distutils.version import LooseVersion
33

44
import pandas as pd
5-
from pandas.util import testing as tm
65

76
from pandas.core.computation.engines import _engines
87
import pandas.core.computation.expr as expr
@@ -39,7 +38,7 @@ def testit():
3938
pytest.skip("no numexpr")
4039
else:
4140
if ne.__version__ < LooseVersion(_MIN_NUMEXPR_VERSION):
42-
with tm.assertRaises(ImportError):
41+
with pytest.raises(ImportError):
4342
testit()
4443
else:
4544
testit()

pandas/tests/computation/test_eval.py

+73-74
Large diffs are not rendered by default.

pandas/tests/dtypes/test_cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def test_numpy_dtypes(self):
293293
for src, common in testcases:
294294
self.assertEqual(find_common_type(src), common)
295295

296-
with tm.assertRaises(ValueError):
296+
with pytest.raises(ValueError):
297297
# empty
298298
find_common_type([])
299299

pandas/tests/dtypes/test_dtypes.py

+25-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import pytest
3+
24
from itertools import product
35

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

2729
def test_equality_invalid(self):
28-
self.assertRaises(self.dtype == 'foo')
29-
self.assertFalse(is_dtype_equal(self.dtype, np.int64))
30+
assert not self.dtype == 'foo'
31+
assert not is_dtype_equal(self.dtype, np.int64)
3032

3133
def test_numpy_informed(self):
34+
pytest.raises(TypeError, np.dtype, self.dtype)
3235

33-
# np.dtype doesn't know about our new dtype
34-
def f():
35-
np.dtype(self.dtype)
36-
37-
self.assertRaises(TypeError, f)
38-
39-
self.assertNotEqual(self.dtype, np.str_)
40-
self.assertNotEqual(np.str_, self.dtype)
36+
assert not self.dtype == np.str_
37+
assert not np.str_ == self.dtype
4138

4239
def test_pickle(self):
4340
result = tm.round_trip_pickle(self.dtype)
44-
self.assertEqual(result, self.dtype)
41+
assert result == self.dtype
4542

4643

4744
class TestCategoricalDtype(Base, tm.TestCase):
@@ -67,7 +64,7 @@ def test_equality(self):
6764
def test_construction_from_string(self):
6865
result = CategoricalDtype.construct_from_string('category')
6966
self.assertTrue(is_dtype_equal(self.dtype, result))
70-
self.assertRaises(
67+
pytest.raises(
7168
TypeError, lambda: CategoricalDtype.construct_from_string('foo'))
7269

7370
def test_is_dtype(self):
@@ -116,8 +113,8 @@ def test_hash_vs_equality(self):
116113
self.assertTrue(hash(dtype) == hash(dtype3))
117114

118115
def test_construction(self):
119-
self.assertRaises(ValueError,
120-
lambda: DatetimeTZDtype('ms', 'US/Eastern'))
116+
pytest.raises(ValueError,
117+
lambda: DatetimeTZDtype('ms', 'US/Eastern'))
121118

122119
def test_subclass(self):
123120
a = DatetimeTZDtype('datetime64[ns, US/Eastern]')
@@ -148,8 +145,8 @@ def test_construction_from_string(self):
148145
result = DatetimeTZDtype.construct_from_string(
149146
'datetime64[ns, US/Eastern]')
150147
self.assertTrue(is_dtype_equal(self.dtype, result))
151-
self.assertRaises(TypeError,
152-
lambda: DatetimeTZDtype.construct_from_string('foo'))
148+
pytest.raises(TypeError,
149+
lambda: DatetimeTZDtype.construct_from_string('foo'))
153150

154151
def test_is_dtype(self):
155152
self.assertFalse(DatetimeTZDtype.is_dtype(None))
@@ -215,7 +212,7 @@ def test_parser(self):
215212

216213
def test_empty(self):
217214
dt = DatetimeTZDtype()
218-
with tm.assertRaises(AttributeError):
215+
with pytest.raises(AttributeError):
219216
str(dt)
220217

221218

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

227224
def test_construction(self):
228-
with tm.assertRaises(ValueError):
225+
with pytest.raises(ValueError):
229226
PeriodDtype('xx')
230227

231228
for s in ['period[D]', 'Period[D]', 'D']:
@@ -284,16 +281,16 @@ def test_construction_from_string(self):
284281
self.assertTrue(is_dtype_equal(self.dtype, result))
285282
result = PeriodDtype.construct_from_string('period[D]')
286283
self.assertTrue(is_dtype_equal(self.dtype, result))
287-
with tm.assertRaises(TypeError):
284+
with pytest.raises(TypeError):
288285
PeriodDtype.construct_from_string('foo')
289-
with tm.assertRaises(TypeError):
286+
with pytest.raises(TypeError):
290287
PeriodDtype.construct_from_string('period[foo]')
291-
with tm.assertRaises(TypeError):
288+
with pytest.raises(TypeError):
292289
PeriodDtype.construct_from_string('foo[D]')
293290

294-
with tm.assertRaises(TypeError):
291+
with pytest.raises(TypeError):
295292
PeriodDtype.construct_from_string('datetime64[ns]')
296-
with tm.assertRaises(TypeError):
293+
with pytest.raises(TypeError):
297294
PeriodDtype.construct_from_string('datetime64[ns, US/Eastern]')
298295

299296
def test_is_dtype(self):
@@ -348,7 +345,7 @@ def test_basic(self):
348345

349346
def test_empty(self):
350347
dt = PeriodDtype()
351-
with tm.assertRaises(AttributeError):
348+
with pytest.raises(AttributeError):
352349
str(dt)
353350

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

365362
def test_construction(self):
366-
with tm.assertRaises(ValueError):
363+
with pytest.raises(ValueError):
367364
IntervalDtype('xx')
368365

369366
for s in ['interval[int64]', 'Interval[int64]', 'int64']:
@@ -419,11 +416,11 @@ def test_construction_from_string(self):
419416
self.assertTrue(is_dtype_equal(self.dtype, result))
420417
result = IntervalDtype.construct_from_string('interval[int64]')
421418
self.assertTrue(is_dtype_equal(self.dtype, result))
422-
with tm.assertRaises(TypeError):
419+
with pytest.raises(TypeError):
423420
IntervalDtype.construct_from_string('foo')
424-
with tm.assertRaises(TypeError):
421+
with pytest.raises(TypeError):
425422
IntervalDtype.construct_from_string('interval[foo]')
426-
with tm.assertRaises(TypeError):
423+
with pytest.raises(TypeError):
427424
IntervalDtype.construct_from_string('foo[int64]')
428425

429426
def test_equality(self):

pandas/tests/frame/test_alter_axes.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import print_function
44

5+
import pytest
6+
57
from datetime import datetime, timedelta
68

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

398400
# have to pass something
399-
self.assertRaises(TypeError, self.frame.rename)
401+
pytest.raises(TypeError, self.frame.rename)
400402

401403
# partial columns
402404
renamed = self.frame.rename(columns={'C': 'foo', 'D': 'bar'})

pandas/tests/frame/test_analytics.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,10 @@ def test_numeric_only_flag(self):
586586
tm.assert_series_equal(expected, result)
587587

588588
# df1 has all numbers, df2 has a letter inside
589-
self.assertRaises(TypeError, lambda: getattr(df1, meth)
590-
(axis=1, numeric_only=False))
591-
self.assertRaises(TypeError, lambda: getattr(df2, meth)
592-
(axis=1, numeric_only=False))
589+
pytest.raises(TypeError, lambda: getattr(df1, meth)(
590+
axis=1, numeric_only=False))
591+
pytest.raises(TypeError, lambda: getattr(df2, meth)(
592+
axis=1, numeric_only=False))
593593

594594
def test_cumsum(self):
595595
self.tsframe.loc[5:10, 0] = nan
@@ -998,7 +998,7 @@ def test_idxmin(self):
998998
skipna=skipna)
999999
tm.assert_series_equal(result, expected)
10001000

1001-
self.assertRaises(ValueError, frame.idxmin, axis=2)
1001+
pytest.raises(ValueError, frame.idxmin, axis=2)
10021002

10031003
def test_idxmax(self):
10041004
frame = self.frame
@@ -1012,7 +1012,7 @@ def test_idxmax(self):
10121012
skipna=skipna)
10131013
tm.assert_series_equal(result, expected)
10141014

1015-
self.assertRaises(ValueError, frame.idxmax, axis=2)
1015+
pytest.raises(ValueError, frame.idxmax, axis=2)
10161016

10171017
# ----------------------------------------------------------------------
10181018
# Logical reductions
@@ -1087,7 +1087,7 @@ def wrapper(x):
10871087
# assert_series_equal(result, comp)
10881088

10891089
# bad axis
1090-
self.assertRaises(ValueError, f, axis=2)
1090+
pytest.raises(ValueError, f, axis=2)
10911091

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

1169-
with tm.assertRaises(TypeError):
1169+
with pytest.raises(TypeError):
11701170
df.isin('aaa')
11711171

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

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

12011201
# cols and index:
12021202
df2.columns = ['B', 'B']
1203-
with tm.assertRaises(ValueError):
1203+
with pytest.raises(ValueError):
12041204
df1.isin(df2)
12051205

12061206
def test_isin_dupe_self(self):
@@ -1629,7 +1629,7 @@ def test_round(self):
16291629

16301630
# Round with a list
16311631
round_list = [1, 2]
1632-
with self.assertRaises(TypeError):
1632+
with pytest.raises(TypeError):
16331633
df.round(round_list)
16341634

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

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

16581658
# String input
16591659
non_int_round_dict = {'col1': 1, 'col2': 'foo'}
1660-
with self.assertRaises(TypeError):
1660+
with pytest.raises(TypeError):
16611661
df.round(non_int_round_dict)
16621662

16631663
non_int_round_Series = Series(non_int_round_dict)
1664-
with self.assertRaises(TypeError):
1664+
with pytest.raises(TypeError):
16651665
df.round(non_int_round_Series)
16661666

16671667
# List input
16681668
non_int_round_dict = {'col1': 1, 'col2': [1, 2]}
1669-
with self.assertRaises(TypeError):
1669+
with pytest.raises(TypeError):
16701670
df.round(non_int_round_dict)
16711671

16721672
non_int_round_Series = Series(non_int_round_dict)
1673-
with self.assertRaises(TypeError):
1673+
with pytest.raises(TypeError):
16741674
df.round(non_int_round_Series)
16751675

16761676
# Non integer Series inputs
16771677
non_int_round_Series = Series(non_int_round_dict)
1678-
with self.assertRaises(TypeError):
1678+
with pytest.raises(TypeError):
16791679
df.round(non_int_round_Series)
16801680

16811681
non_int_round_Series = Series(non_int_round_dict)
1682-
with self.assertRaises(TypeError):
1682+
with pytest.raises(TypeError):
16831683
df.round(non_int_round_Series)
16841684

16851685
# Negative numbers
@@ -1700,10 +1700,10 @@ def test_round(self):
17001700

17011701
if sys.version < LooseVersion('2.7'):
17021702
# Rounding with decimal is a ValueError in Python < 2.7
1703-
with self.assertRaises(ValueError):
1703+
with pytest.raises(ValueError):
17041704
df.round(nan_round_Series)
17051705
else:
1706-
with self.assertRaises(TypeError):
1706+
with pytest.raises(TypeError):
17071707
df.round(nan_round_Series)
17081708

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

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

17661766
def test_built_in_round(self):
17671767
if not compat.PY3:

0 commit comments

Comments
 (0)