Search code examples
implicit-conversionpybind11

pybind11 implicitly_convertible does not work


I am struggling to make the simplest example work. Here is my code:

// example.cpp

#include <pybind11/pybind11.h>
namespace py = pybind11;

class B {
public:
    int b;
};

class A {
public:
    int a;
    A(int a) :a(a) {}
    A(B b) { a = b.b; }
};

void fn(A) {}

PYBIND11_MODULE(example, m) {

    py::class_<A>(m, "A")
        .def(
            py::init<int>(),
            py::arg("a") = 1
        );

    py::class_<B>(m, "B")
        .def(
            py::init<int>(),
            py::arg("b") = 2
        );

    py::implicitly_convertible<A, B>();

    m.def("fn", &fn,
        py::arg("a")
    );
}
# test.py

from example import *

a = A()
b = B()

fn(b)

It builds fine, but the output is:

$ python3.9 test.py 
Traceback (most recent call last):
  File "/pybindtest/test.py", line 8, in <module>
    fn(b)
TypeError: fn(): incompatible function arguments. The following argument types are supported:
    1. (a: example.A) -> None

Invoked with: <example.B object at 0x7fc3016a83b0>

I thought I have implemented the demo case that is described here: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#implicit-conversions

I also tried to add this to the init of A: .def(py::init<B>()) but no luck.

What am I missing here? Thanks in advance!

Solution:

Turns out it has to be py::implicitly_convertible<B, A>(); (so A and B swapped) and the .def(py::init<B>()) is also needed.


Solution

  • You mixed up the order of the template arguments passed to py::implicitly_convertible. It should be py::implicitly_convertible<convert_from, convert_to>, which in your case should be

    py::implicitly_convertible<B, A>