Skip to content

Commit ad6cae9

Browse files
committed
feat: HasShape
1 parent 18f6bd2 commit ad6cae9

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
@@ -90,6 +90,27 @@ def ndim(self) -> int:
9090
...
9191

9292

93+
class HasShape(Protocol):
94+
"""Protocol for array classes that have a shape attribute."""
95+
96+
@property
97+
def shape(self) -> tuple[int | None, ...]:
98+
"""Shape of the array.
99+
100+
Returns:
101+
tuple[int | None, ...]: array dimensions. An array dimension must be None
102+
if and only if a dimension is unknown.
103+
104+
Notes:
105+
For array libraries having graph-based computational models, array
106+
dimensions may be unknown due to data-dependent operations (e.g.,
107+
boolean indexing; `A[:, B > 0]`) and thus cannot be statically
108+
resolved without knowing array contents.
109+
110+
"""
111+
...
112+
113+
93114
# ============================================================================
94115

95116

@@ -541,6 +562,7 @@ class Array(
541562
HasDevice,
542563
HasMatrixTranspose,
543564
HasNDim,
565+
HasShape,
544566
# ------ Methods -------
545567
HasArrayNamespace[NS_co],
546568
CanArrayPos,

0 commit comments

Comments
 (0)