ENH: Add __class_getitem__ to ndarray, dtype and number#19879
Merged
charris merged 10 commits intonumpy:mainfrom Sep 25, 2021
Merged
ENH: Add __class_getitem__ to ndarray, dtype and number#19879charris merged 10 commits intonumpy:mainfrom
__class_getitem__ to ndarray, dtype and number#19879charris merged 10 commits intonumpy:mainfrom
Conversation
BvB93
commented
Sep 15, 2021
BvB93
commented
Sep 15, 2021
… of `npt.NDArray`
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
Member
|
@BvB93 Do you consider this ready? |
Member
Author
|
Personally, I'm happy with the current state of the PR. I'm not sure if anyone else has some final remarks? |
Member
|
Let's give it a shot. Sounds like we will want it in Python >= 3.9 in any case. Thanks Bas. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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,dtypeandnumberclasses,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 annotationsare 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.GenericAliasclass is only available for for python >= 3.9, consequently__class_getitem__will only be available for these version.types.GenericAliasperformsnominimal 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.numbersubclasses (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