The rules for dispatch with ``__array_function__`` match those for ``__array_ufunc__`` (see `NEP-13 <http://www.numpy.org/neps/nep-0013-ufunc-overrides.html>`_). In particular:
- NumPy will gather implementations of ``__array_function__`` from all specified inputs and call them in order: subclasses before superclasses, and otherwise left to right. Note that in some edge cases, this differs slightly from the `current behavior <https://bugs.python.org/issue30140>`_ of Python. - Implementations of ``__array_function__`` indicate that they can handle the operation by returning any value other than ``NotImplemented``. - If all ``__array_function__`` methods return ``NotImplemented``, NumPy will raise ``TypeError``. I’d like to propose two changes to this: - ``np.NotImplementedButCoercible`` be a part of the standard from the start. - If all implementations return this, only then should it be coerced. - In the future, it might be good to mark something as coercible to coerce it to ``ndarray`` before passing to another object’s ``__array_ufunc__``. - This is necessary if libraries want to keep old behaviour for some functions, while overriding others. - Otherwise they have to implement overloads for all functions. This seems rather like an all-or-nothing choice, which I’d like to avoid. - It isn’t too hard to implement in practice. - Objects that don’t implement ``__array_function__`` should be treated as having returned ``np.NotImplementedButCoercible``. - This has the effect of coercing ``list``, etc. - At a minimum, to maintain compatibility, if all objects don’t implement ``__array_function__``, the old behaviour should stay. Also, I’m +1 on Marten’s suggestion that ``ndarray`` itself should implement ``__array_function__``.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion