Skip to content

TST: Assert msg with pytest raises in pandas/tests/extension/base #38232

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
Show file tree
Hide file tree
Changes from 14 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
39 changes: 34 additions & 5 deletions pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@ def test_getitem_mask(self, data):

def test_getitem_mask_raises(self, data):
mask = np.array([True, False])
with pytest.raises(IndexError):
msg = f"Boolean index has wrong length: 2 instead of {len(data)}"
with pytest.raises(IndexError, match=msg):
data[mask]

mask = pd.array(mask, dtype="boolean")
with pytest.raises(IndexError):
with pytest.raises(IndexError, match=msg):
data[mask]

def test_getitem_boolean_array_mask(self, data):
Expand Down Expand Up @@ -305,7 +306,17 @@ def test_take_empty(self, data, na_value, na_cmp):
result = empty.take([-1], allow_fill=True)
assert na_cmp(result[0], na_value)

with pytest.raises(IndexError):
if empty.dtype.name == "arrow_string":
msg = "Index -1 out of bounds"
elif empty.dtype.name == "json":
msg = (
"Index is out of bounds or cannot do a non-empty take "
"from an empty array."
)
else:
msg = "cannot do a non-empty take from an empty axes."

with pytest.raises(IndexError, match=msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also probably be simplified to something like

        msg = 'cannot do a non-empty take from an empty axes|out of bounds'

empty.take([-1])

with pytest.raises(IndexError, match="cannot do a non-empty take"):
Expand All @@ -330,13 +341,31 @@ def test_take_non_na_fill_value(self, data_missing):
self.assert_extension_array_equal(result, expected)

def test_take_pandas_style_negative_raises(self, data, na_value):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=""):
data.take([0, -2], fill_value=na_value, allow_fill=True)

@pytest.mark.parametrize("allow_fill", [True, False])
def test_take_out_of_bounds_raises(self, data, allow_fill):
arr = data[:3]
with pytest.raises(IndexError):
if (arr.dtype.name == "arrow_string") | ("Sparse" in arr.dtype.name):
msg = "out of bounds value in 'indices'."
elif arr.dtype.name == "json":
msg = (
"Index is out of bounds or cannot do a non-empty take "
"from an empty array."
)
else:
if allow_fill:
msg = "indices are out-of-bounds"
else:
if ("numpy" not in str(type(arr))) | (
arr.dtype.name not in ["string"]
):
msg = "index 3 is out of bounds for axis 0 with size 3"
else:
msg = "index 3 is out of bounds for size 3"

with pytest.raises(IndexError, match=msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to match the whole message here, something like

        with pytest.raises(IndexError, match='out of bounds|out-of-bounds'):
            arr.take(np.asarray([0, 3]), allow_fill=allow_fill)

should be enough

arr.take(np.asarray([0, 3]), allow_fill=allow_fill)

def test_take_series(self, data):
Expand Down
49 changes: 47 additions & 2 deletions pandas/tests/extension/base/reduce.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import warnings

import pytest
Expand All @@ -8,6 +9,46 @@
from .base import BaseExtensionTests


def generate_error_message(dtype, op_name):
"""
Generate an error message returned by the following

getattr(s, op_name)(skipna=skipna)

where `s` is an object with datatype `dtype`, `op_name` is an operation
to be performed on `s`, and `skipna` is a boolean flag.

Parameters
----------
dtype : str
Datatype of the object `s`
op_name : str
Name of operation to perform on `s`

Returns
-------
str
Error message caused by performing the operation
"""
if dtype == "category":
if op_name in ["min", "max"]:
return re.escape(
f"Categorical is not ordered for operation {op_name}\n"
"you can use .as_ordered() to change the Categorical to an "
"ordered one\n"
)
else:
return f"'Categorical' does not implement reduction '{op_name}'"
elif dtype in ["string", "arrow_string"]:
return f"Cannot perform reduction '{op_name}' with string dtype"
elif dtype == "interval":
return rf"cannot perform {op_name} with type interval\[float64\]"
elif dtype == "json":
return f"cannot perform {op_name} with type json"
elif dtype == "arrow_bool":
return ""


class BaseReduceTests(BaseExtensionTests):
"""
Reduction specific tests. Generally these only
Expand All @@ -28,15 +69,19 @@ def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna):
op_name = all_numeric_reductions
s = pd.Series(data)

with pytest.raises(TypeError):
msg = generate_error_message(s.dtype.name, op_name)

with pytest.raises(TypeError, match=msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than adding a new function, maybe we could just check

msg = "[Cc]annot perform|Categorical is not ordered for operation|'Categorical' does not implement reduction"

getattr(s, op_name)(skipna=skipna)

@pytest.mark.parametrize("skipna", [True, False])
def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna):
op_name = all_boolean_reductions
s = pd.Series(data)

with pytest.raises(TypeError):
msg = generate_error_message(s.dtype.name, op_name)

with pytest.raises(TypeError, match=msg):
getattr(s, op_name)(skipna=skipna)


Expand Down