Skip to content

Commit 7b9ebb0

Browse files
shubchatjorisvandenbossche
authored andcommitted
TST:Disallow bare pytest raises issue 30999 (#31138)
1 parent 08af93a commit 7b9ebb0

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

pandas/tests/base/test_constructors.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ def test_invalid_delegation(self):
5353

5454
delegate = self.Delegate(self.Delegator())
5555

56-
with pytest.raises(TypeError):
56+
msg = "You cannot access the property foo"
57+
with pytest.raises(TypeError, match=msg):
5758
delegate.foo
5859

59-
with pytest.raises(TypeError):
60+
msg = "The property foo cannot be set"
61+
with pytest.raises(TypeError, match=msg):
6062
delegate.foo = 5
6163

62-
with pytest.raises(TypeError):
64+
msg = "You cannot access the property foo"
65+
with pytest.raises(TypeError, match=msg):
6366
delegate.foo()
6467

6568
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
@@ -85,8 +88,8 @@ class T(NoNewAttributesMixin):
8588
t._freeze()
8689
assert "__frozen" in dir(t)
8790
assert getattr(t, "__frozen")
88-
89-
with pytest.raises(AttributeError):
91+
msg = "You cannot add any new attribute"
92+
with pytest.raises(AttributeError, match=msg):
9093
t.b = "test"
9194

9295
assert not hasattr(t, "b")
@@ -129,7 +132,8 @@ def test_constructor_datetime_outofbound(self, a, klass):
129132
# datetime64[non-ns] raise error, other cases result in object dtype
130133
# and preserve original data
131134
if a.dtype.kind == "M":
132-
with pytest.raises(pd.errors.OutOfBoundsDatetime):
135+
msg = "Out of bounds"
136+
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
133137
klass(a)
134138
else:
135139
result = klass(a)
@@ -138,5 +142,6 @@ def test_constructor_datetime_outofbound(self, a, klass):
138142

139143
# Explicit dtype specified
140144
# Forced conversion fails for all -> all cases raise error
141-
with pytest.raises(pd.errors.OutOfBoundsDatetime):
145+
msg = "Out of bounds"
146+
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
142147
klass(a, dtype="datetime64[ns]")

pandas/tests/base/test_conversion.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ def test_array(array, attr, index_or_series):
303303

304304
def test_array_multiindex_raises():
305305
idx = pd.MultiIndex.from_product([["A"], ["a", "b"]])
306-
with pytest.raises(ValueError, match="MultiIndex"):
306+
msg = "MultiIndex has no single backing array"
307+
with pytest.raises(ValueError, match=msg):
307308
idx.array
308309

309310

@@ -429,11 +430,11 @@ def test_to_numpy_na_value_numpy_dtype(container, values, dtype, na_value, expec
429430
def test_to_numpy_kwargs_raises():
430431
# numpy
431432
s = pd.Series([1, 2, 3])
432-
match = r"to_numpy\(\) got an unexpected keyword argument 'foo'"
433-
with pytest.raises(TypeError, match=match):
433+
msg = r"to_numpy\(\) got an unexpected keyword argument 'foo'"
434+
with pytest.raises(TypeError, match=msg):
434435
s.to_numpy(foo=True)
435436

436437
# extension
437438
s = pd.Series([1, 2, 3], dtype="Int64")
438-
with pytest.raises(TypeError, match=match):
439+
with pytest.raises(TypeError, match=msg):
439440
s.to_numpy(foo=True)

pandas/tests/base/test_ops.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ def check_ops_properties(self, props, filter=None, ignore_failures=False):
123123

124124
# an object that is datetimelike will raise a TypeError,
125125
# otherwise an AttributeError
126+
msg = "no attribute"
126127
err = AttributeError
127128
if issubclass(type(o), DatetimeIndexOpsMixin):
128129
err = TypeError
129-
130-
with pytest.raises(err):
130+
with pytest.raises(err, match=msg):
131131
getattr(o, op)
132132

133133
@pytest.mark.parametrize("klass", [Series, DataFrame])
@@ -211,9 +211,10 @@ def test_none_comparison(self):
211211
if is_datetime64_dtype(o) or is_datetime64tz_dtype(o):
212212
# Following DatetimeIndex (and Timestamp) convention,
213213
# inequality comparisons with Series[datetime64] raise
214-
with pytest.raises(TypeError):
214+
msg = "Invalid comparison"
215+
with pytest.raises(TypeError, match=msg):
215216
None > o
216-
with pytest.raises(TypeError):
217+
with pytest.raises(TypeError, match=msg):
217218
o > None
218219
else:
219220
result = None > o
@@ -235,7 +236,8 @@ def test_ndarray_compat_properties(self):
235236
for p in ["flags", "strides", "itemsize", "base", "data"]:
236237
assert not hasattr(o, p)
237238

238-
with pytest.raises(ValueError):
239+
msg = "can only convert an array of size 1 to a Python scalar"
240+
with pytest.raises(ValueError, match=msg):
239241
o.item() # len > 1
240242

241243
assert o.ndim == 1
@@ -438,7 +440,8 @@ def test_value_counts_bins(self, index_or_series):
438440
s = klass(s_values)
439441

440442
# bins
441-
with pytest.raises(TypeError):
443+
msg = "bins argument only works with numeric data"
444+
with pytest.raises(TypeError, match=msg):
442445
s.value_counts(bins=1)
443446

444447
s1 = Series([1, 1, 2, 3])
@@ -857,7 +860,8 @@ def test_validate_bool_args(self):
857860
invalid_values = [1, "True", [1, 2, 3], 5.0]
858861

859862
for value in invalid_values:
860-
with pytest.raises(ValueError):
863+
msg = "expected type bool"
864+
with pytest.raises(ValueError, match=msg):
861865
self.int_series.drop_duplicates(inplace=value)
862866

863867
def test_getitem(self):
@@ -870,9 +874,11 @@ def test_getitem(self):
870874

871875
assert i[-1] == i[9]
872876

873-
with pytest.raises(IndexError):
877+
msg = "index 20 is out of bounds for axis 0 with size 10"
878+
with pytest.raises(IndexError, match=msg):
874879
i[20]
875-
with pytest.raises(IndexError):
880+
msg = "single positional indexer is out-of-bounds"
881+
with pytest.raises(IndexError, match=msg):
876882
s.iloc[20]
877883

878884
@pytest.mark.parametrize("indexer_klass", [list, pd.Index])

0 commit comments

Comments
 (0)