Skip to content

Commit 8b623fe

Browse files
committed
✨ HasNDim
Signed-off-by: nstarman <[email protected]>
1 parent 15e2c2b commit 8b623fe

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/array_api_typing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"HasDType",
77
"HasDevice",
88
"HasMatrixTranspose",
9+
"HasNDim",
910
"__version__",
1011
"__version_tuple__",
1112
)
@@ -16,5 +17,6 @@
1617
HasDevice,
1718
HasDType,
1819
HasMatrixTranspose,
20+
HasNDim,
1921
)
2022
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_array.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"HasDType",
55
"HasDevice",
66
"HasMatrixTranspose",
7+
"HasNDim",
78
)
89

910
from types import ModuleType
@@ -100,6 +101,20 @@ def mT(self) -> Self: # noqa: N802
100101
...
101102

102103

104+
class HasNDim(Protocol):
105+
"""Protocol for array classes that have a number of dimensions attribute."""
106+
107+
@property
108+
def ndim(self) -> int:
109+
"""Number of array dimensions (axes).
110+
111+
Returns:
112+
int: number of array dimensions (axes).
113+
114+
"""
115+
...
116+
117+
103118
class Array(
104119
# ------ Attributes -------
105120
HasDType[DTypeT_co],

tests/integration/test_numpy1p0.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ _: xpt.HasMatrixTranspose = nparr
5353
_: xpt.HasMatrixTranspose = nparr_i32
5454
_: xpt.HasMatrixTranspose = nparr_f32
5555

56+
# =========================================================
57+
# `xpt.HasNDim`
58+
59+
_: xpt.HasNDim = nparr
60+
_: xpt.HasNDim = nparr_i32
61+
_: xpt.HasNDim = nparr_f32
62+
5663
# =========================================================
5764
# `xpt.Array`
5865

tests/integration/test_numpy2p0.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ _: xpt.HasMatrixTranspose = nparr_i32
6161
_: xpt.HasMatrixTranspose = nparr_f32
6262
_: xpt.HasMatrixTranspose = nparr_b
6363

64+
# =========================================================
65+
# `xpt.HasNDim`
66+
67+
_: xpt.HasNDim = nparr
68+
_: xpt.HasNDim = nparr_i32
69+
_: xpt.HasNDim = nparr_f32
70+
_: xpt.HasNDim = nparr_b
71+
6472
# =========================================================
6573
# `xpt.Array`
6674

0 commit comments

Comments
 (0)