<html><body><blockquote class="hm_quoted_text" style="padding-left:8px;margin:0;border-left:1px solid rgb(185,185,185);color:rgb(100,100,100)"><div>The rules for dispatch with ``__array_function__`` match those for</div><div dir="ltr"><div>``__array_ufunc__`` (see</div><div>`NEP-13 <<a href="http://www.numpy.org/neps/nep-0013-ufunc-overrides.html">http://www.numpy.org/neps/nep-0013-ufunc-overrides.html</a>>`_).</div><div>In particular:</div><div><br></div><div>- NumPy will gather implementations of ``__array_function__`` from all</div><div> specified inputs and call them in order: subclasses before</div><div> superclasses, and otherwise left to right. Note that in some edge cases,</div><div> this differs slightly from the</div><div> `current behavior <<a href="https://bugs.python.org/issue30140">https://bugs.python.org/issue30140</a>>`_ of Python.</div><div>- Implementations of ``__array_function__`` indicate that they can</div><div> handle the operation by returning any value other than</div><div> ``NotImplemented``.</div><div>- If all ``__array_function__`` methods return ``NotImplemented``,</div><div> NumPy will raise ``TypeError``.</div></div></blockquote><div><br></div><div>I’d like to propose two changes to this:</div><div><ul style="font-stretch:normal;line-height:17px;background-color:rgba(0,0,0,0)"><li style="color:rgb(0,0,0);font-family:"Helvetica Neue";font-size:15px;font-style:normal;font-variant-caps:normal;font-weight:normal;text-decoration:none">``np.NotImplementedButCoercible`` be a part of the standard from the start.</li><ul style="color:rgb(0,0,0);font-family:"Helvetica Neue";font-size:15px;font-style:normal;font-variant-caps:normal;font-weight:normal;text-decoration:none"><li>If all implementations return this, only then should it be coerced.</li><ul><li>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__``.</li></ul><li>This is necessary if libraries want to keep old behaviour for some functions, while overriding others.</li><li>Otherwise they have to implement overloads for all functions. This seems rather like an all-or-nothing choice, which I’d like to avoid.</li><li>It isn’t too hard to implement in practice.</li></ul><li>Objects that don’t implement ``__array_function__`` should be treated as having returned ``np.NotImplementedButCoercible``.</li><ul><li>This has the effect of coercing ``list``, etc.</li><li>At a minimum, to maintain compatibility, if all objects don’t implement ``__array_function__``, the old behaviour should stay.</li></ul></ul><div>Also, I’m +1 on Marten’s suggestion that ``ndarray`` itself should implement ``__array_function__``.</div></div><blockquote class="hm_quoted_text" style="padding-left:8px;margin:0;border-left:1px solid rgb(185,185,185);color:rgb(100,100,100)"><div dir="ltr"><div></div></div>
</blockquote>
</body></html>