Skip to content

Defining overrides for aliased NumPy functions in NumPy #13877

Closed

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__.

Activity

  1. rgommers commented on Jul 1, 2019

    @rgommers
    Member

    Nothing -- the error message is clear enough. Dask can implement amin/amax easily enough.

    That's forcing API pollution though ....

    For abs we could switch around the names and make absolute the alias. EDIT: actually we could do that for all of those

  2. eric-wieser commented on Jul 1, 2019

    @eric-wieser
    Member
  3. shoyer commented on Jul 1, 2019

    @shoyer
    MemberAuthor

    I agree that it would make sense to reverse the aliases for min/max/round. Certainly these names are much more commonly used.

    I agree that we can probably leave absolute as is.

  4. rgommers commented on Mar 25, 2020

    @rgommers
    Member

    We discussed this in the triage meeting:

    • preferred indeed to reverse the aliases
    • don't want to deprecate the aliases
    • the issue in Dask was solved without namespace pollution

    So just switch the aliases and close this seems like the way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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