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

Conversation

marktgraham
Copy link
Contributor

@marktgraham marktgraham commented Dec 2, 2020

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

This PR adds messages to the bare pytest raises in reduce.py and getitem.py in pandas/test/extension/base. This PR references #30999.

@marktgraham marktgraham marked this pull request as draft December 2, 2020 11:52
@jreback jreback added the Testing pandas testing functions or related to the test suite label Dec 2, 2020
@pep8speaks
Copy link

pep8speaks commented Dec 3, 2020

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

@marktgraham marktgraham marked this pull request as ready for review December 3, 2020 14:49
@marktgraham
Copy link
Contributor Author

The reason the tests are failing is because I don't know what causes the difference between these two error messages:

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"

The error message is AssertionError: Regex pattern 'index 3 is out of bounds for axis 0 with size 3' does not match 'index 3 is out of bounds for size 3'. i.e. it should be selecting the second one but it selects the first one.

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.

@MarcoGorelli MarcoGorelli self-requested a review December 7, 2020 07:12
Copy link
Member

@MarcoGorelli MarcoGorelli left a 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

Comment on lines 339 to 368
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

Comment on lines 308 to 319
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'

Comment on lines 72 to 74
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"

Copy link
Member

@MarcoGorelli MarcoGorelli left a 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 !

@MarcoGorelli MarcoGorelli added this to the 1.3 milestone Dec 11, 2020
@marktgraham
Copy link
Contributor Author

Thanks @MarcoGorelli !

@jreback jreback merged commit 6eeab01 into pandas-dev:master Dec 12, 2020
@jreback
Copy link
Contributor

jreback commented Dec 12, 2020

thanks @marktgraham

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants