Skip to content

Commit d413d21

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

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ assert_type(x_i32.ndim, int)
7474
# Check Attribute `.shape`
7575
assert_type(x_f32.shape, tuple[int | None, ...])
7676
assert_type(x_i32.shape, tuple[int | None, ...])
77+
78+
# Check Attribute `.size`
79+
assert_type(x_f32.size, int | None)
80+
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
@@ -84,3 +84,8 @@ assert_type(x_b.ndim, int)
8484
assert_type(x_f32.shape, tuple[int | None, ...])
8585
assert_type(x_i32.shape, tuple[int | None, ...])
8686
assert_type(x_b.shape, tuple[int | None, ...])
87+
88+
# Check Attribute `.size`
89+
assert_type(x_f32.size, int | None)
90+
assert_type(x_i32.size, int | None)
91+
assert_type(x_b.size, int | None)

0 commit comments

Comments
 (0)