Repository navigation
ENH: Expand abstract DType hierarchy - #31755
Conversation
|
I don't think it's possible to make this type-safe; because there's no mechanism to "tell" static-type checkers that some registered custom dtype should be assignable to the relevant absract dtype if that custom dtype isn't an actual subclass of it. This is why using the The abstract dtypes themselves seem like a good idea to me though, it's just the |
|
Naming-wise, should |
Hmmm, maybe we don't need it strictly, although it is nice to tape over things for old-style dtypes. But... I hope to move One nice thing about registering (in the future) is that it allows easy creation for custom "superclasses" that also match NumPy if you want to and that can be very nice for promotion logic (i.e. to spell out
Yeah, I would prefer that, we just already ahve it in C... Maybe we can just add an alias there for now... |
register()dec3b0b to
aab2f99
Compare
|
Updated this and renamed the Python side for now only. (So maybe I am not 100% sure if there isn't some follow-up, but I think this should be ready -- although I should probably go through once more myself due to that back and forth.) |
jorenham
left a comment
There was a problem hiding this comment.
don't forget about the numpy.dtypes stubs :)
https://github.com/numpy/numpy/blob/7e1f94485495485c5cc3a408ab9e945940f1b91f/numpy/dtypes.pyi
(or, if you prefer, I could also write them) |
|
Sorry that this was not updated for a while. @jorenham I now pushed an update, the types are pretty much added by claude, they seemed reasonable, but can you take a look? I changed things now to:
|
Introduces ``NumericAbstractDType`` and a fuller numeric "kind" tree
mirroring the array-API hierarchy:
NumericAbstractDType
IntegerAbstractDType
SignedIntegerAbstractDType
UnsignedIntegerAbstractDType
InexactAbstractDType
FloatAbstractDType
ComplexAbstractDType
The new abstract DTypes are exposed on ``numpy.dtypes`` and the legacy
concrete DTypes inherit from them so ``isinstance``/``issubclass`` reflect
the kind hierarchy.
Adds ``register()`` on abstract DTypes for legacy/user-defined DTypes to
opt into a kind, with custom ``__subclasscheck__`` / ``__instancecheck__``
on the metaclass consulting the registered cache and the
``NPY_DT_NUMERIC`` flag.
``np.isdtype`` now uses and accepts DType classes (including these abstracts),
although passing a non-instance as first argument is deprecated as I think
it is a bit wonky.
(Do allow the scalar types as they map OK, but... really that shouldn't be
the normal use-case, I think.)
7a65561 to
193c506
Compare
jorenham
left a comment
There was a problem hiding this comment.
Looks a lot better now; nice.
I left some typing-related comments, most of them about stuff that can be removed.
jorenham
left a comment
There was a problem hiding this comment.
Typing changes look good now, thanks!
|
LGTM. This needs a release note |
|
Sorry, finally added a brief release note (and resolved merge conflict). |
|
The doctest failures are due to this PR. |
|
Thanks @seberg |
Introduces
NumericAbstractDTypeand a fuller numeric "kind" tree mirroring the array-API hierarchy:The new abstract DTypes are exposed on
numpy.dtypesand the legacy concrete DTypes inherit from them soisinstance/issubclassreflect the kind hierarchy.Adds
register()on abstract DTypes for legacy/user-defined DTypes to opt into a kind, with custom__subclasscheck__/__instancecheck__on the metaclass consulting the registered cache and theNPY_DT_NUMERICflag.np.isdtypenow uses and accepts DType classes (including these abstracts), although passing a non-instance as first argument is deprecated as I think it is a bit wonky.(Do allow the scalar types as they map OK, but... really that shouldn't be the normal use-case, I think.)
(I used a bit of AI to implement some larger patterns, and some of the tests are; I'll go through once more myself as well.)