Skip to content

Commit c6b28ad

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

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/array_api_typing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"HasArrayNamespace",
66
"HasDType",
77
"HasMatrixTranspose",
8+
"HasNDim",
89
"__version__",
910
"__version_tuple__",
1011
)
@@ -14,5 +15,6 @@
1415
HasArrayNamespace,
1516
HasDType,
1617
HasMatrixTranspose,
18+
HasNDim,
1719
)
1820
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_array.py

Lines changed: 16 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,11 +101,26 @@ 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],
106121
HasDevice,
107122
HasMatrixTranspose,
123+
HasNDim,
108124
# ------- Methods ---------
109125
HasArrayNamespace[NamespaceT_co],
110126
# -------------------------

tests/integration/test_numpy1p0.pyi

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

tests/integration/test_numpy2p0.pyi

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

0 commit comments

Comments
 (0)