Skip to content

Commit c65c5d5

Browse files
committed
✨ HasTranspose
Signed-off-by: nstarman <[email protected]>
1 parent 697883d commit c65c5d5

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-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
"HasNDim",
99
"HasShape",
1010
"HasSize",
11+
"HasTranspose",
1112
"__version__",
1213
"__version_tuple__",
1314
)
@@ -20,5 +21,6 @@
2021
HasNDim,
2122
HasShape,
2223
HasSize,
24+
HasTranspose,
2325
)
2426
from ._version import version as __version__, version_tuple as __version_tuple__

src/array_api_typing/_array.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"HasNDim",
88
"HasShape",
99
"HasSize",
10+
"HasTranspose",
1011
)
1112

1213
from types import ModuleType
@@ -157,6 +158,36 @@ def size(self) -> int | None:
157158
...
158159

159160

161+
class HasTranspose(Protocol):
162+
"""Protocol for array classes that support the transpose operation."""
163+
164+
@property
165+
def T(self) -> Self: # noqa: N802
166+
"""Transpose of the array.
167+
168+
The array instance must be two-dimensional. If the array instance is not
169+
two-dimensional, an error should be raised.
170+
171+
Returns:
172+
Self: two-dimensional array whose first and last dimensions (axes)
173+
are permuted in reverse order relative to original array. The
174+
returned array must have the same data type as the original
175+
array.
176+
177+
Notes:
178+
Limiting the transpose to two-dimensional arrays (matrices) deviates
179+
from the NumPy et al practice of reversing all axes for arrays
180+
having more than two-dimensions. This is intentional, as reversing
181+
all axes was found to be problematic (e.g., conflicting with the
182+
mathematical definition of a transpose which is limited to matrices;
183+
not operating on batches of matrices; et cetera). In order to
184+
reverse all axes, one is recommended to use the functional
185+
`PermuteDims` interface found in this specification.
186+
187+
"""
188+
...
189+
190+
160191
class Array(
161192
# ------ Attributes -------
162193
HasDType[DTypeT_co],
@@ -165,6 +196,7 @@ class Array(
165196
HasNDim,
166197
HasShape,
167198
HasSize,
199+
HasTranspose,
168200
# ------- Methods ---------
169201
HasArrayNamespace[NamespaceT_co],
170202
# -------------------------

tests/integration/test_numpy1p0.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ assert_type(x_i32.shape, tuple[int | None, ...])
7575
# Check Attribute `.size`
7676
assert_type(x_f32.size, int | None)
7777
assert_type(x_i32.size, int | None)
78+
79+
# Check Attribute `.T`
80+
assert_type(x_f32.T, xpt.Array[dtype[Any]])
81+
assert_type(x_i32.T, xpt.Array[dtype[Any]])

tests/integration/test_numpy2p0.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ assert_type(x_b.shape, tuple[int | None, ...])
8686
assert_type(x_f32.size, int | None)
8787
assert_type(x_i32.size, int | None)
8888
assert_type(x_b.size, int | None)
89+
90+
# Check Attribute `.T`
91+
assert_type(x_f32.T, xpt.Array[np.dtype[F32]])
92+
assert_type(x_i32.T, xpt.Array[np.dtype[I32]])
93+
assert_type(x_b.T, xpt.Array[np.dtype[B]])

0 commit comments

Comments
 (0)