-
Notifications
You must be signed in to change notification settings - Fork 5
feat: HasX attributes #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nstarman
wants to merge
7
commits into
data-apis:main
Choose a base branch
from
nstarman:has_x_attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4de00ae
💄rearrange protocols in Array
nstarman 591853b
✨ HasDevice
nstarman b0b88cf
✨ HasMatrixTranspose
nstarman ed33843
✨ HasNDim
nstarman e49a73a
✨ HasShape
nstarman 0eba222
✨ HasSize
nstarman a5e3d5b
✨ HasTranspose
nstarman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
__all__ = ( | ||
"Array", | ||
"HasArrayNamespace", | ||
"HasDType", | ||
"HasDevice", | ||
"HasMatrixTranspose", | ||
"HasNDim", | ||
"HasShape", | ||
"HasSize", | ||
"HasTranspose", | ||
) | ||
|
||
from types import ModuleType | ||
from typing import Literal, Protocol | ||
from typing import Literal, Protocol, Self | ||
from typing_extensions import TypeVar | ||
|
||
NamespaceT_co = TypeVar("NamespaceT_co", covariant=True, default=ModuleType) | ||
|
@@ -67,10 +74,131 @@ def dtype(self, /) -> DTypeT_co: | |
... | ||
|
||
|
||
class HasDevice(Protocol): | ||
"""Protocol for array classes that have a device attribute.""" | ||
|
||
@property | ||
def device(self) -> object: # TODO: more specific type | ||
"""Hardware device the array data resides on.""" | ||
... | ||
|
||
|
||
class HasMatrixTranspose(Protocol): | ||
"""Protocol for array classes that have a matrix transpose attribute.""" | ||
|
||
@property | ||
def mT(self) -> Self: # noqa: N802 | ||
"""Transpose of a matrix (or a stack of matrices). | ||
|
||
If an array instance has fewer than two dimensions, an error should be | ||
raised. | ||
|
||
Returns: | ||
Self: array whose last two dimensions (axes) are permuted in reverse | ||
order relative to original array (i.e., for an array instance | ||
having shape `(..., M, N)`, the returned array must have shape | ||
`(..., N, M))`. The returned array must have the same data type | ||
as the original array. | ||
|
||
""" | ||
... | ||
|
||
|
||
class HasNDim(Protocol): | ||
"""Protocol for array classes that have a number of dimensions attribute.""" | ||
|
||
@property | ||
def ndim(self) -> int: | ||
"""Number of array dimensions (axes). | ||
|
||
Returns: | ||
int: number of array dimensions (axes). | ||
|
||
""" | ||
... | ||
|
||
|
||
class HasShape(Protocol): | ||
"""Protocol for array classes that have a shape attribute.""" | ||
|
||
@property | ||
def shape(self) -> tuple[int | None, ...]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think in a followup we should add a type parameter Array[..., UnknownDimsT=Never] Then we can define the alias types
|
||
"""Shape of the array. | ||
|
||
Returns: | ||
tuple[int | None, ...]: array dimensions. An array dimension must be None | ||
if and only if a dimension is unknown. | ||
|
||
Notes: | ||
For array libraries having graph-based computational models, array | ||
dimensions may be unknown due to data-dependent operations (e.g., | ||
boolean indexing; `A[:, B > 0]`) and thus cannot be statically | ||
resolved without knowing array contents. | ||
|
||
""" | ||
... | ||
|
||
|
||
class HasSize(Protocol): | ||
"""Protocol for array classes that have a size attribute.""" | ||
|
||
@property | ||
def size(self) -> int | None: | ||
"""Number of elements in an array. | ||
|
||
Returns: | ||
int | None: number of elements in an array. The returned value must | ||
be `None` if and only if one or more array dimensions are | ||
unknown. | ||
|
||
Notes: | ||
This must equal the product of the array's dimensions. | ||
|
||
""" | ||
... | ||
|
||
|
||
class HasTranspose(Protocol): | ||
"""Protocol for array classes that support the transpose operation.""" | ||
|
||
@property | ||
def T(self) -> Self: # noqa: N802 | ||
"""Transpose of the array. | ||
|
||
The array instance must be two-dimensional. If the array instance is not | ||
two-dimensional, an error should be raised. | ||
|
||
Returns: | ||
Self: two-dimensional array whose first and last dimensions (axes) | ||
are permuted in reverse order relative to original array. The | ||
returned array must have the same data type as the original | ||
array. | ||
|
||
Notes: | ||
Limiting the transpose to two-dimensional arrays (matrices) deviates | ||
from the NumPy et al practice of reversing all axes for arrays | ||
having more than two-dimensions. This is intentional, as reversing | ||
all axes was found to be problematic (e.g., conflicting with the | ||
mathematical definition of a transpose which is limited to matrices; | ||
not operating on batches of matrices; et cetera). In order to | ||
reverse all axes, one is recommended to use the functional | ||
`PermuteDims` interface found in this specification. | ||
|
||
""" | ||
... | ||
|
||
|
||
class Array( | ||
HasArrayNamespace[NamespaceT_co], | ||
# ------ Attributes ------- | ||
HasDType[DTypeT_co], | ||
HasDevice, | ||
HasMatrixTranspose, | ||
HasNDim, | ||
HasShape, | ||
HasSize, | ||
HasTranspose, | ||
# ------- Methods --------- | ||
HasArrayNamespace[NamespaceT_co], | ||
# ------------------------- | ||
Protocol[DTypeT_co, NamespaceT_co], | ||
): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.