Skip to content

Commit 591853b

Browse files
committed
✨ HasDevice
Signed-off-by: nstarman <[email protected]>
1 parent 4de00ae commit 591853b

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ ignore = [
126126
"FIX", # flake8-fixme
127127
"ISC001", # Conflicts with formatter
128128
"PYI041", # Use `float` instead of `int | float`
129+
"TD002", # Missing author in TODO
130+
"TD003", # Missing issue link for this TODO
129131
]
130132

131133
[tool.ruff.lint.pydocstyle]

src/array_api_typing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"Array",
55
"HasArrayNamespace",
66
"HasDType",
7+
"HasDevice",
78
"__version__",
89
"__version_tuple__",
910
)
1011

1112
from ._array import (
1213
Array,
1314
HasArrayNamespace,
15+
HasDevice,
1416
HasDType,
1517
)
1618
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_array.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"Array",
33
"HasArrayNamespace",
44
"HasDType",
5+
"HasDevice",
56
)
67

78
from types import ModuleType
@@ -68,9 +69,19 @@ def dtype(self, /) -> DTypeT_co:
6869
...
6970

7071

72+
class HasDevice(Protocol):
73+
"""Protocol for array classes that have a device attribute."""
74+
75+
@property
76+
def device(self) -> object: # TODO: more specific type
77+
"""Hardware device the array data resides on."""
78+
...
79+
80+
7181
class Array(
7282
# ------ Attributes -------
7383
HasDType[DTypeT_co],
84+
HasDevice,
7485
# ------- Methods ---------
7586
HasArrayNamespace[NamespaceT_co],
7687
# -------------------------

tests/integration/test_numpy1p0.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ x_i32: xpt.Array[dtype[Any]] = nparr_i32
5555
# Check Attribute `.dtype`
5656
_: dtype[Any] = x_f32.dtype
5757
_: dtype[Any] = x_i32.dtype
58+
59+
# Check Attribute `.device`
60+
_: object = x_f32.device
61+
_: object = x_i32.device

tests/integration/test_numpy2p0.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@ x_b: xpt.Array[np.dtype[B]] = nparr_b
6161
_: np.dtype[F32] = x_f32.dtype
6262
_: np.dtype[I32] = x_i32.dtype
6363
_: np.dtype[B] = x_b.dtype
64+
65+
# Check Attribute `.device`
66+
_: object = x_f32.device
67+
_: object = x_i32.device
68+
_: object = x_b.device

0 commit comments

Comments
 (0)