-
-
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
TST: Assert msg with pytest raises in pandas/tests/extension/base #38232
Conversation
Hello @marktgraham! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found: There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻 Comment last updated at 2020-12-11 16:27:05 UTC |
The reason the tests are failing is because I don't know what causes the difference between these two error messages:
The error message is I've tried a couple of different things, but neither has worked. I thought it was something to do with the number of dimensions but that doesn't seem to work either. |
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.
match
is matched with re.search
- we don't necessarily need to match the entire error message if that's too complicated and means having to define extra helper functions, it's probably good to avoid too much logic in tests
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 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
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): |
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
msg = 'cannot do a non-empty take from an empty axes|out of bounds'
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 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"
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.
Looks good to me, thanks @marktgraham !
Thanks @MarcoGorelli ! |
thanks @marktgraham |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
This PR adds messages to the bare pytest raises in reduce.py and getitem.py in pandas/test/extension/base. This PR references #30999.