Skip to content

BUG: Scalar array comparison should return np.bool #27288

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 5 commits into from
Dec 11, 2024

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Aug 27, 2024

For scalar array comparison of different type such as np.array(1) == np.array('s') we return a numpy bool instead of a python bool.

See #27271

@eendebakpt eendebakpt changed the title Draft: BUG: Scalar array comparison should return np.bool BUG: Scalar array comparison should return np.bool Aug 27, 2024
@eendebakpt eendebakpt requested a review from seberg September 23, 2024 10:15
@seberg
Copy link
Member

seberg commented Oct 1, 2024

Thanks @eendebakpt the change LGTM (unrelated return changes are also fine). I doubt it will make issues, but maybe add a very short release note anyway?

@seberg
Copy link
Member

seberg commented Oct 2, 2024

Arrg, I noticed one thing that maybe we should defer. But our scalars do the same dance and still return the Python boolean.
And while it is easy to change here, I am not actually sure that it can be changed for the scalars because they may have to adhere to the NotImplemented protocol.

Without any comments, I may just go ahead soon, but I want to let it sink in briefly for myself.

@eendebakpt
Copy link
Contributor Author

Which scalar operations return the Python bool? I checked with

import numpy as np
print(np, np.__version__)

for pair in [( np.array(1), np.array('s')), ( np.array(1), np.int16(2)), ( np.array(1), 2),  (np.float16(1), np.int16(1)),  (np.float16(1), 3)]:
    r= pair[0] == pair[1]
    print(f'{pair}: {r=},\t{type(r)=}')
    r= pair[1] == pair[0]
    print(f'{pair[::-1]}: {r=},\t{type(r)=}')

and they return a numpy bool.

For the NotImplemented protocol (I checked https://docs.python.org/3/reference/datamodel.html#object.__eq__) the method should either return NotImplemented , or an object that can be cast to True/False using bool. Returning a numpy.bool would be fine then.

@seberg
Copy link
Member

seberg commented Oct 2, 2024

Ah, I guess only the ones for which the comparison is not defined np.float64(3) != np.str_("a"). I.e. this path, but for scalars.

@eendebakpt
Copy link
Contributor Author

eendebakpt commented Oct 2, 2024

Thanks. So we have:

np.float64(3) != np.int16(4) # numpy.bool
np.float64(3) != np.str_("a") # python bool

This is not causing any issues (as far as i know), but it is a bit inconsistent.

Note to self: python bool might be returned here

Py_RETURN_NOTIMPLEMENTED;

@eendebakpt eendebakpt requested a review from seberg December 10, 2024 21:27
@seberg
Copy link
Member

seberg commented Dec 11, 2024

OK, thanks @eendebakpt let's give this a try. FWIW, we should of course return a 0-D array in some future (which this does not make any harder, I suspect it just means deleting the code branch completely).
But for now the scalar is right. I dislike the difference to the normal scalars, but c'est la vie.

@seberg seberg merged commit 8bf5d82 into numpy:main Dec 11, 2024
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy