-
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
135f8ad
003b4eb
382373c
f78d155
3ee1591
ebd7bf2
e9d6d23
953458a
ea598bc
5b873c7
66c50b8
5ce8592
25d0170
bee031f
2696f0a
3e39bf4
f112f54
d1378b3
5adb634
baab474
2cf632e
3652bab
cd5ba89
5ca860c
11775ff
b8b9570
5d50e54
6425dec
dac9bb4
14dc47a
d2a0e69
e4fd64a
876b4a5
083ccf1
ba3ee4e
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,95 @@ | ||
__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.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? 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. isomorphic to #32 (comment) |
||
op.CanRSubSelf[T_contra], | ||
op.CanMulSame[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 🤔 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 a real shame that 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. Yea, and there's no need for that restriction either: https://discuss.python.org/t/self-as-typevar-default/909 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've investigated further an it seems like we need CanTruedivSame[_T_contra, _TT_co = Self]:
def __add__(self, rhs: Self | _T_contra, /) -> _TT_co: ... Which isn't allowed because of restrictions in https://discuss.python.org/t/self-as-typevar-default/909, right? So what about expanding CanTruedivSame[_T_contra, _TT_co = Never]:
def __add__(self, rhs: Self | _T_contra, /) -> Self | _TT_co: ... Then this reduces to If this works, it would be a good change for all the binops-Same. 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 like it! Then |
||
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.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.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.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.