11__all__ = (
22 "Array" ,
33 "HasArrayNamespace" ,
4- "HasDType" ,
5- "HasDevice" ,
6- "HasMatrixTranspose" ,
7- "HasNDim" ,
8- "HasShape" ,
9- "HasSize" ,
10- "HasTranspose" ,
114)
125
136from types import ModuleType
1710NamespaceT_co = TypeVar ("NamespaceT_co" , covariant = True , default = ModuleType )
1811DTypeT_co = TypeVar ("DTypeT_co" , covariant = True )
1912DeviceT_co = TypeVar ("DeviceT_co" , covariant = True , default = object )
13+ KeyT_contra = TypeVar ("KeyT_contra" , contravariant = True , default = object )
14+ ValueT_contra = TypeVar ("ValueT_contra" , contravariant = True , default = object )
2015
2116
2217class HasArrayNamespace (Protocol [NamespaceT_co ]):
@@ -66,6 +61,26 @@ def __array_namespace__(
6661 ...
6762
6863
64+ class HasDLPack (Protocol ):
65+ """Protocol for array classes that support DLPack export."""
66+
67+ def __dlpack__ (
68+ self ,
69+ / ,
70+ * ,
71+ stream : object | None = None ,
72+ max_version : tuple [int , int ] | None = None ,
73+ dl_device : tuple [int , int ] | None = None ,
74+ copy : bool | None = None ,
75+ ) -> object :
76+ """Export the array as a DLPack capsule."""
77+ ...
78+
79+ def __dlpack_device__ (self , / ) -> tuple [int , int ]:
80+ """Return the DLPack device type and device ID."""
81+ ...
82+
83+
6984class HasDType (Protocol [DTypeT_co ]):
7085 """Protocol for array classes that have a data type attribute."""
7186
@@ -84,6 +99,14 @@ def device(self) -> DeviceT_co:
8499 ...
85100
86101
102+ class HasGetItem (Protocol [KeyT_contra ]):
103+ """Protocol for array classes that support indexing."""
104+
105+ def __getitem__ (self , key : KeyT_contra , / ) -> Self :
106+ """Return ``self[key]``."""
107+ ...
108+
109+
87110class HasMatrixTranspose (Protocol ):
88111 """Protocol for array classes that have a matrix transpose attribute."""
89112
@@ -140,6 +163,14 @@ def shape(self) -> tuple[int | None, ...]:
140163 ...
141164
142165
166+ class HasSetItem (Protocol [KeyT_contra , ValueT_contra ]):
167+ """Protocol for mutable array classes that support indexed assignment."""
168+
169+ def __setitem__ (self , key : KeyT_contra , value : ValueT_contra , / ) -> None :
170+ """Set ``self[key]`` to ``value``."""
171+ ...
172+
173+
143174class HasSize (Protocol ):
144175 """Protocol for array classes that have a size attribute."""
145176
@@ -159,6 +190,14 @@ def size(self) -> int | None:
159190 ...
160191
161192
193+ class HasToDevice (Protocol ):
194+ """Protocol for array classes that support device transfer."""
195+
196+ def to_device (self , device : object , / , * , stream : object | None = None ) -> Self :
197+ """Copy the array to the specified device."""
198+ ...
199+
200+
162201class HasTranspose (Protocol ):
163202 """Protocol for array classes that support the transpose operation."""
164203
@@ -191,16 +230,26 @@ def T(self) -> Self: # noqa: N802
191230
192231class Array (
193232 # ------ Attributes -------
233+ HasDevice [DeviceT_co ],
194234 HasDType [DTypeT_co ],
235+ HasMatrixTranspose ,
236+ HasNDim ,
237+ HasShape ,
238+ HasSize ,
239+ HasTranspose ,
195240 # ------- Methods ---------
196241 HasArrayNamespace [NamespaceT_co ],
242+ HasDLPack ,
243+ HasGetItem ,
244+ HasSetItem ,
245+ HasToDevice ,
197246 # -------------------------
198- Protocol [DTypeT_co , NamespaceT_co ],
247+ Protocol [DTypeT_co , NamespaceT_co , DeviceT_co ],
199248):
200249 """Array API specification for array object attributes and methods.
201250
202- The type is: ``Array[+DTypeT, +NamespaceT = ModuleType] = Array[DTypeT,
203- NamespaceT]`` where:
251+ The type is: ``Array[+DTypeT, +NamespaceT = ModuleType, +DeviceT = object] =
252+ Array[DTypeT, NamespaceT, DeviceT ]`` where:
204253
205254 - `DTypeT` is the data type of the array elements.
206255 - `NamespaceT` is the type of the array namespace. It defaults to
@@ -209,6 +258,7 @@ class Array(
209258 `types.SimpleNamespace`, to allow for wrapper libraries to
210259 semi-dynamically define their own array namespaces based on the wrapped
211260 array type.
261+ - `DeviceT` is the type of the hardware device the array data resides on.
212262
213263 This type is intended for use in static typing to ensure that an object has
214264 the attributes and methods defined in the array API specification. It should
0 commit comments