Skip to content

Defining overrides for aliased NumPy functions in NumPy #13877

@shoyer

Description

@shoyer

There are handful of NumPy functions that are implemented as aliases of other NumPy function. These include at least min, max, round (non-ufuncs) and abs (an alias of the np.absolute ufunc):

from .fromnumeric import amax as max, amin as min, round_ as round
from .numeric import absolute as abs

Because np.min is defined as amin, that's also how you have to override the function with __array_function__. This leads to surprising behaviors for libraries like dask, which wrote its __array_function__ method to alias numpy functions to dask/cupy functions of the same name: because dask.array.amin does not exist, numpy.min (an alias for numpy.amin) doesn't work: dask/dask#5031

Things we could do about this on the NumPy side:

  1. Nothing -- the error message is clear enough. Dask can implement amin/amax easily enough.
  2. Define separate wrapper functions, so they can be overridden by the name users specify. Interestingly this is already the case for round_, which is a wrapper around around.

I think option (2) makes sense for min, max and round. It's not a great option for abs, since that would mean it no longer goes through __array_ufunc__.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions