Skip to content

Commit 2a82758

Browse files
committed
feat: astype: accept a kind of data type
1 parent 390e9cc commit 2a82758

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/array_api_stubs/_draft/data_type_functions.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def astype(
1616
x: array, dtype: dtype, /, *, copy: bool = True, device: Optional[device] = None
1717
) -> array:
1818
"""
19-
Copies an array to a specified data type irrespective of :ref:`type-promotion` rules.
19+
Copies an array to a specified data type irrespective of :ref:`type-promotion` rules, or to a *kind* of data type.
2020
2121
.. note::
2222
Casting floating-point ``NaN`` and ``infinity`` values to integral data types is not specified and is implementation-dependent.
@@ -40,8 +40,14 @@ def astype(
4040
----------
4141
x: array
4242
array to cast.
43-
dtype: dtype
44-
desired data type.
43+
dtype_or_kind: Union[dtype, str]
44+
desired data type or kind of data type. Supported kinds are:
45+
- ``'bool'``: boolean data types (e.g., ``bool``).
46+
- ``'signed integer'``: signed integer data types (e.g., ``int8``, ``int16``, ``int32``, ``int64``).
47+
- ``'unsigned integer'``: unsigned integer data types (e.g., ``uint8``, ``uint16``, ``uint32``, ``uint64``).
48+
- ``'integral'``: integer data types. Shorthand for ``('signed integer', 'unsigned integer')``.
49+
- ``'real floating'``: real-valued floating-point data types (e.g., ``float32``, ``float64``).
50+
- ``'complex floating'``: complex floating-point data types (e.g., ``complex64``, ``complex128``).
4551
copy: bool
4652
specifies whether to copy an array when the specified ``dtype`` matches the data type of the input array ``x``. If ``True``, a newly allocated array must always be returned. If ``False`` and the specified ``dtype`` matches the data type of the input array, the input array must be returned; otherwise, a newly allocated array must be returned. Default: ``True``.
4753
device: Optional[device]
@@ -50,7 +56,15 @@ def astype(
5056
Returns
5157
-------
5258
out: array
53-
an array having the specified data type. The returned array must have the same shape as ``x``.
59+
For ``dtype_or_kind`` a data type, an array having the specified data type.
60+
For ``dtype_or_kind`` a kind of data type:
61+
- If ``x.dtype`` is already of that kind, the data type is maintained.
62+
- Otherwise, an attempt is made to convert to the specified kind, according to the type promotion rules (see :ref:`type-promotion`).
63+
- Numeric kinds are interpreted as the lowest-precision standard data type of that kind for the purposes of type promotion.
64+
For example, ``astype(x, 'complex floating')`` will return an array with the data type ``complex64`` when ``x.dtype`` is ``float32``,
65+
since ``complex64`` is the result of promoting ``float32`` with the lowest-precision standard complex data type, ``complex64``.
66+
- For kind ``integral``, the 'lowest-precision standard data type' is interpreted as ``int8``, not ``uint8``.
67+
The returned array must have the same shape as ``x``.
5468
5569
Notes
5670
-----

0 commit comments

Comments
 (0)