Skip to content

Commit 19ef830

Browse files
authored
Add specification for isdtype (#503)
1 parent cc5b5af commit 19ef830

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Diff for: spec/API_specification/array_api/data_type_functions.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ._types import Union, array, dtype, finfo_object, iinfo_object
1+
from ._types import Union, Tuple, array, dtype, finfo_object, iinfo_object
22

33
def astype(x: array, dtype: dtype, /, *, copy: bool = True) -> array:
44
"""
@@ -127,6 +127,41 @@ def iinfo(type: Union[dtype, array], /) -> iinfo_object:
127127
integer data type.
128128
"""
129129

130+
def isdtype(dtype: dtype, kind: Union[dtype, str, Tuple[Union[dtype, str], ...]]) -> bool:
131+
"""
132+
Returns a boolean indicating whether a provided dtype is of a specified data type "kind".
133+
134+
Parameters
135+
----------
136+
dtype: dtype
137+
the input dtype.
138+
kind: Union[str, dtype, Tuple[Union[str, dtype], ...]]
139+
data type kind.
140+
141+
- If ``kind`` is a dtype, the function must return a boolean indicating whether the input ``dtype`` is equal to the dtype specified by ``kind``.
142+
- If ``kind`` is a string, the function must return a boolean indicating whether the input ``dtype`` is of a specified data type kind. The following dtype kinds must be supported:
143+
144+
- **bool**: boolean data types (e.g., ``bool``).
145+
- **signed integer**: signed integer data types (e.g., ``int8``, ``int16``, ``int32``, ``int64``).
146+
- **unsigned integer**: unsigned integer data types (e.g., ``uint8``, ``uint16``, ``uint32``, ``uint64``).
147+
- **integral**: integer data types. Shorthand for ``('signed integer', 'unsigned integer')``.
148+
- **real floating**: real-valued floating-point data types (e.g., ``float32``, ``float64``).
149+
- **complex floating**: complex floating-point data types (e.g., ``complex64``, ``complex128``).
150+
- **numeric**: numeric data types. Shorthand for ``('integral', 'real floating', 'complex floating')``.
151+
152+
- If ``kind`` is a tuple, the tuple specifies a union of dtypes and/or kinds, and the function must return a boolean indicating whether the input ``dtype`` is either equal to a specified dtype or belongs to at least one specified data type kind.
153+
154+
.. note::
155+
A conforming implementation of the array API standard is **not** limited to only including the dtypes described in this specification in the required data type kinds. For example, implementations supporting ``float16`` and ``bfloat16`` can include ``float16`` and ``bfloat16`` in the ``real floating`` data type kind. Similarly, implementations supporting ``int128`` can include ``int128`` in the ``signed integer`` data type kind.
156+
157+
In short, conforming implementations may extend data type kinds; however, data type kinds must remain consistent (e.g., only integer dtypes may belong to integer data type kinds and only floating-point dtypes may belong to floating-point data type kinds), and extensions must be clearly documented as such in library documentation.
158+
159+
Returns
160+
-------
161+
out: bool
162+
boolean indicating whether a provided dtype is of a specified data type kind.
163+
"""
164+
130165
def result_type(*arrays_and_dtypes: Union[array, dtype]) -> dtype:
131166
"""
132167
Returns the dtype that results from applying the type promotion rules (see :ref:`type-promotion`) to the arguments.
@@ -145,4 +180,4 @@ def result_type(*arrays_and_dtypes: Union[array, dtype]) -> dtype:
145180
the dtype resulting from an operation involving the input arrays and dtypes.
146181
"""
147182

148-
__all__ = ['astype', 'can_cast', 'finfo', 'iinfo', 'result_type']
183+
__all__ = ['astype', 'can_cast', 'finfo', 'iinfo', 'isdtype', 'result_type']

Diff for: spec/API_specification/data_type_functions.rst

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ Objects in API
2222
can_cast
2323
finfo
2424
iinfo
25+
isdtype
2526
result_type

0 commit comments

Comments
 (0)