Skip to content

Commit e7e6c6d

Browse files
committed
Align casting methods with Python behaviour
1 parent 8d669b2 commit e7e6c6d

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

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

+47-15
Original file line numberDiff line numberDiff line change
@@ -224,32 +224,38 @@ def __array_namespace__(self: array, /, *, api_version: Optional[str] = None) ->
224224

225225
def __bool__(self: array, /) -> bool:
226226
"""
227-
Converts a zero-dimensional boolean array to a Python ``bool`` object.
227+
Converts a zero-dimensional array to a Python ``bool`` object.
228+
229+
.. note::
230+
When casting a real-valued input array to ``bool``, a value of ``0`` must cast to ``False``, and a non-zero value must cast to ``True``.
228231
229232
Parameters
230233
----------
231234
self: array
232-
zero-dimensional array instance. Should have a boolean data type.
235+
zero-dimensional array instance.
233236
234237
Returns
235238
-------
236239
out: bool
237240
a Python ``bool`` object representing the single element of the array.
238241
"""
239242

240-
def __complex__(self: array, /) -> bool:
243+
def __complex__(self: array, /) -> complex:
241244
"""
242-
Converts a zero-dimensional complex array to a Python ``complex`` object.
245+
Converts a zero-dimensional array to a Python ``complex`` object.
246+
247+
.. note::
248+
When casting a boolean input array, a value of ``True`` must cast to ``1``, and a value of ``False`` must cast to ``0``.
243249
244250
Parameters
245251
----------
246252
self: array
247-
zero-dimensional array instance. Should have a complex data type.
253+
zero-dimensional array instance.
248254
249255
Returns
250256
-------
251257
out: complex
252-
a Python ``complex`` object representing the single element of the array.
258+
a Python ``complex`` object representing the single element of the array. If ``self`` has a real-valued or boolean data type, ``out`` must represent the element of ``self`` in the real component.
253259
"""
254260

255261
def __dlpack__(self: array, /, *, stream: Optional[Union[int, Any]] = None) -> PyCapsule:
@@ -356,12 +362,18 @@ def __eq__(self: array, other: Union[int, float, bool, array], /) -> array:
356362

357363
def __float__(self: array, /) -> float:
358364
"""
359-
Converts a zero-dimensional floating-point array to a Python ``float`` object.
365+
Converts a zero-dimensional array to a Python ``float`` object.
366+
367+
.. note::
368+
Casting integer values outside the representable bounds of Python's float type is not specified and is implementation-dependent.
369+
370+
.. note::
371+
When casting a boolean input array, a value of ``True`` must cast to ``1``, and a value of ``False`` must cast to ``0``.
360372
361373
Parameters
362374
----------
363375
self: array
364-
zero-dimensional array instance. Should have a real-valued floating-point data type.
376+
zero-dimensional array instance. Should have a real-valued or boolean data type. If ``self`` has a complex data type, the function must raise a ``TypeError``.
365377
366378
Returns
367379
-------
@@ -488,37 +500,57 @@ def __gt__(self: array, other: Union[int, float, array], /) -> array:
488500
Element-wise results must equal the results returned by the equivalent element-wise function :func:`~array_api.greater`.
489501
"""
490502

491-
def __index__(self: array, /) -> int:
503+
def __index__(self: array, /) -> Union[int, bool]:
492504
"""
493-
Converts a zero-dimensional integer array to a Python ``int`` object.
505+
Converts a zero-dimensional array to a Python object used in indexing.
494506
495507
.. note::
496508
This method is called to implement `operator.index() <https://docs.python.org/3/reference/datamodel.html#object.__index__>`_. See also `PEP 357 <https://www.python.org/dev/peps/pep-0357/>`_.
497509
498510
Parameters
499511
----------
500512
self: array
501-
zero-dimensional array instance. Should have an integer data type.
513+
zero-dimensional array instance. Should have an integer or boolean data type. If ``self`` has a floating-point or complex data type, the function must raise a ``TypeError``.
502514
503515
Returns
504516
-------
505-
out: int
506-
a Python ``int`` object representing the single element of the array instance.
517+
out: Union[int, bool]
518+
a Python object representing the single element of the array instance. If ``self`` has an integer data type, ``out`` must be a Python ``int`` object. If ``self`` has a boolean data type, ``out`` must be a Python ``bool`` object.
507519
"""
508520

509521
def __int__(self: array, /) -> int:
510522
"""
511-
Converts a zero-dimensional integer array to a Python ``int`` object.
523+
Converts a zero-dimensional array to a Python ``int`` object.
524+
525+
.. note::
526+
Casting floating-point values outside the representable bounds of Python's non-arbitary integer type is not specified and is implementation-dependent.
527+
528+
.. note::
529+
When casting a boolean input array, a value of ``True`` must cast to ``1``, and a value of ``False`` must cast to ``0``.
530+
531+
**Special cases**
532+
533+
For floating-point operands,
534+
535+
- If ``self`` is a finite number, the result is the integer part of ``self``.
512536
513537
Parameters
514538
----------
515539
self: array
516-
zero-dimensional array instance. Should have an integer data type.
540+
zero-dimensional array instance. Should have a real-valued or boolean data type. If ``self`` has a complex data type, the function must raise a ``TypeError``.
517541
518542
Returns
519543
-------
520544
out: int
521545
a Python ``int`` object representing the single element of the array instance.
546+
547+
548+
**Raises**
549+
550+
For floating-point operands,
551+
552+
- If ``self`` is either ``+infinity`` or ``-infinity``, raise ``OverflowError``.
553+
- If ``self`` is ``NaN``, raise ``ValueError``.
522554
"""
523555

524556
def __invert__(self: array, /) -> array:

0 commit comments

Comments
 (0)