Skip to content

Commit ebd7bf2

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

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
@@ -131,6 +131,8 @@ version_tuple = {version_tuple!r}
131131
"FBT", # flake8-boolean-trap
132132
"FIX", # flake8-fixme
133133
"ISC001", # Conflicts with formatter
134+
"PLW1641", # Object does not implement `__hash__` method
135+
"PYI041", # Use `float` instead of `int | float`
134136
]
135137

136138
[tool.ruff.lint.pylint]

src/array_api_typing/_array.py

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

7070

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

0 commit comments

Comments
 (0)