-
Notifications
You must be signed in to change notification settings - Fork 5
✨: add CanArrayX protocols #32
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
base: main
Are you sure you want to change the base?
Changes from all commits
3473691
5d56158
c869543
c1962cd
f21c055
f862bf4
154e658
bcac721
dd7bc09
15527d1
47da1d3
94e4c60
ba6c5b4
83b2f18
bba937a
dce36c4
c26f488
19cd5f9
6e2be2c
fe86cd6
e19b28a
5165e18
0a935a8
fa4e6b8
6bb01a8
2ba52db
e346681
839beed
eeb588d
ba8b4f5
45cd14f
855ddf2
56e4814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
"""Static typing support for the array API standard.""" | ||
|
||
__all__ = ( | ||
"Array", | ||
"HasArrayNamespace", | ||
"__version__", | ||
"__version_tuple__", | ||
) | ||
|
||
from ._namespace import HasArrayNamespace | ||
from ._array import Array, HasArrayNamespace | ||
from ._version import version as __version__, version_tuple as __version_tuple__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
__all__ = ( | ||
"Array", | ||
"BoolArray", | ||
"HasArrayNamespace", | ||
"NumericArray", | ||
) | ||
|
||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Literal, Protocol, TypeAlias | ||
from typing_extensions import TypeVar | ||
|
||
import optype as op | ||
|
||
from ._utils import docstring_setter | ||
|
||
# Load docstrings from TOML file | ||
try: | ||
import tomllib | ||
except ImportError: | ||
import tomli as tomllib # type: ignore[import-not-found, no-redef] | ||
|
||
_docstrings_path = Path(__file__).parent / "_array_docstrings.toml" | ||
with _docstrings_path.open("rb") as f: | ||
_array_docstrings = tomllib.load(f)["docstrings"] | ||
|
||
NS_co = TypeVar("NS_co", covariant=True, default=ModuleType) | ||
T_contra = TypeVar("T_contra", contravariant=True) | ||
|
||
|
||
class HasArrayNamespace(Protocol[NS_co]): | ||
"""Protocol for classes that have an `__array_namespace__` method. | ||
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 | ||
) -> NS_co: ... | ||
|
||
|
||
@docstring_setter(**_array_docstrings) | ||
class Array( | ||
HasArrayNamespace[NS_co], | ||
op.CanPosSelf, | ||
op.CanNegSelf, | ||
op.CanAddSame[T_contra], | ||
op.CanIAddSelf[T_contra], | ||
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.
>>> class Thingy:
... def __add__(self, rhs, /):
... return self if isinstance(rhs, Thingy) else NotImplemented
...
>>> a = Thingy()
>>> a + a
<__main__.Thingy object at 0x7f9896498830>
>>> a += a
>>> a
<__main__.Thingy object at 0x7f9896498830> We already require 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. How do you read https://data-apis.org/array-api/2021.12/API_specification/array_object.html#in-place-operators. 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. The "May be implemented" makes it sound like it optional to me 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. But there's the mutability stuff as well; https://data-apis.org/array-api/2021.12/design_topics/copies_views_and_mutation.html#copyview-mutability 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. So I guess it depends on whether we want |
||
op.CanRAddSelf[T_contra], | ||
op.CanSubSame[T_contra], | ||
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. This won't accept boolean numpy arrays: >>> import numpy as np
>>> np.array(True) - np.array(False)
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
np.array(True) - np.array(False)
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead. 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. Hm. Suggestions? |
||
op.CanISubSelf[T_contra], | ||
op.CanRSubSelf[T_contra], | ||
op.CanMulSame[T_contra], | ||
op.CanIMulSelf[T_contra], | ||
op.CanRMulSelf[T_contra], | ||
op.CanTruedivSame[T_contra], | ||
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.
>>> import numpy as np
>>> np.array([1]) / np.array([1])
array([1.])
>>> np.array([True]) / np.array([True])
array([1.]) 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. So we need to write a more flexible Protocol for Truediv? 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. It's just that we can't have it return If you think we'll need that, I wouldn't mind adding such protocols to optype. I'm not sure what to call them though 🤔 |
||
op.CanITruedivSelf[T_contra], | ||
op.CanRTruedivSelf[T_contra], | ||
op.CanFloordivSame[T_contra], | ||
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. This doesn't hold for boolean numpy arrays: >>> import numpy as np
>>> np.array([True]) // np.array([True])
array([1], dtype=int8) |
||
op.CanIFloordivSelf[T_contra], | ||
op.CanRFloordivSelf[T_contra], | ||
op.CanModSame[T_contra], | ||
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. mod and floordiv have identical signatures in numpy, so this won't work for boolean arrays: >>> import numpy as np
>>> np.array([True]) % np.array([True])
array([0], dtype=int8) |
||
op.CanIModSelf[T_contra], | ||
op.CanRModSelf[T_contra], | ||
op.CanPowSame[T_contra], | ||
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. poor boolean arrays: >>> np.array([True]) ** np.array([True])
array([1], dtype=int8) |
||
op.CanIPowSelf[T_contra], | ||
op.CanRPowSelf[T_contra], | ||
Protocol[T_contra, NS_co], | ||
): | ||
"""Array API specification for array object attributes and methods.""" | ||
|
||
|
||
BoolArray: TypeAlias = Array[bool, NS_co] | ||
"""Array API specification for boolean array object attributes and methods. | ||
Specifically, this type alias fills the `T_contra` type variable with `bool`, | ||
allowing for `bool` objects to be added, subtracted, multiplied, etc. to the | ||
array object. | ||
""" | ||
|
||
NumericArray: TypeAlias = Array[float | int, NS_co] | ||
"""Array API specification for numeric array object attributes and methods. | ||
Specifically, this type alias fills the `T_contra` type variable with `float | | ||
int`, allowing for `float | int` objects to be added, subtracted, multiplied, | ||
etc. to the array object. | ||
""" |
Uh oh!
There was an error while loading. Please reload this page.