[BUG] NumPy fixed width integer types don't cast correctly
Open
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
NumPy fixed width integer types don't cast correctly.
Reproducible example code
#include <cstdint>
#include "pybind11/pybind11.h"
namespace pb11 = pybind11;
template<typename T>
T x2(T v)
{
return v + v;
}
PYBIND11_MODULE(OverloadTest, m)
{
m.def("x2", &x2<std::uint8_t>);
m.def("x2", &x2<std::uint16_t>);
m.def("x2", &x2<std::uint32_t>);
m.def("x2", &x2<std::uint64_t>);
}
import numpy as np
import OverloadTest as ot
print(ot.x2(np.uint64(200)))
which outputs
>> 144
So it seems like the np.uint64 is getting cast to the std::uint8_t type instead of the std::uint64_t type.
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
Start by compiling and running the reproducible C++ OverloadTest module with the Python np.uint64 example. Inspect pybind11's overload and fixed-width integer casting behavior, then verify that the uint8_t, uint16_t, uint32_t, and uint64_t overloads select the matching NumPy integer types and return the expected results.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, numpy, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100