Skip to content

Commit 41780e5

Browse files
jbrockmendeljreback
authored andcommitted
STY: use pytest.raises context syntax (#24297)
1 parent 75686eb commit 41780e5

25 files changed

+123
-282
lines changed

pandas/tests/api/test_types.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ def check_deprecation(self, fold, fnew):
4242
expected = fnew('foo')
4343
assert result == expected
4444
except TypeError:
45-
pytest.raises(TypeError, lambda: fnew('foo'))
45+
with pytest.raises(TypeError):
46+
fnew('foo')
4647
except AttributeError:
47-
pytest.raises(AttributeError, lambda: fnew('foo'))
48+
with pytest.raises(AttributeError):
49+
fnew('foo')
4850

4951
def test_deprecated_from_api_types(self):
5052

pandas/tests/arrays/categorical/test_api.py

+7-21
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,17 @@ def test_reorder_categories(self):
157157
# not all "old" included in "new"
158158
cat = Categorical(["a", "b", "c", "a"], ordered=True)
159159

160-
def f():
160+
with pytest.raises(ValueError):
161161
cat.reorder_categories(["a"])
162162

163-
pytest.raises(ValueError, f)
164-
165163
# still not all "old" in "new"
166-
def f():
164+
with pytest.raises(ValueError):
167165
cat.reorder_categories(["a", "b", "d"])
168166

169-
pytest.raises(ValueError, f)
170-
171167
# all "old" included in "new", but too long
172-
def f():
168+
with pytest.raises(ValueError):
173169
cat.reorder_categories(["a", "b", "c", "d"])
174170

175-
pytest.raises(ValueError, f)
176-
177171
def test_add_categories(self):
178172
cat = Categorical(["a", "b", "c", "a"], ordered=True)
179173
old = cat.copy()
@@ -195,11 +189,9 @@ def test_add_categories(self):
195189
assert res is None
196190

197191
# new is in old categories
198-
def f():
192+
with pytest.raises(ValueError):
199193
cat.add_categories(["d"])
200194

201-
pytest.raises(ValueError, f)
202-
203195
# GH 9927
204196
cat = Categorical(list("abc"), ordered=True)
205197
expected = Categorical(
@@ -351,11 +343,9 @@ def test_remove_categories(self):
351343
assert res is None
352344

353345
# removal is not in categories
354-
def f():
346+
with pytest.raises(ValueError):
355347
cat.remove_categories(["c"])
356348

357-
pytest.raises(ValueError, f)
358-
359349
def test_remove_unused_categories(self):
360350
c = Categorical(["a", "b", "c", "d", "a"],
361351
categories=["a", "b", "c", "d", "e"])
@@ -461,20 +451,16 @@ def test_codes_immutable(self):
461451
tm.assert_numpy_array_equal(c.codes, exp)
462452

463453
# Assignments to codes should raise
464-
def f():
454+
with pytest.raises(ValueError):
465455
c.codes = np.array([0, 1, 2, 0, 1], dtype='int8')
466456

467-
pytest.raises(ValueError, f)
468-
469457
# changes in the codes array should raise
470458
# np 1.6.1 raises RuntimeError rather than ValueError
471459
codes = c.codes
472460

473-
def f():
461+
with pytest.raises(ValueError):
474462
codes[4] = 1
475463

476-
pytest.raises(ValueError, f)
477-
478464
# But even after getting the codes, the original array should still be
479465
# writeable!
480466
c[4] = "a"

pandas/tests/arrays/categorical/test_constructors.py

+9-23
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def test_constructor_unsortable(self):
7777
assert not factor.ordered
7878

7979
# this however will raise as cannot be sorted
80-
pytest.raises(
81-
TypeError, lambda: Categorical(arr, ordered=True))
80+
with pytest.raises(TypeError):
81+
Categorical(arr, ordered=True)
8282

8383
def test_constructor_interval(self):
8484
result = Categorical([Interval(1, 2), Interval(2, 3), Interval(3, 6)],
@@ -99,16 +99,12 @@ def test_constructor(self):
9999
tm.assert_numpy_array_equal(c2.__array__(), exp_arr)
100100

101101
# categories must be unique
102-
def f():
102+
with pytest.raises(ValueError):
103103
Categorical([1, 2], [1, 2, 2])
104104

105-
pytest.raises(ValueError, f)
106-
107-
def f():
105+
with pytest.raises(ValueError):
108106
Categorical(["a", "b"], ["a", "b", "b"])
109107

110-
pytest.raises(ValueError, f)
111-
112108
# The default should be unordered
113109
c1 = Categorical(["a", "b", "c", "a"])
114110
assert not c1.ordered
@@ -421,35 +417,25 @@ def test_constructor_with_categorical_categories(self):
421417
def test_from_codes(self):
422418

423419
# too few categories
424-
def f():
420+
with pytest.raises(ValueError):
425421
Categorical.from_codes([1, 2], [1, 2])
426422

427-
pytest.raises(ValueError, f)
428-
429423
# no int codes
430-
def f():
424+
with pytest.raises(ValueError):
431425
Categorical.from_codes(["a"], [1, 2])
432426

433-
pytest.raises(ValueError, f)
434-
435427
# no unique categories
436-
def f():
428+
with pytest.raises(ValueError):
437429
Categorical.from_codes([0, 1, 2], ["a", "a", "b"])
438430

439-
pytest.raises(ValueError, f)
440-
441431
# NaN categories included
442-
def f():
432+
with pytest.raises(ValueError):
443433
Categorical.from_codes([0, 1, 2], ["a", "b", np.nan])
444434

445-
pytest.raises(ValueError, f)
446-
447435
# too negative
448-
def f():
436+
with pytest.raises(ValueError):
449437
Categorical.from_codes([-2, 1, 2], ["a", "b", "c"])
450438

451-
pytest.raises(ValueError, f)
452-
453439
exp = Categorical(["a", "b", "c"], ordered=False)
454440
res = Categorical.from_codes([0, 1, 2], ["a", "b", "c"])
455441
tm.assert_categorical_equal(exp, res)

pandas/tests/arrays/categorical/test_indexing.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,13 @@ def test_categories_assigments(self):
133133
tm.assert_index_equal(s.categories, Index([1, 2, 3]))
134134

135135
# lengthen
136-
def f():
136+
with pytest.raises(ValueError):
137137
s.categories = [1, 2, 3, 4]
138138

139-
pytest.raises(ValueError, f)
140-
141139
# shorten
142-
def f():
140+
with pytest.raises(ValueError):
143141
s.categories = [1, 2]
144142

145-
pytest.raises(ValueError, f)
146-
147143
# Combinations of sorted/unique:
148144
@pytest.mark.parametrize("idx_values", [[1, 2, 3, 4], [1, 3, 2, 4],
149145
[1, 3, 3, 4], [1, 2, 2, 4]])

pandas/tests/arrays/categorical/test_operators.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,22 @@ def test_comparisons(self):
7676
tm.assert_numpy_array_equal(res, exp)
7777

7878
# Only categories with same categories can be compared
79-
def f():
79+
with pytest.raises(TypeError):
8080
cat > cat_rev
8181

82-
pytest.raises(TypeError, f)
83-
8482
cat_rev_base2 = Categorical(
8583
["b", "b", "b"], categories=["c", "b", "a", "d"])
8684

87-
def f():
85+
with pytest.raises(TypeError):
8886
cat_rev > cat_rev_base2
8987

90-
pytest.raises(TypeError, f)
91-
9288
# Only categories with same ordering information can be compared
9389
cat_unorderd = cat.set_ordered(False)
9490
assert not (cat > cat).any()
9591

96-
def f():
92+
with pytest.raises(TypeError):
9793
cat > cat_unorderd
9894

99-
pytest.raises(TypeError, f)
100-
10195
# comparison (in both directions) with Series will raise
10296
s = Series(["b", "b", "b"])
10397
pytest.raises(TypeError, lambda: cat > s)
@@ -194,11 +188,9 @@ def test_comparisons(self, data, reverse, base):
194188
tm.assert_numpy_array_equal(res_rev.values, exp_rev2)
195189

196190
# Only categories with same categories can be compared
197-
def f():
191+
with pytest.raises(TypeError):
198192
cat > cat_rev
199193

200-
pytest.raises(TypeError, f)
201-
202194
# categorical cannot be compared to Series or numpy array, and also
203195
# not the other way around
204196
pytest.raises(TypeError, lambda: cat > s)
@@ -284,14 +276,16 @@ def test_numeric_like_ops(self):
284276

285277
# numpy ops
286278
s = Series(Categorical([1, 2, 3, 4]))
287-
pytest.raises(TypeError, lambda: np.sum(s))
279+
with pytest.raises(TypeError):
280+
np.sum(s)
288281

289282
# numeric ops on a Series
290283
for op in ['__add__', '__sub__', '__mul__', '__truediv__']:
291284
pytest.raises(TypeError, lambda: getattr(s, op)(2))
292285

293286
# invalid ufunc
294-
pytest.raises(TypeError, lambda: np.log(s))
287+
with pytest.raises(TypeError):
288+
np.log(s)
295289

296290
def test_contains(self):
297291
# GH21508

pandas/tests/frame/test_apply.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def test_apply(self, float_frame):
5555
# invalid axis
5656
df = DataFrame(
5757
[[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=['a', 'a', 'c'])
58-
pytest.raises(ValueError, df.apply, lambda x: x, 2)
58+
with pytest.raises(ValueError):
59+
df.apply(lambda x: x, 2)
5960

6061
# GH 9573
6162
df = DataFrame({'c0': ['A', 'A', 'B', 'B'],
@@ -874,19 +875,16 @@ def test_agg_transform(self, axis, float_frame):
874875

875876
def test_transform_and_agg_err(self, axis, float_frame):
876877
# cannot both transform and agg
877-
def f():
878+
with pytest.raises(ValueError):
878879
float_frame.transform(['max', 'min'], axis=axis)
879-
pytest.raises(ValueError, f)
880880

881-
def f():
881+
with pytest.raises(ValueError):
882882
with np.errstate(all='ignore'):
883883
float_frame.agg(['max', 'sqrt'], axis=axis)
884-
pytest.raises(ValueError, f)
885884

886-
def f():
885+
with pytest.raises(ValueError):
887886
with np.errstate(all='ignore'):
888887
float_frame.transform(['max', 'sqrt'], axis=axis)
889-
pytest.raises(ValueError, f)
890888

891889
df = pd.DataFrame({'A': range(5), 'B': 5})
892890

0 commit comments

Comments
 (0)