@@ -259,21 +259,48 @@ including the type hierarchy and the use of abstract DTypes.
259
259
Class getter
260
260
==============================================================================
261
261
262
- To create a dtype instance from a scalar type users now call ``np.dtype `` (for
263
- instance, ``np.dtype(np.int64) ``).
264
-
265
- To get the DType of a scalar type, we propose this getter syntax::
262
+ To create a DType instance from a scalar type users now call
263
+ ``np.dtype `` (for instance, ``np.dtype(np.int64) ``). Sometimes it is
264
+ also necessary to access the underlying DType class; this comes up in
265
+ particular with type hinting because the "type" of a DType instance is
266
+ the DType class. Taking inspiration from type hinting, we propose the
267
+ following getter syntax::
266
268
267
269
np.dtype[np.int64]
268
270
269
- The notation works equally well with built-in and user-defined DTypes
270
- and is inspired by and potentially useful for type hinting .
271
+ to get the DType class corresponding to a scalar type. The notation
272
+ works equally well with built-in and user-defined DTypes .
271
273
272
274
This getter eliminates the need to create an explicit name for every
273
- DType, crowding the ``np `` namespace; the getter itself signifies the type.
275
+ DType, crowding the ``np `` namespace; the getter itself signifies the
276
+ type. It also opens the possibility of making ``np.ndarray `` generic
277
+ over DType class using annotations like::
278
+
279
+ np.ndarray[np.dtype[np.float64]]
280
+
281
+ The above is fairly verbose, so it is possible that we will include
282
+ aliases like::
283
+
284
+ Float64 = np.dtype[np.float64]
285
+
286
+ in ``numpy.typing ``, thus keeping annotations concise but still
287
+ avoiding crowding the ``np `` namespace as discussed above. For a
288
+ user-defined DType::
289
+
290
+ class UserDtype(dtype): ...
291
+
292
+ one can do ``np.ndarray[UserDtype] ``, keeping annotations concise in
293
+ that case without introducing boilerplate in NumPy itself. For a user
294
+ user-defined scalar type::
295
+
296
+ class UserScalar(generic): ...
297
+
298
+ we would need to add a typing overload to ``dtype ``::
299
+
300
+ @overload
301
+ __new__(cls, dtype: Type[UserScalar], ...) -> UserDtype
274
302
275
- Since getter calls won't be needed often, this is unlikely to be burdensome.
276
- Classes can also offer concise alternatives.
303
+ to allow ``np.dtype[UserScalar] ``.
277
304
278
305
The initial implementation probably will return only concrete (not abstract)
279
306
DTypes.
@@ -393,7 +420,7 @@ casting and array coercion, which are described in detail below.
393
420
sortfunction `` that must return ``NotImplemented `` if the given ``sortkind ``
394
421
is not known.
395
422
396
- * Functions that cannot be removed are implemented as special methods.
423
+ * Functions that cannot be removed are implemented as special methods.
397
424
Many of these were previously defined part of the :c:type: `PyArray_ArrFuncs `
398
425
slot of the dtype instance (``PyArray_Descr * ``) and include functions
399
426
such as ``nonzero ``, ``fill `` (used for ``np.arange ``), and
@@ -408,7 +435,7 @@ casting and array coercion, which are described in detail below.
408
435
object to ensure uniqueness for all DTypes. On the C side, ``kind `` and
409
436
``char `` are set to ``\0 `` (NULL character).
410
437
While ``kind `` will be discouraged, the current ``np.issubdtype ``
411
- may remain the preferred method for this type of check.
438
+ may remain the preferred method for this type of check.
412
439
413
440
* A method ``ensure_canonical(self) -> dtype `` returns a new dtype (or
414
441
``self ``) with the ``canonical `` flag set.
@@ -1229,7 +1256,7 @@ Non-parametric dtypes do not have to implement:
1229
1256
1230
1257
* ``discover_descr_from_pyobject `` (uses ``default_descr `` instead)
1231
1258
* ``common_instance `` (uses ``default_descr `` instead)
1232
- * ``ensure_canonical `` (uses ``default_descr `` instead).
1259
+ * ``ensure_canonical `` (uses ``default_descr `` instead).
1233
1260
1234
1261
Sorting is expected to be implemented using:
1235
1262
0 commit comments