Sphinx has a feature called automethod
that extracts the documentation from a method's docstring and embeds that into the documentation. But it not only embeds the docstring, but also the method signature (name + arguments). How do I embed only the docstring (excluding the method signature)?
ref: http://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
Docstrings are accessible from the doc attribute (__doc__) for any of the Python objects and also with the built-in help() function. An object's docstring is defined by including a string constant as the first statement in the object's definition.
The Sphinx docstring formatA pair of :param: and :type: directive options must be used for each parameter we wish to document. The :raises: option is used to describe any errors that are raised by the code, while the :return: and :rtype: options are used to describe any values returned by our code.
All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. Public methods (including the __init__ constructor) should also have docstrings.
I think what you're looking for is:
from sphinx.ext import autodoc
class DocsonlyMethodDocumenter(autodoc.MethodDocumenter):
def format_args(self):
return None
autodoc.add_documenter(DocsonlyMethodDocumenter)
per the current sources this should allow overriding what class is responsible for documenting methods (older versions of add_documenter
forbade such overrides, but now they're explicitly allowed). Having format_args
return None, of course, is THE documented way in autodoc
to say "don't bother with the signature".
I think this is the clean, architected way to perform this task, and, as such, preferable to monkeypatching alternatives. If you need to live with some old versions of sphinx
however you may indeed have to monkeypatch (autodoc.MethodDocumenter.format_args=lambda _:None
-- eek!-) though I would recommend upgrading sphinx
to the current version as a better approach if at all feasible in your specific deployment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With