|
| 1 | +__all__ = ( |
| 2 | + "Array", |
| 3 | + "HasArrayNamespace", |
| 4 | +) |
| 5 | + |
| 6 | +from types import ModuleType |
| 7 | +from typing import Literal, Protocol |
| 8 | +from typing_extensions import TypeVar |
| 9 | + |
| 10 | +NamespaceT_co = TypeVar("NamespaceT_co", covariant=True, default=ModuleType) |
| 11 | +DTypeT_co = TypeVar("DTypeT_co", covariant=True) |
| 12 | + |
| 13 | + |
| 14 | +class HasArrayNamespace(Protocol[NamespaceT_co]): |
| 15 | + """Protocol for classes that have an `__array_namespace__` method. |
| 16 | +
|
| 17 | + This `Protocol` is intended for use in static typing to ensure that an |
| 18 | + object has an `__array_namespace__` method that returns a namespace for |
| 19 | + array operations. This `Protocol` should not be used at runtime for type |
| 20 | + checking or as a base class. |
| 21 | +
|
| 22 | + Example: |
| 23 | + >>> import array_api_typing as xpt |
| 24 | + >>> |
| 25 | + >>> class MyArray: |
| 26 | + ... def __array_namespace__(self): |
| 27 | + ... return object() |
| 28 | + >>> |
| 29 | + >>> x = MyArray() |
| 30 | + >>> def has_array_namespace(x: xpt.HasArrayNamespace) -> bool: |
| 31 | + ... return hasattr(x, "__array_namespace__") |
| 32 | + >>> has_array_namespace(x) |
| 33 | + True |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + def __array_namespace__( |
| 38 | + self, /, *, api_version: Literal["2021.12"] | None = None |
| 39 | + ) -> NamespaceT_co: |
| 40 | + """Returns an object that has all the array API functions on it. |
| 41 | +
|
| 42 | + Args: |
| 43 | + api_version: string representing the version of the array API |
| 44 | + specification to be returned, in 'YYYY.MM' form, for example, |
| 45 | + '2020.10'. If it is `None`, it should return the namespace |
| 46 | + corresponding to latest version of the array API specification. |
| 47 | + If the given version is invalid or not implemented for the given |
| 48 | + module, an error should be raised. Default: `None`. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + NamespaceT_co: An object representing the array API namespace. It |
| 52 | + should have every top-level function defined in the |
| 53 | + specification as an attribute. It may contain other public names |
| 54 | + as well, but it is recommended to only include those names that |
| 55 | + are part of the specification. |
| 56 | +
|
| 57 | + """ |
| 58 | + ... |
| 59 | + |
| 60 | + |
| 61 | +class HasDType(Protocol[DTypeT_co]): |
| 62 | + """Protocol for array classes that have a data type attribute.""" |
| 63 | + |
| 64 | + @property |
| 65 | + def dtype(self, /) -> DTypeT_co: |
| 66 | + """Data type of the array elements.""" |
| 67 | + ... |
| 68 | + |
| 69 | + |
| 70 | +class Array( |
| 71 | + HasArrayNamespace[NamespaceT_co], |
| 72 | + # ------ Attributes ------- |
| 73 | + HasDType[DTypeT_co], |
| 74 | + # ------------------------- |
| 75 | + Protocol[DTypeT_co, NamespaceT_co], |
| 76 | +): |
| 77 | + """Array API specification for array object attributes and methods. |
| 78 | +
|
| 79 | + The type is: ``Array[+DTypeT, +NamespaceT = ModuleType] = Array[DTypeT, |
| 80 | + NamespaceT]`` where: |
| 81 | +
|
| 82 | + - `DTypeT` is the data type of the array elements. |
| 83 | + - `NamespaceT` is the type of the array namespace. It defaults to |
| 84 | + `ModuleType`, which is the most common form of array namespace (e.g., |
| 85 | + `numpy`, `cupy`, etc.). However, it can be any type, e.g. a |
| 86 | + `types.SimpleNamespace`, to allow for wrapper libraries to |
| 87 | + semi-dynamically define their own array namespaces based on the wrapped |
| 88 | + array type. |
| 89 | +
|
| 90 | + This type is intended for use in static typing to ensure that an object has |
| 91 | + the attributes and methods defined in the array API specification. It should |
| 92 | + not be used at runtime for type checking or as a base class. |
| 93 | +
|
| 94 | + """ |
0 commit comments