8000 NEP: update NEP 42 with discussion of type hinting applications (#17447) · numpy/numpy@789b217 · GitHub
[go: up one dir, main page]

Skip to content

Commit 789b217

Browse files
authored
NEP: update NEP 42 with discussion of type hinting applications (#17447)
* NEP: update NEP 42 with discussion of type hinting applications As discussed in #16759, the new DType classes provide a good path forward for making `ndarray` generic over DType. Update NEP 42 to discuss those applications in more detail. * NEP: discuss typing for use scalar types in NEP 42 Also clean up the language a bit.
1 parent c20f08a commit 789b217

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

doc/neps/nep-0042-new-dtypes.rst

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,48 @@ including the type hierarchy and the use of abstract DTypes.
259259
Class getter
260260
==============================================================================
261261

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::
266268

267269
np.dtype[np.int64]
268270

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.
271273

272274
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
274302

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]``.
277304

278305
The initial implementation probably will return only concrete (not abstract)
279306
DTypes.
@@ -393,7 +420,7 @@ casting and array coercion, which are described in detail below.
393420
sortfunction`` that must return ``NotImplemented`` if the given ``sortkind``
394421
is not known.
395422

396-
* Functions that cannot be removed are implemented as special methods.
423+
* Functions that cannot be removed are implemented as special methods.
397424
Many of these were previously defined part of the :c:type:`PyArray_ArrFuncs`
398425
slot of the dtype instance (``PyArray_Descr *``) and include functions
399426
such as ``nonzero``, ``fill`` (used for ``np.arange``), and
@@ -408,7 +435,7 @@ casting and array coercion, which are described in detail below.
408435
object to ensure uniqueness for all DTypes. On the C side, ``kind`` and
409436
``char`` are set to ``\0`` (NULL character).
410437
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.
412439

413440
* A method ``ensure_canonical(self) -> dtype`` returns a new dtype (or
414441
``self``) with the ``canonical`` flag set.
@@ -1229,7 +1256,7 @@ Non-parametric dtypes do not have to implement:
12291256

12301257
* ``discover_descr_from_pyobject`` (uses ``default_descr`` instead)
12311258
* ``common_instance`` (uses ``default_descr`` instead)
1232-
* ``ensure_canonical`` (uses ``default_descr`` instead).
1259+
* ``ensure_canonical`` (uses ``default_descr`` instead).
12331260

12341261
Sorting is expected to be implemented using:
12351262

0 commit comments

Comments
 (0)
0