Skip to content

np.bool_ is not autoconverted to C++ bool unlike other numpy numeric types #922

@anntzer

Description

@anntzer

Issue description

It looks like all numpy types are autoconverted to C++-level int/double/etc. as needed, with the exception of np.bool_. I'd think np.bool_ should likewise get autoconverted to a C++ bool.

Reproducible example code

Compiling the example repository pybind11/python_example with the following patch applied:

diff --git a/src/main.cpp b/src/main.cpp
index d81046d..8adbe25 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,6 +1,6 @@
 #include <pybind11/pybind11.h>

-int add(int i, int j) {
+int add(int i, bool j) {
     return i + j;
 }

Then

python_example.add(np.int64(1), True)

works, but

python_example.add(1, np.bool_(True))

fails with a TypeError.

(initially reported on gitter)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions