Skip to content

Commit 2733901

Browse files
committed
CLN: Remove remaining non-context pytest.raises
1 parent 90681af commit 2733901

File tree

8 files changed

+281
-182
lines changed

8 files changed

+281
-182
lines changed

Diff for: pandas/tests/arrays/sparse/test_libsparse.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
157157
elen = [3, 2, 3, 2]
158158
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
159159

160-
def test_intindex_make_union(self):
160+
def test_int_index_make_union(self):
161161
a = IntIndex(5, np.array([0, 3, 4], dtype=np.int32))
162162
b = IntIndex(5, np.array([0, 2], dtype=np.int32))
163163
res = a.make_union(b)
@@ -184,7 +184,9 @@ def test_intindex_make_union(self):
184184

185185
a = IntIndex(5, np.array([0, 1], dtype=np.int32))
186186
b = IntIndex(4, np.array([0, 1], dtype=np.int32))
187-
with pytest.raises(ValueError):
187+
188+
msg = "Indices must reference same underlying length"
189+
with pytest.raises(ValueError, match=msg):
188190
a.make_union(b)
189191

190192

@@ -197,7 +199,9 @@ def _check_correct(a, b, expected):
197199
assert (result.equals(expected))
198200

199201
def _check_length_exc(a, longer):
200-
pytest.raises(Exception, a.intersect, longer)
202+
msg = "Indices must reference same underlying length"
203+
with pytest.raises(Exception, match=msg):
204+
a.intersect(longer)
201205

202206
def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
203207
xindex = BlockIndex(TEST_LENGTH, xloc, xlen)

Diff for: pandas/tests/computation/test_eval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ def check_pow(self, lhs, arith1, rhs):
339339

340340
if (is_scalar(lhs) and is_scalar(rhs) and
341341
_is_py3_complex_incompat(result, expected)):
342-
pytest.raises(AssertionError, tm.assert_numpy_array_equal,
343-
result, expected)
342+
with pytest.raises(AssertionError):
343+
tm.assert_numpy_array_equal(result, expected)
344344
else:
345345
tm.assert_almost_equal(result, expected)
346346

Diff for: pandas/tests/frame/test_nonunique_indexes.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,12 @@ def test_columns_with_dups(self):
416416
[[1, 2, 1., 2., 3., 'foo', 'bar']], columns=list('ABCDEFG'))
417417
assert_frame_equal(df, expected)
418418

419-
# this is an error because we cannot disambiguate the dup columns
420-
pytest.raises(Exception, lambda x: DataFrame(
421-
[[1, 2, 'foo', 'bar']], columns=['a', 'a', 'a', 'a']))
419+
df = DataFrame([[1, 2, 'foo', 'bar']], columns=['a', 'a', 'a', 'a'])
420+
df.columns = ['a', 'a.1', 'a.2', 'a.3']
421+
str(df)
422+
expected = DataFrame([[1, 2, 'foo', 'bar']],
423+
columns=['a', 'a.1', 'a.2', 'a.3'])
424+
assert_frame_equal(df, expected)
422425

423426
# dups across blocks
424427
df_float = DataFrame(np.random.randn(10, 3), dtype='float64')

Diff for: pandas/tests/indexes/interval/test_interval_new.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def test_get_loc_scalar(self, closed, scalar):
4949
if scalar in correct[closed].keys():
5050
assert idx.get_loc(scalar) == correct[closed][scalar]
5151
else:
52-
pytest.raises(KeyError, idx.get_loc, scalar)
52+
with pytest.raises(KeyError, match=str(scalar)):
53+
idx.get_loc(scalar)
5354

5455
def test_slice_locs_with_interval(self):
5556

@@ -89,13 +90,19 @@ def test_slice_locs_with_interval(self):
8990
# unsorted duplicates
9091
index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)])
9192

92-
pytest.raises(KeyError, index.slice_locs(
93-
start=Interval(0, 2), end=Interval(2, 4)))
94-
pytest.raises(KeyError, index.slice_locs(start=Interval(0, 2)))
93+
with pytest.raises(KeyError):
94+
index.slice_locs(start=Interval(0, 2), end=Interval(2, 4))
95+
96+
with pytest.raises(KeyError):
97+
index.slice_locs(start=Interval(0, 2))
98+
9599
assert index.slice_locs(end=Interval(2, 4)) == (0, 2)
96-
pytest.raises(KeyError, index.slice_locs(end=Interval(0, 2)))
97-
pytest.raises(KeyError, index.slice_locs(
98-
start=Interval(2, 4), end=Interval(0, 2)))
100+
101+
with pytest.raises(KeyError):
102+
index.slice_locs(end=Interval(0, 2))
103+
104+
with pytest.raises(KeyError):
105+
index.slice_locs(start=Interval(2, 4), end=Interval(0, 2))
99106

100107
# another unsorted duplicates
101108
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)])

Diff for: pandas/tests/indexing/multiindex/test_partial.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ def test_partial_ix_missing(
147147
# assert (self.ymd.loc[2000]['A'] == 0).all()
148148

149149
# Pretty sure the second (and maybe even the first) is already wrong.
150-
pytest.raises(Exception, ymd.loc.__getitem__, (2000, 6))
151-
pytest.raises(Exception, ymd.loc.__getitem__, (2000, 6), 0)
150+
with pytest.raises(Exception):
151+
ymd.loc[(2000, 6)]
152+
with pytest.raises(Exception):
153+
ymd.loc[(2000, 6), 0]
152154

153155
# ---------------------------------------------------------------------
154156

Diff for: pandas/tests/indexing/test_floats.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def test_scalar_non_numeric(self):
137137
# for idxr in [lambda x: x.ix,
138138
# lambda x: x]:
139139
# s2 = s.copy()
140-
# def f():
140+
#
141+
# with pytest.raises(TypeError):
141142
# idxr(s2)[3.0] = 0
142-
# pytest.raises(TypeError, f)
143143
pass
144144

145145
else:

0 commit comments

Comments
 (0)