Skip to content

Commit 408ad5b

Browse files
committed
✨ HasNDim
Signed-off-by: nstarman <[email protected]>
1 parent 589fc89 commit 408ad5b

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,26 @@ def mT(self) -> Self: # noqa: N802
9898
...
9999

100100

101+
class HasNDim(Protocol):
102+
"""Protocol for array classes that have a number of dimensions attribute."""
103+
104+
@property
105+
def ndim(self) -> int:
106+
"""Number of array dimensions (axes).
107+
108+
Returns:
109+
int: number of array dimensions (axes).
110+
111+
"""
112+
...
113+
114+
101115
class Array(
102116
# ------ Attributes -------
103117
HasDType[DTypeT_co],
104118
HasDevice,
105119
HasMatrixTranspose,
120+
HasNDim,
106121
# ------- Methods ---------
107122
HasArrayNamespace[NamespaceT_co],
108123
# -------------------------

tests/integration/test_numpy1p0.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@ _: object = x_i32.device
6363
# Check Attribute `.mT`
6464
_: xpt.Array[dtype[Any]] = x_f32.mT
6565
_: xpt.Array[dtype[Any]] = x_i32.mT
66+
67+
# Check Attribute `.ndim`
68+
_: int = x_f32.ndim
69+
_: int = x_i32.ndim

tests/integration/test_numpy2p0.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ _: object = x_b.device
7171
_: xpt.Array[np.dtype[F32]] = x_f32.mT
7272
_: xpt.Array[np.dtype[I32]] = x_i32.mT
7373
_: xpt.Array[np.dtype[B]] = x_b.mT
74+
75+
# Check Attribute `.ndim`
76+
_: int = x_f32.ndim
77+
_: int = x_i32.ndim
78+
_: int = x_b.ndim

0 commit comments

Comments
 (0)