-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
jreback
merged 18 commits into
pandas-dev:master
from
marktgraham:assert-msg-with-pytest-raises-extension-base
Dec 12, 2020
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7c3337a
match message using the generate_error_message method
a34501d
match error messages
ea9ab0c
Merge branch 'master' into assert-msg-with-pytest-raises-extension-base
5df082b
amend messages
69226b6
remove error message
51785f4
change message based on if dtype is float
f1d8437
add object dtype to check
1596905
add msgs for dtypes
7006201
make test pass by adding msgs for arrow_string and json dtypes
3c9b6bb
black-ify
ce3a18b
split line to pass pep8
42e95d4
Merge branch 'master' into assert-msg-with-pytest-raises-extension-base
2abad47
move import to pass pre-commit
e5e42da
check if name not in list of dtypes
82f0384
simplify matching (thanks to Marco's suggestions)
34cb8b1
Merge branch 'master' into assert-msg-with-pytest-raises-extension-base
bef81ee
remove import
da6ed3f
add check for empty error message
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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): | ||
empty.take([-1]) | ||
|
||
with pytest.raises(IndexError, match="cannot do a non-empty take"): | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import re | ||
import warnings | ||
|
||
import pytest | ||
|
@@ -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 | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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