Skip to content

recursive implicit conversion fails, although boost::python work at this situation #2182

@hzhangxyz

Description

@hzhangxyz

Issue description

recursive implicit conversion fails.

Reproducible example code

M.cpp:

#include <pybind11/pybind11.h>

struct S {
   S(int) {}
};

struct E {
   E(S) {}
};

struct T {
   T(E) {}
};

namespace py = pybind11;

PYBIND11_MODULE(M, m) {
   py::class_<S>(m, "S").def(py::init<int>());
   py::implicitly_convertible<int, S>();
   py::class_<E>(m, "E").def(py::init<S>());
   py::implicitly_convertible<S, E>();
   py::class_<T>(m, "T").def(py::init<E>());
}

main.py

from M import *
print("int -> S -> E -> T")
T(E(S(2333)))
print("int -> E -> T")
T(E(2333))
print("int -> S -> T")
T(S(2333))
print("int -> T")
T(2333)

what happen

the above three construction success but T(2333) fail. It say

int -> S -> E -> T
int -> E -> T
int -> S -> T
int -> T
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    T(2333)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. M.T(arg0: M.E)

Invoked with: 2333

environment

arch linux
gcc 9.3.0
python 3.8.2
pybind11 2.5.0

what else

BTW boost::python works well at this situation

M.cpp

#include <boost/python.hpp>

struct S {
   S(int) {}
};

struct E {
   E(S) {}
};

struct T {
   T(E) {}
};

namespace py = boost::python;

BOOST_PYTHON_MODULE(M) {
   py::class_<S>("S", py::no_init).def(py::init<int>());
   py::implicitly_convertible<int, S>();
   py::class_<E>("E", py::no_init).def(py::init<S>());
   py::implicitly_convertible<S, E>();
   py::class_<T>("T", py::no_init).def(py::init<E>());
}

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