|
1 | 1 | import abc
|
2 |
| -from typing import Any, Literal as L |
3 |
| -from typing_extensions import Self |
| 2 | +from typing import Any, Generic, Literal as L, TypeAlias, type_check_only |
| 3 | +from typing_extensions import Self, TypeVar |
4 | 4 |
|
5 | 5 | import numpy as np
|
6 | 6 |
|
7 | 7 | __all__ = ["NDArrayOperatorsMixin"]
|
8 | 8 |
|
9 |
| -# NOTE: `NDArrayOperatorsMixin` is not formally an abstract baseclass, |
10 |
| -# even though it's reliant on subclasses implementing `__array_ufunc__` |
| 9 | +### |
| 10 | + |
| 11 | +_T_contra = TypeVar("_T_contra", contravariant=True, default=Any) |
| 12 | +_T_co = TypeVar("_T_co", covariant=True, default=Any) |
| 13 | + |
| 14 | +_AnyArray: TypeAlias = np.ndarray[Any, np.dtype[Any]] |
| 15 | + |
| 16 | +### |
11 | 17 |
|
12 | 18 | # NOTE: The accepted input- and output-types of the various dunders are
|
13 | 19 | # completely dependent on how `__array_ufunc__` is implemented.
|
14 | 20 | # As such, only little type safety can be provided here.
|
15 | 21 |
|
16 |
| -class NDArrayOperatorsMixin(abc.ABC): |
| 22 | +class NDArrayOperatorsMixin(Generic[_T_contra, _T_co]): |
| 23 | + __slots__ = () |
| 24 | + |
17 | 25 | @abc.abstractmethod
|
| 26 | + @type_check_only |
18 | 27 | def __array_ufunc__(
|
19 | 28 | self,
|
20 | 29 | ufunc: np.ufunc,
|
21 |
| - method: L["__call__", "reduce", "reduceat", "accumulate", "outer", "at"], |
22 |
| - *inputs: Any, |
23 |
| - **kwargs: Any, |
24 |
| - ) -> Any: ... |
25 |
| - def __lt__(self, other: Any, /) -> Any: ... |
26 |
| - def __le__(self, other: Any, /) -> Any: ... |
27 |
| - def __eq__(self, other: object, /) -> Any: ... |
28 |
| - def __ne__(self, other: object, /) -> Any: ... |
29 |
| - def __gt__(self, other: Any, /) -> Any: ... |
30 |
| - def __ge__(self, other: Any, /) -> Any: ... |
31 |
| - def __add__(self, other: Any, /) -> Any: ... |
32 |
| - def __radd__(self, other: Any, /) -> Any: ... |
33 |
| - def __iadd__(self, other: Any, /) -> Self: ... |
34 |
| - def __sub__(self, other: Any, /) -> Any: ... |
35 |
| - def __rsub__(self, other: Any, /) -> Any: ... |
36 |
| - def __isub__(self, other: Any, /) -> Self: ... |
37 |
| - def __mul__(self, other: Any, /) -> Any: ... |
38 |
| - def __rmul__(self, other: Any, /) -> Any: ... |
39 |
| - def __imul__(self, other: Any, /) -> Self: ... |
40 |
| - def __matmul__(self, other: Any, /) -> Any: ... |
41 |
| - def __rmatmul__(self, other: Any, /) -> Any: ... |
42 |
| - def __imatmul__(self, other: Any, /) -> Self: ... |
43 |
| - def __truediv__(self, other: Any, /) -> Any: ... |
44 |
| - def __rtruediv__(self, other: Any, /) -> Any: ... |
45 |
| - def __itruediv__(self, other: Any, /) -> Self: ... |
46 |
| - def __floordiv__(self, other: Any, /) -> Any: ... |
47 |
| - def __rfloordiv__(self, other: Any, /) -> Any: ... |
48 |
| - def __ifloordiv__(self, other: Any, /) -> Self: ... |
49 |
| - def __mod__(self, other: Any, /) -> Any: ... |
50 |
| - def __rmod__(self, other: Any, /) -> Any: ... |
51 |
| - def __imod__(self, other: Any, /) -> Self: ... |
52 |
| - def __divmod__(self, other: Any, /) -> Any: ... |
53 |
| - def __rdivmod__(self, other: Any, /) -> Any: ... |
54 |
| - def __pow__(self, other: Any, /) -> Any: ... |
55 |
| - def __rpow__(self, other: Any, /) -> Any: ... |
56 |
| - def __ipow__(self, other: Any, /) -> Self: ... |
57 |
| - def __lshift__(self, other: Any, /) -> Any: ... |
58 |
| - def __rlshift__(self, other: Any, /) -> Any: ... |
59 |
| - def __ilshift__(self, other: Any, /) -> Self: ... |
60 |
| - def __rshift__(self, other: Any, /) -> Any: ... |
61 |
| - def __rrshift__(self, other: Any, /) -> Any: ... |
62 |
| - def __irshift__(self, other: Any, /) -> Self: ... |
63 |
| - def __and__(self, other: Any, /) -> Any: ... |
64 |
| - def __rand__(self, other: Any, /) -> Any: ... |
65 |
| - def __iand__(self, other: Any, /) -> Self: ... |
66 |
| - def __xor__(self, other: Any, /) -> Any: ... |
67 |
| - def __rxor__(self, other: Any, /) -> Any: ... |
68 |
| - def __ixor__(self, other: Any, /) -> Self: ... |
69 |
| - def __or__(self, other: Any, /) -> Any: ... |
70 |
| - def __ror__(self, other: Any, /) -> Any: ... |
71 |
| - def __ior__(self, other: Any, /) -> Self: ... |
72 |
| - def __neg__(self) -> Any: ... |
73 |
| - def __pos__(self) -> Any: ... |
74 |
| - def __abs__(self) -> Any: ... |
75 |
| - def __invert__(self) -> Any: ... |
| 30 | + method: L["__call__"], |
| 31 | + x: _T_contra, |
| 32 | + /, |
| 33 | + out: tuple[_AnyArray] | tuple[_AnyArray, _AnyArray] | None = None, |
| 34 | + ) -> Self | _T_co | _AnyArray: ... |
| 35 | + |
| 36 | + # |
| 37 | + def __eq__(self, x: _T_contra, /) -> _T_co: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] |
| 38 | + def __ne__(self, x: _T_contra, /) -> _T_co: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] |
| 39 | + def __lt__(self, x: _T_contra, /) -> _T_co: ... |
| 40 | + def __le__(self, x: _T_contra, /) -> _T_co: ... |
| 41 | + def __gt__(self, x: _T_contra, /) -> _T_co: ... |
| 42 | + def __ge__(self, x: _T_contra, /) -> _T_co: ... |
| 43 | + |
| 44 | + # |
| 45 | + def __add__(self, x: _T_contra, /) -> _T_co: ... |
| 46 | + def __radd__(self, x: _T_contra, /) -> _T_co: ... |
| 47 | + def __iadd__(self, x: _T_contra, /) -> Self: ... |
| 48 | + |
| 49 | + # |
| 50 | + def __sub__(self, x: _T_contra, /) -> _T_co: ... |
| 51 | + def __rsub__(self, x: _T_contra, /) -> _T_co: ... |
| 52 | + def __isub__(self, x: _T_contra, /) -> Self: ... |
| 53 | + |
| 54 | + # |
| 55 | + def __mul__(self, x: _T_contra, /) -> _T_co: ... |
| 56 | + def __rmul__(self, x: _T_contra, /) -> _T_co: ... |
| 57 | + def __imul__(self, x: _T_contra, /) -> Self: ... |
| 58 | + |
| 59 | + # |
| 60 | + def __matmul__(self, x: _T_contra, /) -> _T_co: ... |
| 61 | + def __rmatmul__(self, x: _T_contra, /) -> _T_co: ... |
| 62 | + def __imatmul__(self, x: _T_contra, /) -> Self: ... |
| 63 | + |
| 64 | + # |
| 65 | + def __truediv__(self, x: _T_contra, /) -> _T_co: ... |
| 66 | + def __rtruediv__(self, x: _T_contra, /) -> _T_co: ... |
| 67 | + def __itruediv__(self, x: _T_contra, /) -> Self: ... |
| 68 | + |
| 69 | + # |
| 70 | + def __floordiv__(self, x: _T_contra, /) -> _T_co: ... |
| 71 | + def __rfloordiv__(self, x: _T_contra, /) -> _T_co: ... |
| 72 | + def __ifloordiv__(self, x: _T_contra, /) -> Self: ... |
| 73 | + |
| 74 | + # |
| 75 | + def __mod__(self, x: _T_contra, /) -> _T_co: ... |
| 76 | + def __rmod__(self, x: _T_contra, /) -> _T_co: ... |
| 77 | + def __imod__(self, x: _T_contra, /) -> Self: ... |
| 78 | + |
| 79 | + # |
| 80 | + def __divmod__(self, x: _T_contra, /) -> tuple[Any, Any]: ... |
| 81 | + def __rdivmod__(self, x: _T_contra, /) -> tuple[Any, Any]: ... |
| 82 | + |
| 83 | + # |
| 84 | + def __pow__(self, x: _T_contra, /) -> _T_co: ... |
| 85 | + def __rpow__(self, x: _T_contra, /) -> _T_co: ... |
| 86 | + def __ipow__(self, x: _T_contra, /) -> Self: ... |
| 87 | + |
| 88 | + # |
| 89 | + def __lshift__(self, x: _T_contra, /) -> _T_co: ... |
| 90 | + def __rlshift__(self, x: _T_contra, /) -> _T_co: ... |
| 91 | + def __ilshift__(self, x: _T_contra, /) -> Self: ... |
| 92 | + |
| 93 | + # |
| 94 | + def __rshift__(self, x: _T_contra, /) -> _T_co: ... |
| 95 | + def __rrshift__(self, x: _T_contra, /) -> _T_co: ... |
| 96 | + def __irshift__(self, x: _T_contra, /) -> Self: ... |
| 97 | + |
| 98 | + # |
| 99 | + def __and__(self, x: _T_contra, /) -> _T_co: ... |
| 100 | + def __rand__(self, x: _T_contra, /) -> _T_co: ... |
| 101 | + def __iand__(self, x: _T_contra, /) -> Self: ... |
| 102 | + |
| 103 | + # |
| 104 | + def __xor__(self, x: _T_contra, /) -> _T_co: ... |
| 105 | + def __rxor__(self, x: _T_contra, /) -> _T_co: ... |
| 106 | + def __ixor__(self, x: _T_contra, /) -> Self: ... |
| 107 | + |
| 108 | + # |
| 109 | + def __or__(self, x: _T_contra, /) -> _T_co: ... |
| 110 | + def __ror__(self, x: _T_contra, /) -> _T_co: ... |
| 111 | + def __ior__(self, x: _T_contra, /) -> Self: ... |
| 112 | + |
| 113 | + # |
| 114 | + def __neg__(self, /) -> _T_co: ... |
| 115 | + def __pos__(self, /) -> _T_co: ... |
| 116 | + def __abs__(self, /) -> _T_co: ... |
| 117 | + def __invert__(self, /) -> _T_co: ... |
0 commit comments