|
| 1 | +__all__ = ("HasArrayNamespace",) |
| 2 | + |
| 3 | +from types import ModuleType |
| 4 | +from typing import Literal, Protocol |
| 5 | +from typing_extensions import TypeVar |
| 6 | + |
| 7 | +NamespaceT_co = TypeVar("NamespaceT_co", covariant=True, default=ModuleType) |
| 8 | + |
| 9 | + |
| 10 | +class HasArrayNamespace(Protocol[NamespaceT_co]): |
| 11 | + """Protocol for classes that have an `__array_namespace__` method. |
| 12 | +
|
| 13 | + This `Protocol` is intended for use in static typing to ensure that an |
| 14 | + object has an `__array_namespace__` method that returns a namespace for |
| 15 | + array operations. This `Protocol` should not be used at runtime, for type |
| 16 | + checking or as a base class. |
| 17 | +
|
| 18 | + Example: |
| 19 | + >>> import array_api_typing as xpt |
| 20 | + >>> |
| 21 | + >>> class MyArray: |
| 22 | + ... def __array_namespace__(self): |
| 23 | + ... return object() |
| 24 | + >>> |
| 25 | + >>> x = MyArray() |
| 26 | + >>> def has_array_namespace(x: xpt.HasArrayNamespace) -> bool: |
| 27 | + ... return hasattr(x, "__array_namespace__") |
| 28 | + >>> has_array_namespace(x) |
| 29 | + True |
| 30 | +
|
| 31 | + """ |
| 32 | + |
| 33 | + def __array_namespace__( |
| 34 | + self, /, *, api_version: Literal["2021.12"] | None = None |
| 35 | + ) -> NamespaceT_co: ... |
| 36 | + """Returns an object that has all the array API functions on it. |
| 37 | +
|
| 38 | + Args: |
| 39 | + api_version: string representing the version of the array API |
| 40 | + specification to be returned, in 'YYYY.MM' form, for example, |
| 41 | + '2020.10'. If it is `None`, it should return the namespace |
| 42 | + corresponding to latest version of the array API specification. |
| 43 | + If the given version is invalid or not implemented for the given |
| 44 | + module, an error should be raised. Default: `None`. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + NamespaceT_co: An object representing the array API namespace. It |
| 48 | + should have every top-level function defined in the |
| 49 | + specification as an attribute. It may contain other public names |
| 50 | + as well, but it is recommended to only include those names that |
| 51 | + are part of the specification. |
| 52 | +
|
| 53 | + """ |
| 54 | + ... |
0 commit comments