I'll mention one other option that I've been contemplating recently, a bit of a hybrid of solutions 1 and 2:
- We could build a new library with dispatchable functions inside NumPy itself, e.g., "numpy.api."
Functions in numpy.api
work just like those in numpy, with two critical differences:
- They support overloading, via some to be determined mechanism.
- They don't coerce unknown types to
np.array()
using__array__()
.
This approach has a number of advantages over adjusting existing NumPy functions:
- Backwards compatibility. For any particular numpy function without overloads, there is assuredly existing code that relies on it always coercing to numpy arrays. Every time we add a new overload in NumPy (e.g., np.any() recently), we've seen things break in downstream libraries like pandas. Even if we require downstream libraries to opt-in (e.g., by implementing
__array_ufunc__
) that just pushes the breakage downstream. - Predictability. We can remove any uncertainty over whether a NumPy function s