-
Notifications
You must be signed in to change notification settings - Fork 5
✨ HasDType, Array #48
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,12 @@ | ||
"""Static typing support for the array API standard.""" | ||
|
||
__all__ = ( | ||
"Array", | ||
"HasArrayNamespace", | ||
"HasDType", | ||
"__version__", | ||
"__version_tuple__", | ||
) | ||
|
||
from ._namespace import HasArrayNamespace | ||
from ._array import Array, HasArrayNamespace, HasDType | ||
from ._version import version as __version__, version_tuple as __version_tuple__ |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
__all__ = ( | ||
"Array", | ||
"HasArrayNamespace", | ||
) | ||
|
||
from types import ModuleType | ||
from typing import Literal, Protocol | ||
from typing_extensions import TypeVar | ||
|
||
NamespaceT_co = TypeVar("NamespaceT_co", covariant=True, default=ModuleType) | ||
DTypeT_co = TypeVar("DTypeT_co", covariant=True) | ||
|
||
|
||
class HasArrayNamespace(Protocol[NamespaceT_co]): | ||
"""Protocol for classes that have an `__array_namespace__` method. | ||
|
||
This `Protocol` is intended for use in static typing to ensure that an | ||
object has an `__array_namespace__` method that returns a namespace for | ||
array operations. This `Protocol` should not be used at runtime for type | ||
checking or as a base class. | ||
|
||
Example: | ||
>>> import array_api_typing as xpt | ||
>>> | ||
>>> class MyArray: | ||
... def __array_namespace__(self): | ||
... return object() | ||
>>> | ||
>>> x = MyArray() | ||
>>> def has_array_namespace(x: xpt.HasArrayNamespace) -> bool: | ||
... return hasattr(x, "__array_namespace__") | ||
>>> has_array_namespace(x) | ||
True | ||
|
||
""" | ||
|
||
def __array_namespace__( | ||
self, /, *, api_version: Literal["2021.12"] | None = None | ||
) -> NamespaceT_co: | ||
"""Returns an object that has all the array API functions on it. | ||
|
||
Args: | ||
api_version: string representing the version of the array API | ||
specification to be returned, in 'YYYY.MM' form, for example, | ||
'2020.10'. If it is `None`, it should return the namespace | ||
corresponding to latest version of the array API specification. | ||
If the given version is invalid or not implemented for the given | ||
module, an error should be raised. Default: `None`. | ||
|
||
Returns: | ||
NamespaceT_co: An object representing the array API namespace. It | ||
should have every top-level function defined in the | ||
specification as an attribute. It may contain other public names | ||
as well, but it is recommended to only include those names that | ||
are part of the specification. | ||
|
||
""" | ||
jorenham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
... | ||
|
||
|
||
class HasDType(Protocol[DTypeT_co]): | ||
jorenham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Protocol for array classes that have a data type attribute.""" | ||
|
||
@property | ||
def dtype(self, /) -> DTypeT_co: | ||
"""Data type of the array elements.""" | ||
... | ||
|
||
|
||
class Array( | ||
jorenham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HasArrayNamespace[NamespaceT_co], | ||
# ------ Attributes ------- | ||
HasDType[DTypeT_co], | ||
# ------------------------- | ||
Protocol[DTypeT_co, NamespaceT_co], | ||
): | ||
"""Array API specification for array object attributes and methods. | ||
|
||
The type is: ``Array[+DTypeT, +NamespaceT = ModuleType] = Array[DTypeT, | ||
NamespaceT]`` where: | ||
|
||
- `DTypeT` is the data type of the array elements. | ||
- `NamespaceT` is the type of the array namespace. It defaults to | ||
`ModuleType`, which is the most common form of array namespace (e.g., | ||
`numpy`, `cupy`, etc.). However, it can be any type, e.g. a | ||
`types.SimpleNamespace`, to allow for wrapper libraries to | ||
semi-dynamically define their own array namespaces based on the wrapped | ||
array type. | ||
|
||
This type is intended for use in static typing to ensure that an object has | ||
the attributes and methods defined in the array API specification. It should | ||
not be used at runtime for type checking or as a base class. | ||
|
||
""" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# mypy: disable-error-code="no-redef" | ||
|
||
from types import ModuleType | ||
from typing import Any | ||
|
||
import numpy.array_api as np # type: ignore[import-not-found, unused-ignore] | ||
from numpy import dtype | ||
|
||
import array_api_typing as xpt | ||
|
||
# Define NDArrays against which we can test the protocols | ||
# Note that `np.array_api` doesn't support boolean arrays. | ||
nparr = np.eye(2) | ||
nparr_i32 = np.asarray([1], dtype=np.int32) | ||
nparr_f32 = np.asarray([1.0], dtype=np.float32) | ||
|
||
# ========================================================= | ||
# `xpt.HasArrayNamespace` | ||
|
||
_: xpt.HasArrayNamespace[ModuleType] = nparr | ||
_: xpt.HasArrayNamespace[ModuleType] = nparr_i32 | ||
_: xpt.HasArrayNamespace[ModuleType] = nparr_f32 | ||
|
||
# Check `__array_namespace__` method | ||
a_ns: xpt.HasArrayNamespace[ModuleType] = nparr | ||
ns: ModuleType = a_ns.__array_namespace__() | ||
|
||
# Incorrect values are caught when using `__array_namespace__` and | ||
# backpropagated to the type of `a_ns` | ||
_: xpt.HasArrayNamespace[dict[str, int]] = nparr # not caught | ||
|
||
# ========================================================= | ||
# `xpt.HasDType` | ||
|
||
# Note that `np.array_api` uses dtype objects, not dtype classes, so we can't | ||
# type annotate specific dtypes like `np.float32` or `np.int32`. | ||
|
||
_: xpt.HasDType[dtype[Any]] = nparr | ||
_: xpt.HasDType[dtype[Any]] = nparr_i32 | ||
_: xpt.HasDType[dtype[Any]] = nparr_f32 | ||
|
||
# ========================================================= | ||
# `xpt.Array` | ||
|
||
# Check NamespaceT_co assignment | ||
a_ns: xpt.Array[Any, ModuleType] = nparr | ||
|
||
# Check DTypeT_co assignment | ||
# Note that `np.array_api` uses dtype objects, not dtype classes, so we can't | ||
# type annotate specific dtypes like `np.float32` or `np.int32`. | ||
_: xpt.Array[dtype[Any]] = nparr | ||
_: xpt.Array[dtype[Any]] = nparr_i32 | ||
_: xpt.Array[dtype[Any]] = nparr_f32 |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# mypy: disable-error-code="no-redef" | ||
|
||
from types import ModuleType | ||
from typing import Any, TypeAlias | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
import array_api_typing as xpt | ||
|
||
# DType aliases | ||
F32: TypeAlias = np.float32 | ||
I32: TypeAlias = np.int32 | ||
|
||
# Define NDArrays against which we can test the protocols | ||
nparr: npt.NDArray[Any] | ||
nparr_i32: npt.NDArray[I32] | ||
nparr_f32: npt.NDArray[F32] | ||
nparr_b: npt.NDArray[np.bool_] | ||
|
||
# ========================================================= | ||
# `xpt.HasArrayNamespace` | ||
|
||
# Check assignment | ||
_: xpt.HasArrayNamespace[ModuleType] = nparr | ||
_: xpt.HasArrayNamespace[ModuleType] = nparr_i32 | ||
_: xpt.HasArrayNamespace[ModuleType] = nparr_f32 | ||
_: xpt.HasArrayNamespace[ModuleType] = nparr_b | ||
|
||
# Check `__array_namespace__` method | ||
a_ns: xpt.HasArrayNamespace[ModuleType] = nparr | ||
ns: ModuleType = a_ns.__array_namespace__() | ||
|
||
# Incorrect values are caught when using `__array_namespace__` and | ||
# backpropagated to the type of `a_ns` | ||
_: xpt.HasArrayNamespace[dict[str, int]] = nparr # not caught | ||
|
||
# ========================================================= | ||
# `xpt.HasDType` | ||
|
||
# Check DTypeT_co assignment | ||
_: xpt.HasDType[Any] = nparr | ||
_: xpt.HasDType[np.dtype[I32]] = nparr_i32 | ||
_: xpt.HasDType[np.dtype[F32]] = nparr_f32 | ||
_: xpt.HasDType[np.dtype[np.bool_]] = nparr_b | ||
|
||
# ========================================================= | ||
# `xpt.Array` | ||
|
||
# Check NamespaceT_co assignment | ||
a_ns: xpt.Array[Any, ModuleType] = nparr | ||
|
||
# Check DTypeT_co assignment | ||
_: xpt.Array[Any] = nparr | ||
_: xpt.Array[np.dtype[I32]] = nparr_i32 | ||
_: xpt.Array[np.dtype[F32]] = nparr_f32 | ||
_: xpt.Array[np.dtype[np.bool_]] = nparr_b |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from test_numpy2p0 import nparr | ||
|
||
import array_api_typing as xpt | ||
|
||
_: xpt.HasArrayNamespace[dict[str, int]] = nparr # type: ignore[assignment] |
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.