Search code examples
pythonpybind11

How to have Pybind11 positional-argument names show up on help()?


double foo(int x, int y, double dx, double dy) {...};

PYBIND11_MODULE(mymodule, m) {
    m.doc() = "Module docstring";
    m.def("foo", &foo, "Function docstring");
}

Once installed, the help pages don't include the positional argument name and instead replaced with arg<#>:

>>> from mymodule import foo
>>> help(foo)

Help on built-in function foo in module mymodule:

foo(...) method of builtins.PyCapsule instance
    foo(arg0: int, arg1: int, arg2: float, arg3: float) -> float

    Function docstring

Solution

  • You need to pass their names. Here are two versions of how to do it:

    double foo(int x, int y, double dx, double dy) {...};
    
    PYBIND11_MODULE(mymodule, m) {
        m.doc() = "Module docstring";
        // First version:
        using namespace pybind11::literals;
        m.def("foo", &foo, "Function docstring", "x"_a, "y"_a, "dx"_a, "dy"_a);
        // Second version:
        m.def("foo", &foo, "Function docstring", py::arg("x"), py::arg("y"), py::arg("dx"), py::arg("dy"));
    }
    

    The two versions are identical - the first one is syntactic sugar for the second one that uses user defined literals.