简体   繁体   English

在 Sphinx 文档中显示 *only* 文档字符串?

[英]Show *only* docstring in Sphinx documentation?

Sphinx has a feature called automethod that extracts the documentation from a method's docstring and embeds that into the documentation. Sphinx 有一个称为automethod的功能,它从方法的文档字符串中提取文档并将其嵌入到文档中。 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参考: http : //www.sphinx-doc.org/en/master/usage/extensions/autodoc.html

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).根据当前来源,这应该允许覆盖负责记录方法的类(旧版本的add_documenter禁止此类覆盖,但现在明确允许它们)。 Having format_args return None, of course, is THE documented way in autodoc to say "don't bother with the signature".当然,让format_args返回 None 是autodoc记录的方式,可以说“不要打扰签名”。

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.如果您需要使用一些旧版本的sphinx但是您可能确实需要使用monkeypatch ( autodoc.MethodDocumenter.format_args=lambda _:None -- eek!-) 虽然我建议将sphinx升级到当前版本作为更好的方法,如果在您的特定部署中完全可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM