sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
238 stars 49 forks source link

Could not generate stubs signature when a method returns an object from a class in a namespace #107

Closed sunavlis closed 1 year ago

sunavlis commented 1 year ago

I try to generate a stub file from a C++ class which is wrapped with pybind11. My class is in a namespace and returns an object from a enum class.

Here is an example of the class:

namespace Test {
    enum class test_e {
        testA,    // I_Data
        testB,    // I_Raw
        testC     // I_Vol
    };
    class TestClass {
    private:
        test_e testMember;
    public:
        TestClass() : testMember(test_e::testA) { }
        test_e getTestMember() { return testMember; }
    };
}

Which is wrapped in a PYBIND11_MODULE with the following code:

  py::class_<Test::TestClass, std::shared_ptr<Test::TestClass>> testClass(m, "TestClass");
  testClass.def(py::init<>())
      .def("getTestMember", &Test::TestClass::getTestMember)
  ;
  py::enum_<Test::test_e>(testClass, "test_e")
      .value("testA", Test::test_e::testA)
      .value("testB", Test::test_e::testB)
      .value("testC", Test::test_e::testC)
  ;

It compiles as expected and works in my Python script. But the stubs generation is not working and returns the following error:

  Generated stubs signature is degraded to `(*args, **kwargs) -> typing.Any` for
  def getTestMember(self: py_lib.TestClass) -> Test::test_e: ...
                                                    ^-- Invalid syntax

ass soon as I put the test_e enum definition outside of the Test namespace, the stub generation works as expected.

Do I miss something in the definition or implementation? Or is it a bug in the stubgen implementation?

sizmailov commented 1 year ago

The pybind11-stubgen log contains a link to https://pybind11.readthedocs.io/en/stable/advanced/misc.html?highlight=docstring#avoiding-c-types-in-docstrings which should help you solve the problem.

sunavlis commented 1 year ago

Thanks for pointing me to the right documentation. It solved the problem.