Skip to content

Commit 0eba222

Browse files
committed
✨ HasSize
Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent e49a73a commit 0eba222

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
@@ -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: 21 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,13 +138,33 @@ 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],
143163
HasDevice,
144164
HasMatrixTranspose,
145165
HasNDim,
146166
HasShape,
167+
HasSize,
147168
# ------- Methods ---------
148169
HasArrayNamespace[NamespaceT_co],
149170
# -------------------------

tests/integration/test_numpy1p0.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,7 @@ _: int = x_i32.ndim
7171
# Check Attribute `.shape`
7272
_: tuple[int | None, ...] = x_f32.shape
7373
_: tuple[int | None, ...] = x_i32.shape
74+
75+
# Check Attribute `.size`
76+
_: int | None = x_f32.size
77+
_: int | None = x_i32.size

tests/integration/test_numpy2p0.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,8 @@ _: int = x_b.ndim
8181
_: tuple[int | None, ...] = x_f32.shape
8282
_: tuple[int | None, ...] = x_i32.shape
8383
_: tuple[int | None, ...] = x_b.shape
84+
85+
# Check Attribute `.size`
86+
_: int | None = x_f32.size
87+
_: int | None = x_i32.size
88+
_: int | None = x_b.size

0 commit comments

Comments
 (0)