Skip to content

Add an Array Protocol & improve static typing support #589

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
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use general Array protocol for other in comparisons
Signed-off-by: nstarman <[email protected]>
  • Loading branch information
nstarman committed Nov 24, 2024
commit 2feaad9251893bc66244f051aff5a1d71816f600
1 change: 1 addition & 0 deletions src/_array_api_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
]
nitpick_ignore_regex = [
("py:class", ".*array"),
("py:class", ".*Array"),
("py:class", ".*device"),
("py:class", ".*Device"),
("py:class", ".*dtype"),
Expand Down
26 changes: 13 additions & 13 deletions src/array_api_stubs/_draft/array_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __abs__(self: array, /) -> array:
"""
...

def __add__(self: array, other: int | float | array, /) -> array:
def __add__(self: array, other: int | float | Array, /) -> array:
"""
Calculates the sum for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -513,7 +513,7 @@ def __dlpack_device__(self, /) -> tuple[Enum, int]:
# Note that __eq__ returns an array while `object.__eq__` returns a bool.
# Hence Mypy will complain that this violates the Liskov substitution
# principle - ignore that.
def __eq__(self: array, other: int | float | bool | array, /) -> array: # type: ignore[override]
def __eq__(self: array, other: int | float | bool | Array, /) -> array: # type: ignore[override]
r"""
Computes the truth value of ``self_i == other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -577,7 +577,7 @@ def __float__(self, /) -> float:
"""
...

def __floordiv__(self: array, other: int | float | array, /) -> array:
def __floordiv__(self: array, other: int | float | Array, /) -> array:
"""
Evaluates ``self_i // other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand All @@ -602,7 +602,7 @@ def __floordiv__(self: array, other: int | float | array, /) -> array:
"""
...

def __ge__(self: array, other: int | float | array, /) -> array:
def __ge__(self: array, other: int | float | Array, /) -> array:
"""
Computes the truth value of ``self_i >= other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -658,7 +658,7 @@ def __getitem__(
"""
...

def __gt__(self: array, other: int | float | array, /) -> array:
def __gt__(self: array, other: int | float | Array, /) -> array:
"""
Computes the truth value of ``self_i > other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -786,7 +786,7 @@ def __invert__(self: array, /) -> array:
"""
...

def __le__(self: array, other: int | float | array, /) -> array:
def __le__(self: array, other: int | float | Array, /) -> array:
"""
Computes the truth value of ``self_i <= other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -836,7 +836,7 @@ def __lshift__(self: array, other: int | array, /) -> array:
"""
...

def __lt__(self: array, other: int | float | array, /) -> array:
def __lt__(self: array, other: int | float | Array, /) -> array:
"""
Computes the truth value of ``self_i < other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -913,7 +913,7 @@ def __matmul__(self: array, other: array, /) -> array:
"""
...

def __mod__(self: array, other: int | float | array, /) -> array:
def __mod__(self: array, other: int | float | Array, /) -> array:
"""
Evaluates ``self_i % other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand All @@ -938,7 +938,7 @@ def __mod__(self: array, other: int | float | array, /) -> array:
"""
...

def __mul__(self: array, other: int | float | array, /) -> array:
def __mul__(self: array, other: int | float | Array, /) -> array:
r"""
Calculates the product for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -969,7 +969,7 @@ def __mul__(self: array, other: int | float | array, /) -> array:
...

# See note above __eq__ method for explanation of the `type: ignore`
def __ne__(self: array, other: int | float | bool | array, /) -> array: # type: ignore[override]
def __ne__(self: array, other: int | float | bool | Array, /) -> array: # type: ignore[override]
"""
Computes the truth value of ``self_i != other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -1078,7 +1078,7 @@ def __pos__(self: array, /) -> array:
"""
...

def __pow__(self: array, other: int | float | array, /) -> array:
def __pow__(self: array, other: int | float | Array, /) -> array:
r"""
Calculates an implementation-dependent approximation of exponentiation by raising each element (the base) of an array instance to the power of ``other_i`` (the exponent), where ``other_i`` is the corresponding element of the array ``other``.

Expand Down Expand Up @@ -1163,7 +1163,7 @@ def __setitem__(
"""
...

def __sub__(self: array, other: int | float | array, /) -> array:
def __sub__(self: array, other: int | float | Array, /) -> array:
"""
Calculates the difference for each element of an array instance with the respective element of the array ``other``.

Expand Down Expand Up @@ -1192,7 +1192,7 @@ def __sub__(self: array, other: int | float | array, /) -> array:
"""
...

def __truediv__(self: array, other: int | float | array, /) -> array:
def __truediv__(self: array, other: int | float | Array, /) -> array:
r"""
Evaluates ``self_i / other_i`` for each element of an array instance with the respective element of the array ``other``.

Expand Down