|
1 |
| -# Static typing |
| 1 | +Static typing |
| 2 | +============= |
2 | 3 |
|
3 | 4 | Good support for static typing both in array libraries and array-consuming
|
4 | 5 | code is desirable. Therefore the exact type or set of types for each
|
5 | 6 | parameter, keyword and return value is specified for functions and methods -
|
6 |
| -see {ref}`function-and-method-signatures`. That section specifies arrays |
7 |
| -simply as `array`; what that means is dealt with in this section. |
| 7 | +see :ref:`function-and-method-signatures`. That section specifies arrays |
| 8 | +simply as ``array``; what that means is dealt with in this section. |
8 | 9 |
|
9 | 10 | Introducing type annotations in libraries became more relevant only when
|
10 | 11 | Python 2.7 support was dropped at the start of 2020. As a consequence, using
|
11 | 12 | type annotations with array libraries is largely still a work in progress.
|
12 |
| -This version of the API standard does not deal with trying to type _array |
13 |
| -properties_ like shape, dimensionality or dtype, because that's not a solved |
| 13 | +This version of the API standard does not deal with trying to type *array |
| 14 | +properties* like shape, dimensionality or dtype, because that's not a solved |
14 | 15 | problem in individual array libraries yet.
|
15 | 16 |
|
16 |
| -An `array` type annotation can mean either the type of one specific array |
| 17 | +An ``array`` type annotation can mean either the type of one specific array |
17 | 18 | object, or some superclass or typing Protocol - as long as it is consistent
|
18 |
| -with the array object specified in {ref}`array-object`. To illustrate by |
| 19 | +with the array object specified in :ref:`array-object`. To illustrate by |
19 | 20 | example:
|
20 | 21 |
|
21 |
| -```python |
22 |
| -# `Array` is a particular class in the library |
23 |
| -def sin(x: Array, / ...) -> Array: |
24 |
| - ... |
25 |
| -``` |
26 |
| - |
27 |
| -and |
28 |
| - |
29 |
| -```python |
30 |
| -# There's some base class `_BaseArray`, and there may be multiple |
31 |
| -# array subclasses inside the library |
32 |
| -A = TypeVar('A', bound=_BaseArray) |
33 |
| -def sin(x: A, / ...) -> A: |
34 |
| - ... |
35 |
| -``` |
| 22 | +.. code-block:: python |
| 23 | +
|
| 24 | + # `Array` is a particular class in the library |
| 25 | + def sin(x: Array, / ...) -> Array: |
| 26 | + ... |
| 27 | +
|
| 28 | +and |
| 29 | + |
| 30 | +.. code-block:: python |
| 31 | +
|
| 32 | + # There's some base class `_BaseArray`, and there may be multiple |
| 33 | + # array subclasses inside the library |
| 34 | + A = TypeVar('A', bound=_BaseArray) |
| 35 | + def sin(x: A, / ...) -> A: |
| 36 | + ... |
| 37 | +
|
36 | 38 | should both be fine. There may be other variations possible. Also note that
|
37 | 39 | this standard does not require that input and output array types are the same
|
38 | 40 | (they're expected to be defined in the same library though). Given that
|
39 | 41 | array libraries don't have to be aware of other types of arrays defined in
|
40 |
| -other libraries (see {ref}`assumptions-dependencies`), this should be enough |
| 42 | +other libraries (see :ref:`assumptions-dependencies`), this should be enough |
41 | 43 | for a single array library.
|
42 | 44 |
|
43 | 45 | That said, an array-consuming library aiming to support multiple array types
|
|
0 commit comments