Skip to content

Commit e32764f

Browse files
committed
✨ HasShape
1 parent 08a7e46 commit e32764f

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
@@ -111,12 +111,34 @@ def ndim(self) -> int:
111111
...
112112

113113

114+
class HasShape(Protocol):
115+
"""Protocol for array classes that have a shape attribute."""
116+
117+
@property
118+
def shape(self) -> tuple[int | None, ...]:
119+
"""Shape of the array.
120+
121+
Returns:
122+
tuple[int | None, ...]: array dimensions. An array dimension must be None
123+
if and only if a dimension is unknown.
124+
125+
Notes:
126+
For array libraries having graph-based computational models, array
127+
dimensions may be unknown due to data-dependent operations (e.g.,
128+
boolean indexing; `A[:, B > 0]`) and thus cannot be statically
129+
resolved without knowing array contents.
130+
131+
"""
132+
...
133+
134+
114135
class Array(
115136
# ------ Attributes -------
116137
HasDType[DTypeT_co],
117138
HasDevice,
118139
HasMatrixTranspose,
119140
HasNDim,
141+
HasShape,
120142
# ------- Methods ---------
121143
HasArrayNamespace[NamespaceT_co],
122144
# -------------------------

0 commit comments

Comments
 (0)