You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is a feature request to prepend docstrings with all of the overload signatures of the function (unless options.disable_function_signatures() has been called).
To use an example from the pybind11 documentation:
Furthermore, I think it needs to be possible to have all of the function signatures at the start of the docstring generated but without the section headings. This would allow a user to write the whole docstring as one, but still have the function signatures generated for Sphinx and/or stubgen.
So it should be possible to generate a docstring like this:
I think the second change, to generate the signatures without signature headings, would require the addition of a new option. To maintain current behaviour, the options.disable_function_signatures() function could still turn all signatures off. A new option disable_signature_headings() would turn only the section headings off.
So the following code would produce the docstring seen above:
py::options options;
options.disable_signature_headings();
py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &, int>())
.def("set", (void (Pet::*)(int)) &Pet::set)
.def("set", (void (Pet::*)(const std::string &)) &Pet::set, "Set attributes on the pet.\n\nWhen arg0 is an integer, the pet's age is set.\nWhen arg0 is a string, the pet's name is set.");
The text was updated successfully, but these errors were encountered:
Sphinx can nowadays nicely parse overload chains if all function
signatures are printed at the beginning of the docstring. This change
therefore moves signatures to the beginning to improve Sphinx output.
When an overload chain specifies multiple overload-specific docstrings,
another copy of the signature (in ``code quotes``) is inserted later on
for context.
This change resembles a PR in the pybind11 repository
(pybind/pybind11#2619).
This issue follows on from a discussion at python/mypy#9070
Current Behaviour
Currently, when a function is overloaded in pybind, the docstring generated includes a single, generic type signature at the top (
name(*args, **kwargs) -> Any
).Sphinx autodoc will document the signature of the function using this generic type signature (https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_docstring_signature). However it recently added support for reading multiple type signatures from the docstring (sphinx-doc/sphinx#2106). This is apparently an already established convention and it's used in CPython source code (https://github.com/python/cpython/blob/c0f22fb8b3006936757cebb959cee94e285bc503/Objects/rangeobject.c#L156).
Furthermore, stubgen (https://mypy.readthedocs.io/en/stable/stubgen.html) uses this generic function signature, which makes the type stubs that it generates useless (this was the original issue discussed in python/mypy#9070).
New Behaviour
This issue is a feature request to prepend docstrings with all of the overload signatures of the function (unless
options.disable_function_signatures()
has been called).To use an example from the pybind11 documentation:
This produces docstrings that looks like the following:
This request is to have the docstrings instead look like the following:
Furthermore, I think it needs to be possible to have all of the function signatures at the start of the docstring generated but without the section headings. This would allow a user to write the whole docstring as one, but still have the function signatures generated for Sphinx and/or stubgen.
So it should be possible to generate a docstring like this:
Implementation Details
I think the second change, to generate the signatures without signature headings, would require the addition of a new option. To maintain current behaviour, the
options.disable_function_signatures()
function could still turn all signatures off. A new optiondisable_signature_headings()
would turn only the section headings off.So the following code would produce the docstring seen above:
The text was updated successfully, but these errors were encountered: