Skip to content
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

match / case operation: np.bool_ vs bool #102905

Closed
ego-thales opened this issue Mar 22, 2023 · 5 comments
Closed

match / case operation: np.bool_ vs bool #102905

ego-thales opened this issue Mar 22, 2023 · 5 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@ego-thales
Copy link

ego-thales commented Mar 22, 2023

Hello,

Sorry, I'm not sure whether this is an intended behaviour or a bug -- couldn't find definitive answer here.
The following snippet doesn't match as I expected:

>>> b = np.True_
>>> b == True
True
>>> match b:
...  case True:
...   print("Ha!")
...  case _:
...   print("Oh...")
... 
Oh...

Tested on Python 3.10.6.

Thanks for your time.
Cheers!

@ego-thales ego-thales added the type-bug An unexpected behavior, bug, or error label Mar 22, 2023
@JosephSBoyle
Copy link
Contributor

Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x = np.bool_(1)
>>> x
True
>>> type(x)
<class 'numpy.bool_'>
>>> isinstance(x, bool)
False
>>>

Could be related to the fact that np.bool_ doesn't seem to be a subtype of bool.

@ego-thales ego-thales changed the title match / case operation: np(bool_) vs bool match / case operation: np.bool_ vs bool Mar 22, 2023
@sobolevn
Copy link
Member

Quoting PEP: https://peps.python.org/pep-0636/#matching-specific-values

Literal values are compared with the == operator except for the constants True, False and None which are compared with the is operator.

So, it works as it should :)

@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2023
@ego-thales
Copy link
Author

Thanks for the reference, I couldn't find it!
Do you have any info on why these constant should behave differently? It seems a bit strange without furhter insight.

🙏

@sobolevn
Copy link
Member

Because they are three special singletons :)

For example, 1 == 1.0 == True
Probably, matching True case with 1 value is not something users want.

@ego-thales
Copy link
Author

Ok thanks for your answer, I guess it makes sense.

I was wondering then what would the most natural way of matching a numpy boolean array be. I think the answer is to view the array with dtype=object, since

>>> np.True_.astype(object) is True
True

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants