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

发布于 2024-08-03 21:10:34 字数 317 浏览 11 评论 0原文

Sphinx 有一个名为 automethod 的功能,可以从方法的文档字符串中提取文档并将其嵌入到文档中。但它不仅嵌入了文档字符串,还嵌入了方法签名(名称+参数)。如何嵌入文档字符串(不包括方法签名)?

参考: http://www.sphinx-doc.org/ en/master/usage/extensions/autodoc.html

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2024-08-10 21:10:34

我认为您正在寻找的是:

from sphinx.ext import autodoc

class DocsonlyMethodDocumenter(autodoc.MethodDocumenter):
  def format_args(self):
    return None

autodoc.add_documenter(DocsonlyMethodDocumenter)

根据当前来源< /a> 这应该允许覆盖负责记录方法的类(旧版本的 add_documenter 禁止此类覆盖,但现在明确允许)。当然,让 format_args 返回 None 是 autodoc 中记录的方式,表示“不用理会签名”。

我认为这是执行此任务的干净、架构化的方式,因此比猴子补丁替代方案更可取。如果您需要使用一些旧版本的sphinx,但是您可能确实需要monkeypatch(autodoc.MethodDocumenter.format_args=lambda _:None - eek!-)如果在您的特定部署中可行的话,我建议将 sphinx 升级到当前版本作为更好的方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文