|
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]): |
@@ -57,8 +58,32 @@ def __array_namespace__( |
57 | 58 | ... |
58 | 59 |
|
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 | + |
60 | 70 | class Array( |
61 | | - HasArrayNamespace[NamespaceT_co], |
62 | | - Protocol[NamespaceT_co], |
| 71 | + # ------ Attributes ------- |
| 72 | + HasDType[DTypeT_co], |
| 73 | + # ------------------------- |
| 74 | + Protocol[DTypeT_co, NamespaceT_co], |
63 | 75 | ): |
64 | | - """Array API specification for array object attributes and methods.""" |
| 76 | + """Array API specification for array object attributes and methods. |
| 77 | +
|
| 78 | + The type is: ``Array[+DTypeT, +NamespaceT = ModuleType] = Array[DTypeT, |
| 79 | + NamespaceT]`` where: |
| 80 | +
|
| 81 | + - `DTypeT` is the data type of the array elements. |
| 82 | + - `NamespaceT` is the type of the array namespace. It defaults to |
| 83 | + `ModuleType`, which is the most common form of array namespace (e.g., |
| 84 | + `numpy`, `cupy`, etc.). However, it can be any type, e.g. a |
| 85 | + `types.SimpleNamespace`, to allow for wrapper libraries to |
| 86 | + semi-dynamically define their own array namespaces based on the wrapped |
| 87 | + array type. |
| 88 | +
|
| 89 | + """ |
0 commit comments