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

Hameer Abbasi einstein.edison at gmail.com
Sun Jun 3 14:00:32 EDT 2018


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__``.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180603/85524fcc/attachment.html>


More information about the NumPy-Discussion mailing list