Skip to content

Commit acb49cd

Browse files
committed
docs: update guidance
1 parent 911ee68 commit acb49cd

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

spec/draft/API_specification/indexing.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ Integer Array Indexing
187187

188188
An array must support indexing by an indexing tuple which contains only integers and integer arrays according to the following rules. Let ``A`` be an ``N``-dimensional array with shape ``S1``. Let ``T`` be a tuple ``(t1, t2, ..., tN)`` having length ``N``. Let ``tk`` be an individual element of ``T``.
189189

190+
.. note::
191+
This specification does not currently address indexing tuples which combine slices and integer arrays. Behavior for such indexing tuples is left unspecified and thus implementation-defined. This may be revisited in a future revision of this standard.
192+
193+
.. note::
194+
This specification does not currently address indexing tuples which include array-like elements, such as Python lists, tuples, and other sequences. Behavior when indexing an array using array-like elements is left unspecified and thus implementation-defined.
195+
196+
- If ``tk`` is an integer array, ``tk`` should have the default array index data type (see :ref:`data-type-defaults`).
197+
198+
.. note::
199+
Conforming implementations of this standard may support integer arrays having other integer data types; however, consumers of this standard should be aware that integer arrays having uncommon array index data types such as ``int8`` and ``uint8`` may not be widely supported as index arrays across conforming array libraries. To dynamically resolve the default array index data type, including for that of the current device context, use the inspection API ``default_dtypes()``.
200+
190201
- Providing a zero-dimensional integer array ``tk`` containing an integer index must be equivalent to providing an integer index having the value ``tk[()]``. Conversely, each integer index ``tk`` must be equivalent to a zero-dimensional integer array containing the same value and be treated as such, including shape inference and broadcasting. Accordingly, if ``T`` consists of only integers and zero-dimensional integer arrays, the result must be equivalent to indexing multiple axes using integer indices. For example, if ``A`` is a two-dimensional array, ``T`` is the tuple ``(i, J)``, ``i`` is a valid integer index, and ``J`` is a zero-dimensional array containing a valid integer index ``j``, the result of ``A[T]`` must be equivalent to ``A[(i,j)]`` (see :ref:`indexing-multi-axis`).
191202

192203
- If ``tk`` is an integer array, each element in ``tk`` must independently satisfy the rules stated above for indexing a single-axis with an integer index (see :ref:`indexing-single-axis`).
@@ -196,8 +207,8 @@ An array must support indexing by an indexing tuple which contains only integers
196207

197208
- If ``tk`` is an integer array containing duplicate valid integer indices, the result must include the corresponding elements of ``A`` with the same duplication.
198209

199-
.. note::
200-
Given the assignment operation ``x[T] = y[...]``, if ``T`` contains an integer array having duplicate indices, the order in which elements in ``y`` are assigned to the corresponding element(s) in ``x`` is unspecified and thus implementation-defined.
210+
..
211+
TODO: once setitem semantics are determined, insert the following note: Given the assignment operation ``x[T] = y[...]``, if ``T`` contains an integer array having duplicate indices, the order in which elements in ``y`` are assigned to the corresponding element(s) in ``x`` is unspecified and thus implementation-defined.
201212
202213
- If ``T`` contains at least one non-zero-dimensional integer array, all elements of ``T`` must be broadcast against each other to determine a common shape ``S2 = (s1, s2, ..., sN)`` according to standard broadcasting rules (see :ref:`broadcasting`). If one or more elements in ``T`` are not broadcast-compatible with the others, an exception must be raised.
203214

@@ -207,9 +218,6 @@ An array must support indexing by an indexing tuple which contains only integers
207218

208219
- The result of ``A[U]`` must be an array having the broadcasted shape ``S2``.
209220

210-
.. note::
211-
This specification does not currently address indexing tuples which combine slices and integer arrays. Behavior for such indexing tuples is left unspecified and thus implementation-defined. This may be revisited in a future revision of this standard.
212-
213221
Boolean Array Indexing
214222
----------------------
215223

src/array_api_stubs/_draft/array_object.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -610,30 +610,31 @@ def __getitem__(
610610
slice,
611611
ellipsis,
612612
None,
613-
Tuple[Union[int, slice, ellipsis, None], ...],
613+
Tuple[Union[int, slice, ellipsis, array, None], ...],
614614
array,
615615
],
616616
/,
617617
) -> array:
618618
"""
619619
Returns ``self[key]``.
620620
621-
See :ref:`indexing` for details on supported indexing semantics.
622-
623621
Parameters
624622
----------
625623
self: array
626624
array instance.
627-
key: Union[int, slice, ellipsis, None, Tuple[Union[int, slice, ellipsis, None], ...], array]
625+
key: Union[int, slice, ellipsis, None, Tuple[Union[int, slice, ellipsis, array, None], ...], array]
628626
index key.
629627
630628
Returns
631629
-------
632630
out: array
633631
an array containing the accessed value(s). The returned array must have the same data type as ``self``.
634632
635-
.. note::
636-
When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having zero dimensions or more than one dimension is unspecified and thus implementation-defined.
633+
Notes
634+
-----
635+
636+
- See :ref:`indexing` for details on supported indexing semantics.
637+
- When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having zero dimensions or more than one dimension is unspecified and thus implementation-defined.
637638
638639
"""
639640

@@ -1081,28 +1082,31 @@ def __rshift__(self: array, other: Union[int, array], /) -> array:
10811082
def __setitem__(
10821083
self: array,
10831084
key: Union[
1084-
int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], array
1085+
int, slice, ellipsis, Tuple[Union[int, slice, ellipsis, array], ...], array
10851086
],
10861087
value: Union[int, float, complex, bool, array],
10871088
/,
10881089
) -> None:
10891090
"""
10901091
Sets ``self[key]`` to ``value``.
10911092
1092-
See :ref:`indexing` for details on supported indexing semantics.
1093-
10941093
Parameters
10951094
----------
10961095
self: array
10971096
array instance.
1098-
key: Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis], ...], array]
1097+
key: Union[int, slice, ellipsis, Tuple[Union[int, slice, ellipsis, array], ...], array]
10991098
index key.
11001099
value: Union[int, float, complex, bool, array]
11011100
value(s) to set. Must be compatible with ``self[key]`` (see :ref:`broadcasting`).
11021101
11031102
Notes
11041103
-----
11051104
1105+
- See :ref:`indexing` for details on supported indexing semantics.
1106+
1107+
.. note::
1108+
Indexing semantics when ``key`` is an integer array or a tuple of integers and integer arrays is currently unspecified and thus implementation-defined. This will be revisited in a future revision of this standard.
1109+
11061110
- Setting array values must not affect the data type of ``self``.
11071111
- When ``value`` is a Python scalar (i.e., ``int``, ``float``, ``complex``, ``bool``), behavior must follow specification guidance on mixing arrays with Python scalars (see :ref:`type-promotion`).
11081112
- When ``value`` is an ``array`` of a different data type than ``self``, how values are cast to the data type of ``self`` is implementation defined.

0 commit comments

Comments
 (0)