Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Added missing error check in ndarray.__contains__ #15553

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 4 commits into from
Feb 13, 2020

Conversation

ganesh-k13
Copy link
Member

Added NULL checks.

resolves #15552
resolves pandas-dev/pandas#31922

Copy link
Member

@eric-wieser eric-wieser left a comment

Choose a reason for hiding this comment

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

Only style nits, otherwise looks good

@seberg
Copy link
Member

seberg commented Feb 12, 2020

Thanks, would you be happy to add a test as well? It will need to be an object which raises an error when you call __bool__ and returns self for __eq__ probably.

@ganesh-k13
Copy link
Member Author

Glad to add tests. I'm new here and did not quite understand when you say

raises an error when you call __bool__

@seberg
Copy link
Member

seberg commented Feb 12, 2020

This is the implementation of a python class with the specific behaviour, such as:

class MyNA:
    def __bool__(self):
        raise ValueError("MyNA cannot be interpreted as a boolean value.")

Since the code will also call MyNA() == MyNA() here, you also have to implement __eq__ check the special dunder methods when creating a python class.

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Feb 13, 2020
@ganesh-k13
Copy link
Member Author

Guys, I can't see a workaround, it's working in the shell but failing in the test script:

class CustomNA:
	def __bool__(self):
		raise TypeError("CustomNA cannot be interpreted as a boolean value.")

	def __eq__(self, other):
		return self
CustomNA() in np.array(["a"], dtype=object)

DeprecationWarning: elementwise comparison failed; this will raise an error in the future.

In shell:

>>> CustomNA() not in np.array(["a"], dtype=object)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in __bool__
TypeError: CustomNA cannot be interpreted as a boolean value.

Can you please help me. Apologies if it's a very basic doubt.

@seberg
Copy link
Member

seberg commented Feb 13, 2020

No, that is not basic at all... The whole issue is far more complicated than it appears, and the DeprecationWarning (which is raised as an error in the test, you will see identical results with import warnings; warnings.simplefilter("error")) makes things tricky.

The "correct" test could be hacked by adding __array_priority__ = 1 (which also shows how badly broken __contains__ is in yet another way...

Maybe Eric anticipated this. I am OK with just putting it in without a test also, it gets complicated to cover the exact code path you are hitting... But you can add it referencing this issue with gh-15553 in a comment so that there is at least some fighting chance of understanding what that test does/did...

@ganesh-k13
Copy link
Member Author

Oh I see, I guess we can add that UT after fixing the main issue.

@seberg
Copy link
Member

seberg commented Feb 13, 2020

OK, I will just merge this for now. If anyone feels we should add the test with __array_priority__ just comment and I will do it, but it does feel a bit complicated.

The test failures are unrelated due to some gfortran issue.

@seberg seberg changed the title BUG: Added NULL check BUG: Added missing error check in ndarray.__contains__ Feb 13, 2020
@seberg seberg merged commit 491f41a into numpy:master Feb 13, 2020
@ganesh-k13
Copy link
Member Author

Thanks for the merge @seberg, I'll try it myself and ask here if I'm stuck.

charris pushed a commit to charris/numpy that referenced this pull request Mar 2, 2020
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: array_contains missing error check BUG: segmentation fault with pd.NA and np.ndarray.__contains__
4 participants