numpy: Overloads between scalars and arrays can produce confusing results
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Relates https://github.com/numpy/numpy/issues/10404 - can transplant the main info here if need be
Given how NumPy permits converting arrays of size 1 to scalars (regardless of dimension), this can create confusing interplay between overloads of an array and a scalar numeric value, especially if the array type is meant to be implicitly convertible from int or float.
Reproduction code:
sm.def("overload_scalar", [](py::array_t<float>) { return "Vector"; });
sm.def("overload_scalar", [](int) { return "Int"; });
def test_overload_scalar():
assert m.overload_scalar(0) == "Int"
assert m.overload_scalar([0]) == "Vector"
assert m.overload_scalar(np.array([0.])) == "Int" # What???
As a workaround, a user could define some wrapper type, like scalar_only<T>, and an accompanying type_converter<scalar_only<T>>, which explicitly rejects any containers that are iterable.
Regarding how to solve this, I unfortunately do not have any good ideas (though it would be nice if/when https://github.com/numpy/numpy/pull/10615 lands).
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
No source file or test is named. Start with the C++/Python reproduction in the issue and trace pybind11's overload resolution and array/scalar conversion paths; review NumPy issue #10404 and PR #10615 for constraints. Done means the behavior and conversion policy are settled for the shown integer, list, and NumPy-array cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100