简体   繁体   English

如何在 Sphinx 中标记文档字符串以进行条件包含?

[英]How can I tag a docstring for conditional inclusion in Sphinx?

I have extensive docstrings in my Python package classes and methods that are brought into Sphinx using the autodoc directive to generate the project docs.我的 Python 包类和方法中有大量的文档字符串,它们使用 autodoc 指令导入 Sphinx 以生成项目文档。

Some of the external (not starting with underscore) methods are API methods that should appear in the user documentation.一些外部(不以下划线开头)方法是应出现在用户文档中的 API 方法。 Others are external because they need to be called from another module, but make up an internal API.其他是外部的,因为它们需要从另一个模块调用,但构成了一个内部API。 These should not appear in the user documentation.这些不应出现在用户文档中。

So far, I have manually distinguished user API methods from internal API methods using the :members: parameter.到目前为止,我已经使用 :members: 参数手动区分了用户 API 方法和内部 API 方法。 This turns out to be error prone as I add new methods and I would like to indicate right in the docstring whether the method should appear in user API docs.事实证明,当我添加新方法时,这很容易出错,我想在文档字符串中指明该方法是否应出现在用户 API 文档中。

Is there a way I can "tag" the docstring or something like that to appear directly in the source code to indicate it should appear in user API docs?有没有办法可以“标记”文档字符串或类似的东西直接出现在源代码中,以表明它应该出现在用户 API 文档中?

Looks like you want to exclude members from the autodoc?看起来您想从 autodoc 中排除成员?

.. automodule:: yourmodule
   :inherited-members:        # include inherited functions
   :special-members:          # include __functions__
   :private-members:          # include _functions and __functions
   :members:                  # include all other documented functions
   :undoc-members:            # include even undocumented functions
   :exclude-members: f_1, f_2 # exclude certain members

To work out the condional documentation more programmatically, you'd edit the conf.py file and override the slot autodoc-skip-member like explained here .要以更编程的方式制定条件文档,您需要编辑 conf.py 文件并覆盖插槽 autodoc-skip-member ,如解释here

def skip(app, what, name, obj, skip, options):
    return name in ["exclude1", "exclude2", "exclude3"]

def setup(app):
    app.connect("autodoc-skip-member", skip)

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

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