Skip to content

Commit 1aee6e0

Browse files
committed
feat: HasShape
1 parent 2d8ec48 commit 1aee6e0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/array_api_typing/_array.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,35 @@ def ndim(self) -> int:
106106
...
107107

108108

109+
class HasShape(Protocol):
110+
"""Protocol for array classes that have a shape attribute."""
111+
112+
@property
113+
def shape(self) -> tuple[int | None, ...]:
114+
"""Shape of the array.
115+
116+
Returns:
117+
tuple[int | None, ...]: array dimensions. An array dimension must be None
118+
if and only if a dimension is unknown.
119+
120+
Notes:
121+
For array libraries having graph-based computational models, array
122+
dimensions may be unknown due to data-dependent operations (e.g.,
123+
boolean indexing; `A[:, B > 0]`) and thus cannot be statically
124+
resolved without knowing array contents.
125+
126+
"""
127+
...
128+
129+
109130
@docstring_setter(**_array_docstrings)
110131
class Array(
111132
# ------ Attributes -------
112133
HasDType[DType_co],
113134
HasDevice,
114135
HasMatrixTranspose,
115136
HasNDim,
137+
HasShape,
116138
# ------ Methods -------
117139
HasArrayNamespace[NS_co],
118140
op.CanPosSelf,

0 commit comments

Comments
 (0)