MCPcopy Create free account
hub / github.com/dask/dask / test_binary_operator_delegation

Function test_binary_operator_delegation

dask/array/tests/test_array_core.py:1232–1274  ·  view source on GitHub ↗
(mocker)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

from_arrayFunction · 0.90

Tested by

no test coverage detected