NEP 13 should mention how we handle unknown scalar types
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:
- Assuming the unknown type properly returns
NotImplementedfrom binary operations for unrecognized arguments, the numpy ufunc will get called. - The NumPy ufunc (e.g.,
np.add) will coerce the unknown object into a scalar NumPy array with object dtype. - The scalar array gets broadcast to the same shape as the NumPy array.
- The NumPy ufunc loop (for object dtype) calls the Python operator on each element of the two arrays.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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