Typing for array API and array-api-compat
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file types_array_api-1.2.0.tar.gz.
File metadata
- Download URL: types_array_api-1.2.0.tar.gz
- Upload date:
- Size: 371.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
804752410da92672f7e28cd80aed291bf2a2b05d7acbb99bec8f4ac23621b70a
|
|
| MD5 |
f7eff170b6a3f6ec026cb58d13168d96
|
|
| BLAKE2b-256 |
b27d70ea27edb442de8ee65a44f3732e92a8ba2de7c01aa83d74b51e5bed07eb
|
Provenance
The following attestation bundles were made for types_array_api-1.2.0.tar.gz:
Publisher:
ci.yml on 34j/types-array-api
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
types_array_api-1.2.0.tar.gz -
Subject digest:
804752410da92672f7e28cd80aed291bf2a2b05d7acbb99bec8f4ac23621b70a - Sigstore transparency entry: 759995871
- Sigstore integration time:
-
Permalink:
34j/types-array-api@2a0f531ea3700974d854b7ab28ca6b76d7543404 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/34j
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a0f531ea3700974d854b7ab28ca6b76d7543404 -
Trigger Event:
push
-
Statement type:
File details
Details for the file types_array_api-1.2.0-py3-none-any.whl.
File metadata
- Download URL: types_array_api-1.2.0-py3-none-any.whl
- Upload date:
- Size: 374.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c2500bf823ea010f78d3cf83874e8047604c61da99a7c274853394230549ca7
|
|
| MD5 |
202cfad0e5c406b49e1e5c1fb33c31c6
|
|
| BLAKE2b-256 |
a1abf8d707bb2bc9bca6d44a07a3215399d9e2aa163c5f84d21da2890759c857
|
Provenance
The following attestation bundles were made for types_array_api-1.2.0-py3-none-any.whl:
Publisher:
ci.yml on 34j/types-array-api
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
types_array_api-1.2.0-py3-none-any.whl -
Subject digest:
8c2500bf823ea010f78d3cf83874e8047604c61da99a7c274853394230549ca7 - Sigstore transparency entry: 759995874
- Sigstore integration time:
-
Permalink:
34j/types-array-api@2a0f531ea3700974d854b7ab28ca6b76d7543404 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/34j
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@2a0f531ea3700974d854b7ab28ca6b76d7543404 -
Trigger Event:
push
-
Statement type: