[Numpy-discussion] NEP: Dispatch Mechanism for NumPy’s high level API

Marten van Kerkwijk m.h.vankerkwijk at gmail.com
Sun Jun 3 14:10:37 EDT 2018


On Sun, Jun 3, 2018 at 2:00 PM, Hameer Abbasi <einstein.edison at gmail.com>
wrote:

> 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.
>
> I think the issue is real but I would be slightly worried about adding
multiple possible things to return - there is a benefit to an answer being
either "I cannot do this" or "here's the result". I also am not sure there
is an actual problem: In the scheme as proposed, implementations could just
coerce themselves to array and call the routine again. (Or, in the scheme I
proposed, call the routine again but with `coerce=True`.)


>
>    - 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.
>
> I think that in the proposed scheme this is effectively what happens.

-- Marten
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180603/ac22be3f/attachment.html>


More information about the NumPy-Discussion mailing list