Skip to content

Commit 9e8b867

Browse files
committed
✨: add CanArrayAdd protocol
Support addition operator for array classes. Signed-off-by: Nathaniel Starkman <[email protected]>
1 parent 836b386 commit 9e8b867

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ version_tuple = {version_tuple!r}
137137
"FBT", # flake8-boolean-trap
138138
"FIX", # flake8-fixme
139139
"ISC001", # Conflicts with formatter
140+
"PLW1641", # Object does not implement `__hash__` method
141+
"PYI041", # Use `float` instead of `int | float`
140142
]
141143

142144
[tool.ruff.lint.per-file-ignores]

src/array_api_typing/_array.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,32 @@ def __neg__(self) -> Self:
6666
...
6767

6868

69+
class CanArrayAdd(Protocol):
70+
"""Protocol for array classes that support the addition operator."""
71+
72+
def __add__(self, other: Self | int | float, /) -> Self:
73+
"""Calculates the sum for each element of an array instance with the respective element of the array `other`.
74+
75+
Args:
76+
other: addend array. Must be compatible with `self` (see
77+
Broadcasting). Should have a numeric data type.
78+
79+
Returns:
80+
Self: an array containing the element-wise sums. The returned array
81+
must have a data type determined by Type Promotion Rules.
82+
83+
See Also:
84+
array_api_typing.Add
85+
86+
""" # noqa: E501
87+
...
88+
89+
6990
class Array(
7091
HasArrayNamespace[NS_co],
7192
CanArrayPos,
7293
CanArrayNeg,
94+
CanArrayAdd,
7395
Protocol,
7496
):
7597
"""Array API specification for array object attributes and methods."""

0 commit comments

Comments
 (0)