(mocker)
| 1231 | |
| 1232 | |
| 1233 | def test_binary_operator_delegation(mocker): |
| 1234 | # Binary operator delegation in Dask should follow Numpy's: |
| 1235 | # https://numpy.org/neps/nep-0013-ufunc-overrides.html#behavior-in-combination-with-python-s-binary-operations |
| 1236 | |
| 1237 | x = from_array([1, 2, 3]) |
| 1238 | |
| 1239 | # Mock various types of `other` objects |
| 1240 | ufunc_none = mocker.Mock() |
| 1241 | ufunc_none.__array_ufunc__ = None |
| 1242 | ufunc_none.__radd__ = mocker.Mock() |
| 1243 | |
| 1244 | ufunc_high_priority = mocker.Mock() |
| 1245 | ufunc_high_priority.__array_priority__ = x.__array_priority__ + 1 |
| 1246 | ufunc_high_priority.__radd__ = mocker.Mock() |
| 1247 | |
| 1248 | ufunc_low_priority = mocker.Mock() |
| 1249 | ufunc_low_priority.__array_priority__ = x.__array_priority__ - 1 |
| 1250 | ufunc_low_priority.__radd__ = mocker.Mock() |
| 1251 | |
| 1252 | ufunc_no_priority = mocker.Mock() |
| 1253 | ufunc_no_priority.__radd__ = mocker.Mock() |
| 1254 | |
| 1255 | # If `other.__array_ufunc__ is None`, delegates back to Python |
| 1256 | # and therefore call reflected operator on `other` |
| 1257 | x + ufunc_none |
| 1258 | ufunc_none.__radd__.assert_called_once() |
| 1259 | |
| 1260 | # If the `__array_ufunc__` attribute is absent on other and |
| 1261 | # `other.__array_priority__ > self.__array_priority__`, also delegates back |
| 1262 | # to Python and therefore call reflected operator on `other` |
| 1263 | x + ufunc_high_priority |
| 1264 | ufunc_high_priority.__radd__.assert_called_once() |
| 1265 | |
| 1266 | # If `other.__array_priority__ <= self.__array_priority__`, does not |
| 1267 | # delegate (here it raises an error) |
| 1268 | with pytest.raises(TypeError): |
| 1269 | x + ufunc_low_priority |
| 1270 | ufunc_low_priority.__radd__.assert_not_called() |
| 1271 | |
| 1272 | # If `other.__array_priority__` is absent, does not delegate (raises) |
| 1273 | with pytest.raises(TypeError): |
| 1274 | x + ufunc_no_priority |
| 1275 | ufunc_no_priority.__radd__.assert_not_called() |
| 1276 | |
| 1277 | |
| 1278 | @pytest.mark.filterwarnings("ignore:overflow encountered in cast") # numpy >=2.0 |
nothing calls this directly
no test coverage detected
searching dependent graphs…