Search code examples
c++pybind11docstring

how to make pybind11 property docstring to show up?


I have following code:

py::class_<Logger> logger(m, "Logger");
logger.doc() = "class docstring";
logger.def("setLevels", [](uint16_t levels)
    {
        LoggerInstance.setLevels(levels);
    },
    R"(
        Turns on/off different logging levels
        Parameters
        * levels Logging level flags
    )"
    );

//expose values as constant fields
logger.def_property_readonly_static("LOG_NONE", [](py::object /* self */){return LOG_NONE;}, "no log level turned on");
logger.def_property_readonly_static("LOG_INFO", [](py::object /* self */){return LOG_INFO;}, "info level logs turned on");
logger.def_property_readonly_static("LOG_DEBUG", [](py::object /* self */){return LOG_DEBUG;}, "debug level logs turned on");
logger.def_property_readonly_static("LOG_WARN", [](py::object /* self */){return LOG_WARN;}, "warning logs turned on");
logger.def_property_readonly_static("LOG_ERROR", [](py::object /* self */){return LOG_ERROR;}, "error logs turned on");

According to this answer, this is the correct way for passing docstring. But when I check it in python (3.6)

help(Logger)

I get empty data descriptors:

enter image description here

How could I get the docstrings to show up in python help?


Solution

  • I think it's a bug (see here). If you can change the C++ source you could try to make those members non-static and then use def_property_readonly (for which docstrings show up, at least in my tests).