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

Stephan Hoyer shoyer at gmail.com
Tue Jun 5 14:34:18 EDT 2018


On Mon, Jun 4, 2018 at 5:39 AM Matthew Harrigan <harrigan.matthew at gmail.com>
wrote:

> Should there be discussion of typing (pep-484) or abstract base classes in
> this nep?  Are there any requirements on the result returned by
> __array_function__?
>

This is a good question that should be addressed in the NEP. Currently, we
impose no limitations on the types returned by __array_function__ (or
__array_ufunc__, for that matter). Given the complexity of potential
__array_function__ implementations, I think this would be hard/impossible
to do in general.

I think the best case scenario we could hope for is that type checkers
would identify that result of NumPy functions as:
- numpy.ndarray if all inputs are numpy.ndarray objects
- Any if any non-numpy.ndarray inputs implement the __array_function__

Based on my understanding of proposed rules for typing protocols [1] and
overloads [2], I think this could just work, e.g.,

@overload
def func(array: np.ndarray) -> np.ndarray: ...
@overload
def func(array: ImplementsArrayFunction) -> Any: ...

[1] https://www.python.org/dev/peps/pep-0544/
[2] https://github.com/python/typing/issues/253#issuecomment-389262904
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180605/72e9f820/attachment.html>


More information about the NumPy-Discussion mailing list