Python:Sphinx 命名元组文档

Art*_*tem 3 python python-sphinx autodoc

我正在尝试记录namedtuple。当我构建文档时,我WARNING: duplicate object description在记录的函数之后收到警告和相同的空函数。例如:在此输入图像描述

如何删除这些别名?我已经尝试过这个解决方案,编写了一些函数来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
...
Run Code Online (Sandbox Code Playgroud)

小智 7

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

只需在你的 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)
Run Code Online (Sandbox Code Playgroud)

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

解释

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

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