Python array API standard typing
Documentation: https://array-api.readthedocs.io
Source Code: https://github.com/34j/types-array-api
Typing for array API and array-api-compat
Installation
Install this via pip (or your favourite package manager):
pip install types-array-api
Usage
Type stubs
Autocompletion for array-api-compat is available in your IDE just by installing this package.
import array_api_compat
xp = array_api_compat.array_namespace(x)
Typing functions using Array
There are multiple ways to type functions:
-
from array_api.latest import Array def simple(x: Array) -> Array: return x + 1
The simplest way to enjoy autocompletion for
Array. This should be enough for most use cases. -
To make sure that the same type of array is returned (
ndarray→ndarray,Tensor→Tensor), aTypeVarbound toArraycan be used:def generic[TArray: Array](x: TArray) -> TArray: return x + 1
Advanced Usage
Namespace Type
You can test if an object matches the Protocol as they are runtime-checkable:
import array_api_strict
from array_api.latest import ArrayNamespace, ArrayNamespaceFull
assert isinstance(array_api_strict, ArrayNamespace)
# Full version contains fft and linalg
# fft and linalg are not included by default in array_api_strict
assert not isinstance(array_api_strict, ArrayNamespaceFull)
Shape Typing
-
To clarify the input and output shapes,
ShapedArrayandShapedAnyArraycan be used:from array_api.latest import ShapedAnyArray as Array def sum_last_axis[*TShape](x: Array[*TShape, Any]) -> Array[*TShape]: return xp.sum(x, axis=-1)
More complex example using NewType or type aliases:
RTheta = NewType("RTheta", int) XY = NewType("XY", int) def polar_coordinates[*TShape](randtheta: Array[*TShape, RTheta]) -> Array[*TShape, XY]: """Convert polar coordinates to Cartesian coordinates.""" r = randtheta[..., 0] theta = randtheta[..., 1] x = r * xp.cos(theta) y = r * xp.sin(theta) return xp.stack((x, y), axis=-1)
Note that
ShapedAnyArrayexists only for documentation purposes and internally it is treated asArray. Using both generic and shaped are impossible due to python/typing#548. -
Note that the below example is ideal but impossible due to Python specification.
def impossible[ TDtype, TDevice, *TShapeFormer: int, *TShapeLatter: int, TArray: Array ](x: TArray[*TShapeFormer, *TShapeLatter | Literal[1], TDtype, TDevice], y: TArray[*TShapeLatter | Literal[1], TDtype, TDevice]) -> TArray[*TShapeFormer, *TShapeLatter, TDtype, TDevice]: return x + y # broadcasting
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Credits
This package was created with Copier and the browniebroke/pypackage-template project template.
Metadata
Release files for types-array-api 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| types_array_api-1.3.0.tar.gz | 472.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| types_array_api-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 949.1 kB
Release files / types_array_api-1.3.0.tar.gz
| Download URL | types_array_api-1.3.0.tar.gz |
|---|---|
| Size | 472.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
918526e110c1008618cd9b4487f98aa518f3869efc67b0da2491ceff1b53efcd
|
|
BLAKE2b-256 checksum How to use checksums |
ddeac0e0850c44f5d0f0c94796882589d8aec92aee60059793af66d71c587cd2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.
Transparency logRelease files / types_array_api-1.3.0-py3-none-any.whl
| Download URL | types_array_api-1.3.0-py3-none-any.whl |
|---|---|
| Size | 476.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8cddba53e105f045f888f16c838f46df1296136423e4ae871ce9ab26bee275b1
|
|
BLAKE2b-256 checksum How to use checksums |
9108409256f4b35e5c06b1577270d27a31a4b0134851ca479aa9060c095e3e12
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 28, 2026.
Transparency log