How to export the same type in two different submodules with `pybind11`?

Viewed 26

In my C++ library I have two namespaces that share some symbol:

namespace NS1 {
  struct S1 { };
}

namespace NS2 { 
  using NS1::S1;

  struct S2 { };
}

I would like to export two submodule of my Python module with the same structure, i.e. having the submodule NS1 export S1, and NS2 export both S1 and S2, with S1 being the same type in both modules.

I tried this way:

PYBIND11_MODULE(my_module, m) {

  py::module_ m1 = m.def_submodule("NS1", "");
  py::module_ m2 = m.def_submodule("NS2", "");

  py::class_<NS1::S1>(m1, "S1");
  py::class_<NS2::S1>(m2, "S1");
  py::class_<NS2::S2>(m2, "S2");
}

But this does not work:

Traceback (most recent call last):
  File "/test-python.py", line 3, in <module>
    from my_module import *
ImportError: generic_type: type "S1" is already registered!

How can I export the same type from two different submodules?

0 Answers
Related