python-sphinx - 仅显示带有 autodoc 的函数签名?

python-sphinx - Display only function signature with autodoc?

在 Sphinx 中,可以使用 py:function(或 py:method)指令手动包含函数或方法的签名:

.. py:function:: my_func(data, named=None, *args, *kwargs)

也可以使用 autodoc 指令来包含和格式化函数或方法的整个文档字符串:

.. automethod:: my_func

我想知道是否有一种方法可以配置 autodoc 以仅包含和格式化签名,而不包含其余的文档字符串,这样我就不必手动进行了。

请参阅 autodoc 的 sphinx.ext.autodoc.between

Return a listener that either keeps, or if exclude is True excludes, lines between lines that match the marker regular expression. If no line matches, the resulting docstring would be empty, so no change will be made unless keepempty is true.

If what is a sequence of strings, only docstrings of a type in what will be processed.

这里也可以使用autodoc-process-signature。

def process_signature(app, what, name, obj, options, signature, return_annotation):
    return modified_signature, modified_return_annotation
    # will be rendered to method(modified_signature) -> modified_return_annotation

def setup(app):
    app.connect("autodoc-process-signature", process_signature)

http://www.sphinx-doc.org/en/master/_modules/sphinx/ext/autodoc.html