|
8 | 8 | from typing_extensions import TypeVar |
9 | 9 |
|
10 | 10 | NamespaceT_co = TypeVar("NamespaceT_co", covariant=True, default=ModuleType) |
| 11 | +DTypeT_co = TypeVar("DTypeT_co", covariant=True) |
11 | 12 |
|
12 | 13 |
|
13 | 14 | class HasArrayNamespace(Protocol[NamespaceT_co]): |
@@ -38,8 +39,32 @@ def __array_namespace__( |
38 | 39 | ) -> NamespaceT_co: ... |
39 | 40 |
|
40 | 41 |
|
| 42 | +class HasDType(Protocol[DTypeT_co]): |
| 43 | + """Protocol for array classes that have a data type attribute.""" |
| 44 | + |
| 45 | + @property |
| 46 | + def dtype(self, /) -> DTypeT_co: |
| 47 | + """Data type of the array elements.""" |
| 48 | + ... |
| 49 | + |
| 50 | + |
41 | 51 | class Array( |
42 | | - HasArrayNamespace[NamespaceT_co], |
43 | | - Protocol[NamespaceT_co], |
| 52 | + # ------ Attributes ------- |
| 53 | + HasDType[DTypeT_co], |
| 54 | + # ------------------------- |
| 55 | + Protocol[DTypeT_co, NamespaceT_co], |
44 | 56 | ): |
45 | | - """Array API specification for array object attributes and methods.""" |
| 57 | + """Array API specification for array object attributes and methods. |
| 58 | +
|
| 59 | + The type is: ``Array[+DTypeT, +NamespaceT = ModuleType] = Array[DTypeT, |
| 60 | + NamespaceT]`` where: |
| 61 | +
|
| 62 | + - `DTypeT` is the data type of the array elements. |
| 63 | + - `NamespaceT` is the type of the array namespace. It defaults to |
| 64 | + `ModuleType`, which is the most common form of array namespace (e.g., |
| 65 | + `numpy`, `cupy`, etc.). However, it can be any type, e.g. a |
| 66 | + `types.SimpleNamespace`, to allow for wrapper libraries to |
| 67 | + semi-dynamically define their own array namespaces based on the wrapped |
| 68 | + array type. |
| 69 | +
|
| 70 | + """ |
0 commit comments