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
ENH: Add __class_getitem__
to ndarray
, dtype
and number
#19879
Merged
Merged
Conversation
This file contains 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
BvB93
added
01 - Enhancement
62 - Python API
Changes or additions to the Python API. Mailing list should usually be notified.
labels
Sep 15, 2021
BvB93
commented
Sep 15, 2021
BvB93
commented
Sep 15, 2021
… of `npt.NDArray`
BvB93
force-pushed
the
cls_getitem
branch
3 times, most recently
from
September 16, 2021 10:59
4dd0a96
to
3fc9eb5
Compare
…erform basic validation of its input arguments It will still raise on python 3.8, but now with a more explicit exception message
@BvB93 Do you consider this ready? |
Personally, I'm happy with the current state of the PR. I'm not sure if anyone else has some final remarks? |
Let's give it a shot. Sounds like we will want it in Python >= 3.9 in any case. Thanks Bas. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
01 - Enhancement
62 - Python API
Changes or additions to the Python API. Mailing list should usually be notified.
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.
This PR adds the
__class_getitem__
classmethod to thendarray
,dtype
andnumber
classes,mimicking what PEP 585 did for the standard library. Consequently, expressions that were previously
only allowed in .pyi stub files or with the help of
from __future__ import annotations
are now alsolegal during runtime.
A couple things of note (most of them summarized from the linked PEP):
__class_getitem__
method returns parametrized wrapper (types.GenericAlias
) around the original type and, in most situations, can be used whenever the non-wrapped type is expected.types.GenericAlias
class is only available for for python >= 3.9, consequently__class_getitem__
will only be available for these version.types.GenericAlias
performsnominimal validation (i.e. checking the number of passed parameters) on the passed object, so this means that__class_getitem__
can in practice take any object. I'm personally not a huge fan of this, but, on the other hand, I feel that copying the stdlib approach here is the lesser of evils due to its simplicity and maintainability, especially since the more thorough argument parsing performed bytyping.Generic.__class_getitem__
is quite complex and prone to change.number
subclasses (uint8
,int32
, etc.) are treated as if they were already subscribed, and will thus raise upon calling__class_getitem__
due to a lack of free parameters.Examples