0

I have some c++ code, and I successfully export it to python module using pybind11. The code is:

class A
{
public:
    A()
    {

    }

    /**
    * @brief explanation from c++
    */
    int GetWidth()
    {
        return width; 
    }

private:
    int width = 0; 
};


PYBIND11_MODULE(module, m) {


    m.doc() = "My Library Python Bindings";

    py::class_<A>(m, "A")
        .def(py::init<>())
        .def("GetWidth", &A::GetWidth, "explanation from pybind");

}

My question is: how to read the documentation for function in pycharm?

For example, when I use opencv, I can know the input for function. For example:

enter image description here

But, when I import my-self module, I don't know what is the input parameter.

For pybind11, how can I add the documentation for the exported module? In addition, I add lots of explanation for c++ function using the doxygen format. Is it possible to convert the c++ explanation to python function.

Any suggestion is appreciated~~~

0

1 Answer 1

0

you need to create a stub file, see What is the use of stub files (.pyi ) in python?.

a stub file just contains the definition of the function, for example here is the definition of the opencv function you are looking at in its stub file. __init__.pyi

@_typing.overload
def imread(filename: str, dst: cv2.typing.MatLike | None = ..., flags: int = ...) -> cv2.typing.MatLike: ...

the function body is an ellipsis ... you can also add a docstring in there.

pycharm can generate some stub files for extensions (with limitations) but the portable way is to provide your own stub file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.