-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
ENH: Allow subscript access for np.bool
by adding __class_getitem__
#29370
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 guess this one has been sitting for a month or so, perhaps because it is a bit esoteric. I checked locally that the regression test fails before the source patch is applied.
I also verified that
assert_type(cls, np.dtype[npt.NDArray[bool]])
works on this branch, as does the originalassert_type(cls, np.dtype[np.bool[bool]])
. I find the latter a little harder to parse, perhaps because it appears to be information-redundant, but I mostly gave up on understanding the static typing details beyond the surface level.Anyway, I guess this one is for @jorenham to check? Would it make sense to have the explicit
assert_type
check somewhere as well, rather than just the absence of error here? I'm pretty sure I've seen some test files withassert_type
all over the place--not sure if those would run the runtime checks or just the "type checking" time checks, but Joren will know.I'm not sure I really want to know the answer, but is there an intuitive explanation for why only the
np.bool
type allows subscripting, and not the others?The runtime type check seems a bit confusing though, because the diff below the fold also appears to pass on this branch:
The
reveal_type
seems pretty crude forcls
in general across the types at runtime here, which kind of makes me wonder how much value one really gets out of the runtime check in the original ticket--I suppose not much, and the only real argument was that it shouldn't error out.Uh oh!
There was an error while loading. Please reload this page.
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.
Thank you for checking this so thoroughly!
I agree it could be useful to add an explicit
assert_type
test.I am happy to update the PR to include that. But, I am also not sure about the right place to add them, so I will wait an answer from Joren.
If my understanding is correct, some users perfer to treat True and False as independent types similar to
Literal[True]
andLiteral[False]
. (Boolean has only two variants, so it is possible to do so.)Thank you again for the further suggestions. I didn't realize that
reveal_type
is so crude for cls.I might have over-modified things here, as you point out. I'll fix it if you think it’s preferable.
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.
You could also write
assert_type(cls, np.dtype[np.bool])
, becausenp.bool
is exactly equivalent tonp.bool[bool]
.assert_type
is purely a static check, and this PR only affects runtime behavior, so I don't think that's needed.As @riku-sakamoto correctly explained, the main use-case of
np.bool
's generic type parameter, is so that static type-checkers are able to distinguish betweennp.True_
andnp.False_
.In
scipy-stubs
, for example, this allows users to pass e.g.keepdims=np.True_
, and the return type will be inferred as e.g.Array[float64]
. Ifnp.bool
wouldn't have been generic, then that would instead have beenfloat64 | Array[float64]
.But the downside of having this generic type-parameter, is that type-checkers will now display the type of
a: np.bool = ...
asnp.bool[bool]
. That's because thenp.bool
type parameter defaults tobuiltins.bool
if omitted. Type-checkers like pyright and mypy will interpretnp.bool
asnp.bool[builtins.bool]
, and report it as such.But they don't have to display it that way. The type-checkers could also have decided to report
np.bool
asnp.bool
. So the way I see it, this unnecessarily verbose formatting is not our responsibility, but that of the type-checkers.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.
Another use-case that's kinda fun (and kinda niche), is that static type-checkers now understand boolean algebra:
numpy/numpy/typing/tests/data/reveal/bitwise_ops.pyi
Lines 154 to 167 in 4e00e4d
where
b0_ = np.False_
andb1_ = np.True_
.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.
nitpick: you forgot the
Literal
(and there are 4 dots), i.e.