Skip to content

NEP 13 should mention how we handle unknown scalar types #12258

Open
@shoyer

Description

@shoyer

This section of NEP 13 describes how __array_ufunc__ interacts with Python's binary operations:
http://www.numpy.org/neps/nep-0013-ufunc-overrides.html#behavior-in-combination-with-python-s-binary-operations

But it doesn't fully describe what happens an object of unknown type that implements arithmetic (e.g., decimal.Decimal) but doesn't implement any NumPy special methods like __array_ufunc__ or __array_priority__ is encountered in arithmetic with a NumPy array:

  1. Assuming the unknown type properly returns NotImplemented from binary operations for unrecognized arguments, the numpy ufunc will get called.
  2. The NumPy ufunc (e.g., np.add) will coerce the unknown object into a scalar NumPy array with object dtype.
  3. The scalar array gets broadcast to the same shape as the NumPy array.
  4. The NumPy ufunc loop (for object dtype) calls the Python operator on each element of the two arrays.
  5. The result is another object dtype array.

If this seems correct, I will make a minor addendum to add this clarification. This is particularly valuable as an example to other projects (e.g., see pandas-dev/pandas#23293) of the right way to implement arithmetic like NumPy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions