Skip to content

Commit 629638d

Browse files
committed
✨ HasSize
Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent 28fa737 commit 629638d

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/array_api_typing/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"HasMatrixTranspose",
99
"HasNDim",
1010
"HasShape",
11+
"HasSize",
1112
"__version__",
1213
"__version_tuple__",
1314
)
@@ -20,5 +21,6 @@
2021
HasMatrixTranspose,
2122
HasNDim,
2223
HasShape,
24+
HasSize,
2325
)
2426
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_array.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"HasMatrixTranspose",
77
"HasNDim",
88
"HasShape",
9+
"HasSize",
910
)
1011

1112
from types import ModuleType
@@ -137,6 +138,25 @@ def shape(self) -> tuple[int | None, ...]:
137138
...
138139

139140

141+
class HasSize(Protocol):
142+
"""Protocol for array classes that have a size attribute."""
143+
144+
@property
145+
def size(self) -> int | None:
146+
"""Number of elements in an array.
147+
148+
Returns:
149+
int | None: number of elements in an array. The returned value must
150+
be `None` if and only if one or more array dimensions are
151+
unknown.
152+
153+
Notes:
154+
This must equal the product of the array's dimensions.
155+
156+
"""
157+
...
158+
159+
140160
class Array(
141161
# ------ Attributes -------
142162
HasDType[DTypeT_co],

tests/integration/test_numpy1p0.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ _: xpt.HasShape = nparr
6767
_: xpt.HasShape = nparr_i32
6868
_: xpt.HasShape = nparr_f32
6969

70+
# =========================================================
71+
# `xpt.HasShape`
72+
73+
_: xpt.HasShape = nparr
74+
_: xpt.HasShape = nparr_i32
75+
_: xpt.HasShape = nparr_f32
76+
7077
# =========================================================
7178
# `xpt.Array`
7279

tests/integration/test_numpy2p0.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ _: xpt.HasShape = nparr_i32
7777
_: xpt.HasShape = nparr_f32
7878
_: xpt.HasShape = nparr_b
7979

80+
# =========================================================
81+
# `xpt.HasSize`
82+
83+
_: xpt.HasSize = nparr
84+
_: xpt.HasSize = nparr_i32
85+
_: xpt.HasSize = nparr_f32
86+
_: xpt.HasSize = nparr_b
87+
8088
# =========================================================
8189
# `xpt.Array`
8290

0 commit comments

Comments
 (0)