Skip to content

Commit 77958e1

Browse files
committed
✨ HasSize
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
1 parent 7ef429c commit 77958e1

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,33 @@ def shape(self) -> tuple[int | None, ...]:
132132
...
133133

134134

135+
class HasSize(Protocol):
136+
"""Protocol for array classes that have a size attribute."""
137+
138+
@property
139+
def size(self) -> int | None:
140+
"""Number of elements in an array.
141+
142+
Returns:
143+
int | None: number of elements in an array. The returned value must
144+
be `None` if and only if one or more array dimensions are
145+
unknown.
146+
147+
Notes:
148+
This must equal the product of the array's dimensions.
149+
150+
"""
151+
...
152+
153+
135154
class Array(
136155
# ------ Attributes -------
137156
HasDType[DTypeT_co],
138157
HasDevice,
139158
HasMatrixTranspose,
140159
HasNDim,
141160
HasShape,
161+
HasSize,
142162
# ------- Methods ---------
143163
HasArrayNamespace[NamespaceT_co],
144164
# -------------------------

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)