Skip to content

Commit dc5e56d

Browse files
committed
Add a page on exceptions, and treat exceptions more consistently
Closes gh-606
1 parent 94a24c8 commit dc5e56d

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. _exceptions:
2+
3+
Exceptions
4+
==========
5+
6+
This standard specifies expected syntax and semantics for a set of APIs. When
7+
inputs to an API do not match what is expected, libraries may emit warnings,
8+
raise exceptions, or misbehave in unexpected ways. In general it is not
9+
possible to foresee or specify all the ways in which unexpected or invalid
10+
inputs are provided. Therefore, this standard does not attempt to specify
11+
exception or warning types to the extent needed in order to do exception
12+
handling in a portable manner. In general, it is expected that array library
13+
implementers follow `the guidance given by the documentation of the Python
14+
language <https://docs.python.org/3/library/exceptions.html>`__, and use either
15+
fitting builtin exception or warning types that are appropriate for the
16+
situation, or custom exceptions or warnings that derive from those builtin
17+
ones.
18+
19+
In specific cases, it may be useful to provide guidance to array library
20+
authors regarding what an appropriate exception is. That guidance will be
21+
phrased as *should* rather than *must* (typically in a *Raises* section),
22+
because (a) there may be reasons for an implementer to deviate, and (b) more
23+
often than not existing array library implementation already differ in their
24+
choices and it may not be worth them breaking backwards compatibility in order
25+
to comply with a "must" in this standard.
26+
27+
In other cases, this standard will only specify that an exception should or
28+
must be raised, but not mention what type of exception that is.

spec/draft/design_topics/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Design topics & constraints
1111
device_support
1212
static_typing
1313
accuracy
14+
exceptions
1415
complex_numbers
1516
C_API
1617
parallelism

src/array_api_stubs/_draft/manipulation_functions.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ def expand_dims(x: array, /, *, axis: int = 0) -> array:
6767
x: array
6868
input array.
6969
axis: int
70-
axis position (zero-based). If ``x`` has rank (i.e, number of dimensions) ``N``, a valid ``axis`` must reside on the closed-interval ``[-N-1, N]``. If provided a negative ``axis``, the axis position at which to insert a singleton dimension must be computed as ``N + axis + 1``. Hence, if provided ``-1``, the resolved axis position must be ``N`` (i.e., a singleton dimension must be appended to the input array ``x``). If provided ``-N-1``, the resolved axis position must be ``0`` (i.e., a singleton dimension must be prepended to the input array ``x``). An ``IndexError`` exception must be raised if provided an invalid ``axis`` position.
70+
axis position (zero-based). If ``x`` has rank (i.e, number of dimensions) ``N``, a valid ``axis`` must reside on the closed-interval ``[-N-1, N]``. If provided a negative ``axis``, the axis position at which to insert a singleton dimension must be computed as ``N + axis + 1``. Hence, if provided ``-1``, the resolved axis position must be ``N`` (i.e., a singleton dimension must be appended to the input array ``x``). If provided ``-N-1``, the resolved axis position must be ``0`` (i.e., a singleton dimension must be prepended to the input array ``x``).
7171
7272
Returns
7373
-------
7474
out: array
7575
an expanded output array having the same data type as ``x``.
76+
77+
Raises
78+
------
79+
IndexError
80+
If provided an invalid ``axis`` position, an ``IndexError`` should be raised.
7681
"""
7782

7883

@@ -125,12 +130,18 @@ def reshape(
125130
shape: Tuple[int, ...]
126131
a new shape compatible with the original shape. One shape dimension is allowed to be ``-1``. When a shape dimension is ``-1``, the corresponding output array shape dimension must be inferred from the length of the array and the remaining dimensions.
127132
copy: Optional[bool]
128-
boolean indicating whether or not to copy the input array. If ``True``, the function must always copy. If ``False``, the function must never copy and must raise a ``ValueError`` in case a copy would be necessary. If ``None``, the function must reuse existing memory buffer if possible and copy otherwise. Default: ``None``.
133+
whether or not to copy the input array. If ``True``, the function must always copy. If ``False``, the function must never copy. If ``None``, the function must avoid copying if possible, and may copy otherwise. Default: ``None``.
129134
130135
Returns
131136
-------
132137
out: array
133138
an output array having the same data type and elements as ``x``.
139+
140+
Raises
141+
------
142+
ValueError
143+
If ``copy=False`` and a copy would be necessary, a ``ValueError``
144+
should be raised.
134145
"""
135146

136147

@@ -169,12 +180,18 @@ def squeeze(x: array, /, axis: Union[int, Tuple[int, ...]]) -> array:
169180
x: array
170181
input array.
171182
axis: Union[int, Tuple[int, ...]]
172-
axis (or axes) to squeeze. If a specified axis has a size greater than one, a ``ValueError`` must be raised.
183+
axis (or axes) to squeeze.
173184
174185
Returns
175186
-------
176187
out: array
177188
an output array having the same data type and elements as ``x``.
189+
190+
Raises
191+
------
192+
ValueError
193+
If a specified axis has a size greater than one, i.e. it is not a
194+
singleton dimension, a ``ValueError`` should be raised.
178195
"""
179196

180197

0 commit comments

Comments
 (0)