|
1 | 1 | # Static typing
|
| 2 | + |
| 3 | +Good support for static typing both in array libraries and array-consuming |
| 4 | +code is desirable. Therefore the exact type or set of types for each |
| 5 | +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. |
| 8 | + |
| 9 | +Introducing type annotations in libraries became more relevant only when |
| 10 | +Python 2.7 support was dropped at the start of 2020. As a consequence, using |
| 11 | +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 |
| 14 | +problem in individual array libraries yet. |
| 15 | + |
| 16 | +An `array` type annotation can mean either the type of one specific array |
| 17 | +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 | +example: |
| 20 | + |
| 21 | +```python |
| 22 | +# `Array` is a particular class in the library |
| 23 | +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 | +sin(x: A, / ...) -> A: |
| 34 | + ... |
| 35 | +``` |
| 36 | +should both be fine. There may be other variations possible. Also note that |
| 37 | +this standard does not require that input and output array types are the same |
| 38 | +(they're expected to be defined in the same library though). Given that |
| 39 | +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 |
| 41 | +for a single array library. |
| 42 | + |
| 43 | +That said, an array-consuming library aiming to support multiple array types |
| 44 | +may need more - for example a protocol to enable structural subtyping. This |
| 45 | +API standard currently takes the position that it does not provide any |
| 46 | +reference implementation or package that can or should be relied on at |
| 47 | +runtime, hence no such protocol is defined here. This may be dealt with in a |
| 48 | +future version of this standard. |
0 commit comments