Skip to content

Commit f9e108f

Browse files
committed
✨ HasNDim
Signed-off-by: nstarman <[email protected]>
1 parent 199e9a3 commit f9e108f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/array_api_typing/_array.py

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

9999

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

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)