Wrapping a MaskedArray rather than subclassing to test that ufunc deferrals are commutative. See: https://github.com/numpy/numpy/issues/15200)
| 149 | |
| 150 | |
| 151 | class WrappedArray(NDArrayOperatorsMixin): |
| 152 | """ |
| 153 | Wrapping a MaskedArray rather than subclassing to test that |
| 154 | ufunc deferrals are commutative. |
| 155 | See: https://github.com/numpy/numpy/issues/15200) |
| 156 | """ |
| 157 | __slots__ = ('_array', 'attrs') |
| 158 | __array_priority__ = 20 |
| 159 | |
| 160 | def __init__(self, array, **attrs): |
| 161 | self._array = array |
| 162 | self.attrs = attrs |
| 163 | |
| 164 | def __repr__(self): |
| 165 | return f"{self.__class__.__name__}(\n{self._array}\n{self.attrs}\n)" |
| 166 | |
| 167 | def __array__(self): |
| 168 | return np.asarray(self._array) |
| 169 | |
| 170 | def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): |
| 171 | if method == '__call__': |
| 172 | inputs = [arg._array if isinstance(arg, self.__class__) else arg |
| 173 | for arg in inputs] |
| 174 | return self.__class__(ufunc(*inputs, **kwargs), **self.attrs) |
| 175 | else: |
| 176 | return NotImplemented |
| 177 | |
| 178 | |
| 179 | class TestSubclassing: |
no outgoing calls