Skip to main content

Typing for array API and array-api-compat

Project description

Python array API standard typing

CI Status Documentation Status Test coverage percentage

uv Ruff pre-commit

PyPI Version Supported Python versions License


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)

Screenshot 1 Screenshot 2

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 (ndarrayndarray, TensorTensor), a TypeVar bound to Array can 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, ShapedArray and ShapedAnyArray can 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 ShapedAnyArray exists only for documentation purposes and internally it is treated as Array. 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

Copier

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

types_array_api-1.2.0.tar.gz (371.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

types_array_api-1.2.0-py3-none-any.whl (374.7 kB view details)

Uploaded Python 3

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

Hashes for types_array_api-1.2.0.tar.gz
Algorithm Hash digest
SHA256 804752410da92672f7e28cd80aed291bf2a2b05d7acbb99bec8f4ac23621b70a
MD5 f7eff170b6a3f6ec026cb58d13168d96
BLAKE2b-256 b27d70ea27edb442de8ee65a44f3732e92a8ba2de7c01aa83d74b51e5bed07eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for types_array_api-1.2.0.tar.gz:

Publisher: ci.yml on 34j/types-array-api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for types_array_api-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c2500bf823ea010f78d3cf83874e8047604c61da99a7c274853394230549ca7
MD5 202cfad0e5c406b49e1e5c1fb33c31c6
BLAKE2b-256 a1abf8d707bb2bc9bca6d44a07a3215399d9e2aa163c5f84d21da2890759c857

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page