numpy / numpy/numpy

NEP 13 should mention how we handle unknown scalar types

Open
#12,258 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
32.8k
Forks
12.8k
Avg merge
1d 7h
Merged PRs (30d)
197

Description

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 https://github.com/pandas-dev/pandas/pull/23293) of the right way to implement arithmetic like NumPy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the NEP 13 section on behavior in combination with Python's binary operations at the linked URL, and verify the described behavior for unknown scalar types such as decimal.Decimal. Done means the NEP includes a concise clarification of this interaction, subject to confirming the proposed sequence with NumPy maintainers.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.