Skip to content

TST:Disallow bare pytest raises issue 30999 #31138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pandas/tests/base/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ def test_invalid_delegation(self):

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

with pytest.raises(TypeError):
msg = "You cannot access the property foo"
with pytest.raises(TypeError, match=msg):
delegate.foo

with pytest.raises(TypeError):
msg = "The property foo cannot be set"
with pytest.raises(TypeError, match=msg):
delegate.foo = 5

with pytest.raises(TypeError):
msg = "You cannot access the property foo"
with pytest.raises(TypeError, match=msg):
delegate.foo()

@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
Expand All @@ -85,8 +88,8 @@ class T(NoNewAttributesMixin):
t._freeze()
assert "__frozen" in dir(t)
assert getattr(t, "__frozen")

with pytest.raises(AttributeError):
msg = "You cannot add any new attribute"
with pytest.raises(AttributeError, match=msg):
t.b = "test"

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

# Explicit dtype specified
# Forced conversion fails for all -> all cases raise error
with pytest.raises(pd.errors.OutOfBoundsDatetime):
msg = "Out of bounds"
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
klass(a, dtype="datetime64[ns]")
9 changes: 5 additions & 4 deletions pandas/tests/base/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ def test_array(array, attr, index_or_series):

def test_array_multiindex_raises():
idx = pd.MultiIndex.from_product([["A"], ["a", "b"]])
with pytest.raises(ValueError, match="MultiIndex"):
msg = "MultiIndex has no single backing array"
with pytest.raises(ValueError, match=msg):
idx.array


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

# extension
s = pd.Series([1, 2, 3], dtype="Int64")
with pytest.raises(TypeError, match=match):
with pytest.raises(TypeError, match=msg):
s.to_numpy(foo=True)
24 changes: 15 additions & 9 deletions pandas/tests/base/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def check_ops_properties(self, props, filter=None, ignore_failures=False):

# an object that is datetimelike will raise a TypeError,
# otherwise an AttributeError
msg = "no attribute"
err = AttributeError
if issubclass(type(o), DatetimeIndexOpsMixin):
err = TypeError

with pytest.raises(err):
with pytest.raises(err, match=msg):
getattr(o, op)

@pytest.mark.parametrize("klass", [Series, DataFrame])
Expand Down Expand Up @@ -211,9 +211,10 @@ def test_none_comparison(self):
if is_datetime64_dtype(o) or is_datetime64tz_dtype(o):
# Following DatetimeIndex (and Timestamp) convention,
# inequality comparisons with Series[datetime64] raise
with pytest.raises(TypeError):
msg = "Invalid comparison"
with pytest.raises(TypeError, match=msg):
None > o
with pytest.raises(TypeError):
with pytest.raises(TypeError, match=msg):
o > None
else:
result = None > o
Expand All @@ -235,7 +236,8 @@ def test_ndarray_compat_properties(self):
for p in ["flags", "strides", "itemsize", "base", "data"]:
assert not hasattr(o, p)

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

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

# bins
with pytest.raises(TypeError):
msg = "bins argument only works with numeric data"
with pytest.raises(TypeError, match=msg):
s.value_counts(bins=1)

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

for value in invalid_values:
with pytest.raises(ValueError):
msg = "expected type bool"
with pytest.raises(ValueError, match=msg):
self.int_series.drop_duplicates(inplace=value)

def test_getitem(self):
Expand All @@ -870,9 +874,11 @@ def test_getitem(self):

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

with pytest.raises(IndexError):
msg = "index 20 is out of bounds for axis 0 with size 10"
with pytest.raises(IndexError, match=msg):
i[20]
with pytest.raises(IndexError):
msg = "single positional indexer is out-of-bounds"
with pytest.raises(IndexError, match=msg):
s.iloc[20]

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