Skip to content

Commit 697883d

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

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
@@ -7,6 +7,7 @@
77
"HasMatrixTranspose",
88
"HasNDim",
99
"HasShape",
10+
"HasSize",
1011
"__version__",
1112
"__version_tuple__",
1213
)
@@ -18,5 +19,6 @@
1819
HasMatrixTranspose,
1920
HasNDim,
2021
HasShape,
22+
HasSize,
2123
)
2224
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 @@ assert_type(x_i32.ndim, int)
7171
# Check Attribute `.shape`
7272
assert_type(x_f32.shape, tuple[int | None, ...])
7373
assert_type(x_i32.shape, tuple[int | None, ...])
74+
75+
# Check Attribute `.size`
76+
assert_type(x_f32.size, int | None)
77+
assert_type(x_i32.size, int | None)

tests/integration/test_numpy2p0.pyi

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

0 commit comments

Comments
 (0)