Python:Sphinx namedtuple 文档

标签 python python-sphinx autodoc

我正在尝试记录 namedtuple。当我构建文档时,我收到警告 WARNING: duplicate object description 和在记录的函数之后相同的空函数。例如:enter image description here

如何删除那些别名? 我已经试过了 this solution , 向 conf.py 写入一些函数来创建空属性。

此外,我认为值得一提的是,在构建之后我得到了使用 :noindex: 的注释,但我不明白我应该在哪里使用它?在我的文档字符串、第一个文件或其他地方?

代码示例:


File = namedtuple("File", ["path", "size", "extension",
                           "adate", "mdate", "links",
                           "user_owner", "group_owner",
                           "inode", "device", "permissions",
                           "depth"])
"""File attributes.

.. py:attribute:: path

    **-** path to the found file

     .. note::
        depending on command-line arguments can be absolute or relative
...

最佳答案

我遇到了一个我认为比接受的答案更好的解决方案,只是想分享一下:

只需在您的 conf.py 中写入:

# -- Post process ------------------------------------------------------------
import collections
def remove_namedtuple_attrib_docstring(app, what, name, obj, skip, options):
    if type(obj) is collections._tuplegetter:
        return True
    return skip


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

这会从所有 NamedTuple 类中删除使用此“字段别名 ..”自动记录的所有参数。

解释

这使用 autodoc 事件 autodoc-skip-member 每次遇到任何类型的成员时都会触发一个处理程序

  • app.connect('autodoc-skip-member', remove_namedtuple_attrib_docstring) 将处理程序 remove_namedtuple_attrib_docstring 附加到事件
  • 如果成员是 tuplegetter,则处理程序返回 true,否则返回默认的跳过值

关于Python:Sphinx namedtuple 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61572220/

相关文章:

python-asyncio - 使用 Sphinx autodoc 记录 Python 协程

python - AES-GCM 模式的正确 nonce/iv 大小

python - 特征提取 Tensorflow Estimator

markdown - 如何在 Mkdocs 或 Sphinx 中创建选项卡式代码块?

python-2.7 - 使用 Sphinx autodoc 记录 Flask 应用程序

python - restructedText 内联文字中的冒号

python - sphinx-apidoc 获取子模块,但 autodoc 没有记录它们

python - 如何在 Sphinx 的 Python 文档字符串中指定变量类型?

python "instance has no attribute ..."

python - 以编程方式从共享 Dropbox 文件夹链接下载内容