pybind / pybind/pybind11

[BUG] NumPy fixed width integer types don't cast correctly

Open
#2,815 5 comments 0 reactions 0 assignees View on GitHub

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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.