Coverage for src/bioimageio/core/_magic_tensor_ops.py: 73%
158 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1# this file was modified from the generated
2# https://github.com/pydata/xarray/blob/cf3655968b8b12cc0ecd28fb324e63fb94d5e7e2/xarray/core/_typed_ops.py
3# TODO: should we generate this ourselves?
4# TODO: test these magic methods
5import operator
6from collections.abc import Callable
7from typing import TYPE_CHECKING, Any
9from typing_extensions import Self
10from xarray.core import nputils
12if TYPE_CHECKING:
13 from xarray.computation import ops
14else:
15 try:
16 # xarray >= 2025.03
17 from xarray.computation import ops
18 except ImportError:
19 # xarray < 2025.03
20 from xarray.core import ops
23class MagicTensorOpsMixin:
24 __slots__ = ()
25 _Compatible = Any
27 def _binary_op(
28 self,
29 other: _Compatible,
30 f: Callable[[Any, Any], Any],
31 reflexive: bool = False,
32 ) -> Self:
33 raise NotImplementedError
35 def __add__(self, other: _Compatible) -> Self:
36 return self._binary_op(other, operator.add)
38 def __sub__(self, other: _Compatible) -> Self:
39 return self._binary_op(other, operator.sub)
41 def __mul__(self, other: _Compatible) -> Self:
42 return self._binary_op(other, operator.mul)
44 def __pow__(self, other: _Compatible) -> Self:
45 return self._binary_op(other, operator.pow) # pyright: ignore[reportUnknownArgumentType]
47 def __truediv__(self, other: _Compatible) -> Self:
48 return self._binary_op(other, operator.truediv) # pyright: ignore[reportUnknownArgumentType]
50 def __floordiv__(self, other: _Compatible) -> Self:
51 return self._binary_op(other, operator.floordiv) # pyright: ignore[reportUnknownArgumentType]
53 def __mod__(self, other: _Compatible) -> Self:
54 return self._binary_op(other, operator.mod)
56 def __and__(self, other: _Compatible) -> Self:
57 return self._binary_op(other, operator.and_) # pyright: ignore[reportUnknownArgumentType]
59 def __xor__(self, other: _Compatible) -> Self:
60 return self._binary_op(other, operator.xor) # pyright: ignore[reportUnknownArgumentType]
62 def __or__(self, other: _Compatible) -> Self:
63 return self._binary_op(other, operator.or_) # pyright: ignore[reportUnknownArgumentType]
65 def __lshift__(self, other: _Compatible) -> Self:
66 return self._binary_op(other, operator.lshift) # pyright: ignore[reportUnknownArgumentType]
68 def __rshift__(self, other: _Compatible) -> Self:
69 return self._binary_op(other, operator.rshift) # pyright: ignore[reportUnknownArgumentType]
71 def __lt__(self, other: _Compatible) -> Self:
72 return self._binary_op(other, operator.lt)
74 def __le__(self, other: _Compatible) -> Self:
75 return self._binary_op(other, operator.le)
77 def __gt__(self, other: _Compatible) -> Self:
78 return self._binary_op(other, operator.gt)
80 def __ge__(self, other: _Compatible) -> Self:
81 return self._binary_op(other, operator.ge)
83 def __eq__(self, other: _Compatible) -> Self: # type: ignore[override]
84 return self._binary_op(
85 other,
86 nputils.array_eq, # pyright: ignore[reportUnknownArgumentType]
87 )
89 def __ne__(self, other: _Compatible) -> Self: # type: ignore[override]
90 return self._binary_op(
91 other,
92 nputils.array_ne, # pyright: ignore[reportUnknownArgumentType]
93 )
95 # When __eq__ is defined but __hash__ is not, then an object is unhashable,
96 # and it should be declared as follows:
97 __hash__: None # type:ignore[assignment]
99 def __radd__(self, other: _Compatible) -> Self:
100 return self._binary_op(other, operator.add, reflexive=True)
102 def __rsub__(self, other: _Compatible) -> Self:
103 return self._binary_op(other, operator.sub, reflexive=True)
105 def __rmul__(self, other: _Compatible) -> Self:
106 return self._binary_op(other, operator.mul, reflexive=True)
108 def __rpow__(self, other: _Compatible) -> Self:
109 return self._binary_op(other, operator.pow, reflexive=True) # pyright: ignore[reportUnknownArgumentType]
111 def __rtruediv__(self, other: _Compatible) -> Self:
112 return self._binary_op(other, operator.truediv, reflexive=True) # pyright: ignore[reportUnknownArgumentType]
114 def __rfloordiv__(self, other: _Compatible) -> Self:
115 return self._binary_op(other, operator.floordiv, reflexive=True) # pyright: ignore[reportUnknownArgumentType]
117 def __rmod__(self, other: _Compatible) -> Self:
118 return self._binary_op(other, operator.mod, reflexive=True)
120 def __rand__(self, other: _Compatible) -> Self:
121 return self._binary_op(other, operator.and_, reflexive=True) # pyright: ignore[reportUnknownArgumentType]
123 def __rxor__(self, other: _Compatible) -> Self:
124 return self._binary_op(other, operator.xor, reflexive=True) # pyright: ignore[reportUnknownArgumentType]
126 def __ror__(self, other: _Compatible) -> Self:
127 return self._binary_op(other, operator.or_, reflexive=True) # pyright: ignore[reportUnknownArgumentType]
129 def _inplace_binary_op(
130 self, other: _Compatible, f: Callable[[Any, Any], Any]
131 ) -> Self:
132 raise NotImplementedError
134 def __iadd__(self, other: _Compatible) -> Self:
135 return self._inplace_binary_op(other, operator.iadd) # pyright: ignore[reportUnknownArgumentType]
137 def __isub__(self, other: _Compatible) -> Self:
138 return self._inplace_binary_op(other, operator.isub) # pyright: ignore[reportUnknownArgumentType]
140 def __imul__(self, other: _Compatible) -> Self:
141 return self._inplace_binary_op(other, operator.imul) # pyright: ignore[reportUnknownArgumentType]
143 def __ipow__(self, other: _Compatible) -> Self:
144 return self._inplace_binary_op(other, operator.ipow) # pyright: ignore[reportUnknownArgumentType]
146 def __itruediv__(self, other: _Compatible) -> Self:
147 return self._inplace_binary_op(other, operator.itruediv) # pyright: ignore[reportUnknownArgumentType]
149 def __ifloordiv__(self, other: _Compatible) -> Self:
150 return self._inplace_binary_op(other, operator.ifloordiv) # pyright: ignore[reportUnknownArgumentType]
152 def __imod__(self, other: _Compatible) -> Self:
153 return self._inplace_binary_op(other, operator.imod) # pyright: ignore[reportUnknownArgumentType]
155 def __iand__(self, other: _Compatible) -> Self:
156 return self._inplace_binary_op(other, operator.iand) # pyright: ignore[reportUnknownArgumentType]
158 def __ixor__(self, other: _Compatible) -> Self:
159 return self._inplace_binary_op(other, operator.ixor) # pyright: ignore[reportUnknownArgumentType]
161 def __ior__(self, other: _Compatible) -> Self:
162 return self._inplace_binary_op(other, operator.ior) # pyright: ignore[reportUnknownArgumentType]
164 def __ilshift__(self, other: _Compatible) -> Self:
165 return self._inplace_binary_op(other, operator.ilshift) # pyright: ignore[reportUnknownArgumentType]
167 def __irshift__(self, other: _Compatible) -> Self:
168 return self._inplace_binary_op(other, operator.irshift) # pyright: ignore[reportUnknownArgumentType]
170 def _unary_op(self, f: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Self:
171 raise NotImplementedError
173 def __neg__(self) -> Self:
174 return self._unary_op(operator.neg)
176 def __pos__(self) -> Self:
177 return self._unary_op(operator.pos)
179 def __abs__(self) -> Self:
180 return self._unary_op(operator.abs)
182 def __invert__(self) -> Self:
183 return self._unary_op(operator.invert)
185 def round(self, *args: Any, **kwargs: Any) -> Self:
186 return self._unary_op(
187 ops.round_, # pyright: ignore[reportUnknownArgumentType]
188 *args,
189 **kwargs,
190 )
192 def argsort(self, *args: Any, **kwargs: Any) -> Self:
193 return self._unary_op(
194 ops.argsort, # pyright: ignore[reportUnknownArgumentType]
195 *args,
196 **kwargs,
197 )
199 def conj(self, *args: Any, **kwargs: Any) -> Self:
200 return self._unary_op(
201 ops.conj, # pyright: ignore[reportUnknownArgumentType]
202 *args,
203 **kwargs,
204 )
206 def conjugate(self, *args: Any, **kwargs: Any) -> Self:
207 return self._unary_op(
208 ops.conjugate, # pyright: ignore[reportUnknownArgumentType]
209 *args,
210 **kwargs,
211 )
213 __add__.__doc__ = operator.add.__doc__
214 __sub__.__doc__ = operator.sub.__doc__
215 __mul__.__doc__ = operator.mul.__doc__
216 __pow__.__doc__ = operator.pow.__doc__
217 __truediv__.__doc__ = operator.truediv.__doc__
218 __floordiv__.__doc__ = operator.floordiv.__doc__
219 __mod__.__doc__ = operator.mod.__doc__
220 __and__.__doc__ = operator.and_.__doc__
221 __xor__.__doc__ = operator.xor.__doc__
222 __or__.__doc__ = operator.or_.__doc__
223 __lshift__.__doc__ = operator.lshift.__doc__
224 __rshift__.__doc__ = operator.rshift.__doc__
225 __lt__.__doc__ = operator.lt.__doc__
226 __le__.__doc__ = operator.le.__doc__
227 __gt__.__doc__ = operator.gt.__doc__
228 __ge__.__doc__ = operator.ge.__doc__
229 __eq__.__doc__ = nputils.array_eq.__doc__
230 __ne__.__doc__ = nputils.array_ne.__doc__
231 __radd__.__doc__ = operator.add.__doc__
232 __rsub__.__doc__ = operator.sub.__doc__
233 __rmul__.__doc__ = operator.mul.__doc__
234 __rpow__.__doc__ = operator.pow.__doc__
235 __rtruediv__.__doc__ = operator.truediv.__doc__
236 __rfloordiv__.__doc__ = operator.floordiv.__doc__
237 __rmod__.__doc__ = operator.mod.__doc__
238 __rand__.__doc__ = operator.and_.__doc__
239 __rxor__.__doc__ = operator.xor.__doc__
240 __ror__.__doc__ = operator.or_.__doc__
241 __iadd__.__doc__ = operator.iadd.__doc__
242 __isub__.__doc__ = operator.isub.__doc__
243 __imul__.__doc__ = operator.imul.__doc__
244 __ipow__.__doc__ = operator.ipow.__doc__
245 __itruediv__.__doc__ = operator.itruediv.__doc__
246 __ifloordiv__.__doc__ = operator.ifloordiv.__doc__
247 __imod__.__doc__ = operator.imod.__doc__
248 __iand__.__doc__ = operator.iand.__doc__
249 __ixor__.__doc__ = operator.ixor.__doc__
250 __ior__.__doc__ = operator.ior.__doc__
251 __ilshift__.__doc__ = operator.ilshift.__doc__
252 __irshift__.__doc__ = operator.irshift.__doc__
253 __neg__.__doc__ = operator.neg.__doc__
254 __pos__.__doc__ = operator.pos.__doc__
255 __abs__.__doc__ = operator.abs.__doc__
256 __invert__.__doc__ = operator.invert.__doc__