带有toctree的sphinx autosummary也列出了导入的成员

我使用Sphinx和autosummary来生成Python软件的文档。 它运行良好,但生成的.rst文件还列出了导入的函数和类,这不是我想要的行为。

例如一个带有docstring的包“packageex”:

"""
Package Example (:mod:`packageex`)
==================================

.. currentmodule:: packageex
.. autosummary::
   :toctree:

   module0
   module1
"""

会产生一个文件packageex.module0.rst

Module0 (:mod:`packageex.module0`)
=================================

.. currentmodule:: packageex.module0

.. rubric:: Functions

.. autosummary::

   f0
   f1
   f2_imported
   f3_imported

.. rubric:: Classes

.. autosummary::

   Class0
   ClassImported

有没有办法只列出模块中定义的函数和类(而不是那些导入的)?

在autodoc文档(http://sphinx-doc.org/latest/ext/autodoc.html)中,有“在设置了成员选项的automodule指令中,只有模块成员的__module__属性等于模块名称这是为了防止导入的类或函数的文档,如果你想阻止这种行为并且记录所有可用的成员,请设置imported-members选项,注意来自导入模块的属性不会被记录,因为通过解析当前模块的源文件来发现属性文档。“ 是否可以使用自动摘要来获取相同的行为?


正如mzjn所提到的,它似乎是扩展自动摘要的已知奇怪行为。 为了获得想要的行为(即阻止导入对象的列表),我刚刚修改了函数get_members (sphinx.ext.autosummary.generate的l。166),如下所示:

def get_members(obj, typ, include_public=[], imported=False):
    items = []
    for name in dir(obj):
        try:
            obj_name = safe_getattr(obj, name)
            documenter = get_documenter(obj_name, obj)
        except AttributeError:
            continue
        if documenter.objtype == typ:
            try:
                cond = (
                    imported or 
                    obj_name.__module__ == obj.__name__
                    )
            except AttributeError:
                cond = True
            if cond:
                items.append(name)
    public = [x for x in items
              if x in include_public or not x.startswith('_')]
    return public, items
链接地址: http://www.djcxy.com/p/84125.html

上一篇: sphinx autosummary with toctree also lists imported members

下一篇: Sphinx class attribute documentation