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.3.0.tar.gz (472.1 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.3.0-py3-none-any.whl (476.9 kB view details)

Uploaded Python 3

File details

Details for the file types_array_api-1.3.0.tar.gz.

File metadata

  • Download URL: types_array_api-1.3.0.tar.gz
  • Upload date:
  • Size: 472.1 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.3.0.tar.gz
Algorithm Hash digest
SHA256 918526e110c1008618cd9b4487f98aa518f3869efc67b0da2491ceff1b53efcd
MD5 b11bd2c4d0e38ffc5670871c5ad9170b
BLAKE2b-256 ddeac0e0850c44f5d0f0c94796882589d8aec92aee60059793af66d71c587cd2

See more details on using hashes here.

Provenance

The following attestation bundles were made for types_array_api-1.3.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.3.0-py3-none-any.whl.

File metadata

  • Download URL: types_array_api-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 476.9 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8cddba53e105f045f888f16c838f46df1296136423e4ae871ce9ab26bee275b1
MD5 b8fe4c15d9539fc7ab1e2fedaf76ce86
BLAKE2b-256 9108409256f4b35e5c06b1577270d27a31a4b0134851ca479aa9060c095e3e12

See more details on using hashes here.

Provenance

The following attestation bundles were made for types_array_api-1.3.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