Skip to content

Commit ac4e177

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

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/array_api_typing/_array.py

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

135135

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

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)