如何使用Sphinx的autodoc来记录类的__init __(self)方法?

Jac*_*ble 88 python python-sphinx autodoc

默认情况下,Sphinx不会为__init __(self)生成文档.我尝试过以下方法:

.. automodule:: mymodule
    :members:
Run Code Online (Sandbox Code Playgroud)

..autoclass:: MyClass
    :members:
Run Code Online (Sandbox Code Playgroud)

在conf.py中,设置以下内容只会将__init __(self)docstring附加到类docstring(Sphinx autodoc文档似乎同意这是预期的行为,但没有提到我正在尝试解决的问题):

autoclass_content = 'both'
Run Code Online (Sandbox Code Playgroud)

mzj*_*zjn 98

以下是三种选择:

  1. 为了确保__init__()始终记录,您可以autodoc-skip-member在conf.py中使用.像这样:

    def skip(app, what, name, obj, would_skip, options):
        if name == "__init__":
            return False
        return would_skip
    
    def setup(app):
        app.connect("autodoc-skip-member", skip)
    
    Run Code Online (Sandbox Code Playgroud)

    这明确定义__init__不要跳过(默认情况下).此配置指定一次,并且不需要为.rst源中的每个类添加任何其他标记.

  2. special-members选项已添加到Sphinx 1.1中.它使"特殊"成员(名称相似的成员__special__)由autodoc记录.

    从Sphinx 1.2开始,这个选项采用的参数使它比以前更有用.

  3. 用途automethod:

    .. autoclass:: MyClass     
       :members: 
    
       .. automethod:: __init__
    
    Run Code Online (Sandbox Code Playgroud)

    必须为每个类添加(不能使用automodule,如本答案的第一个修订的注释中所指出的).

  • 在Sphinx 1.2.1中,`special-members`使用`automodule`工作正常.使用`:special-members:__ init__`来仅记录`__init__`. (8认同)
  • 这对自动模块没有帮助,因为它必须添加到每个类. (7认同)
  • 在Sphinx 1.2中,[special-members](http://sphinx-doc.org/ext/autodoc.html)选项可以采用参数,使选项2比以前更具吸引力. (4认同)
  • 为了将来参考,我认为最好颠倒上市订单,这样每个人都可以轻松地看到什么是最佳解决方案. (3认同)
  • 第一种选择有效.在我的情况下,它比第二和第三种选择更好,因为它不需要编辑.rst文件. (2认同)

got*_*nes 58

你很亲密 您可以autoclass_contentconf.py文件中使用该选项:

autoclass_content = 'both'
Run Code Online (Sandbox Code Playgroud)

  • 这不是他说他试过的吗? (7认同)
  • 这对我很有用,我希望我的__init__参数文档显示在类文档部分中,并且这样做了.谢谢! (3认同)
  • 我赞成,因为它帮助了我. (2认同)
  • 我尝试设置 `autoclass_content = 'both'` 选项,该选项确实记录了 __init__ 方法,但它使自动摘要出现两次。 (2认同)
  • 这是最好的方法,它与自动摘要完美配合,结果比“特殊方法”好得多,前者在类的开头添加构造函数文档,后者添加单独的“__init__”方法给医生。 (2认同)

dhe*_*inz 15

尽管这是一篇较旧的文章,但对于那些现在正在查找它的人来说,1.8 版本中还引入了另一种解决方案。根据文档,您可以将 special-members中的密钥添加autodoc_default_options到您的conf.py.

例子:

autodoc_default_options = {
    'members': True,
    'member-order': 'bysource',
    'special-members': '__init__',
    'undoc-members': True,
    'exclude-members': '__weakref__'
}
Run Code Online (Sandbox Code Playgroud)


xol*_*lox 5

在过去的几年中,我autodoc-skip-member为各种不相关的Python项目编写了多种回调类型,因为我希望使用这类方法__init__()__enter__()并将__exit__()其显示在我的API文档中(毕竟,这些“特殊方法”是API的一部分,还有什么更好的地方记录它们,而不是在特殊方法的文档字符串中记录它们。

最近,我采用了最佳的实现,并将其纳入了我的Python项目之一(此处是文档)。实现基本上可以归结为:

def setup(app):
    """Enable Sphinx customizations."""
    enable_special_methods(app)


def enable_special_methods(app):
    """
    Enable documenting "special methods" using the autodoc_ extension.

    :param app: The Sphinx application object.

    This function connects the :func:`special_methods_callback()` function to
    ``autodoc-skip-member`` events.

    .. _autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
    """
    app.connect('autodoc-skip-member', special_methods_callback)


def special_methods_callback(app, what, name, obj, skip, options):
    """
    Enable documenting "special methods" using the autodoc_ extension.

    Refer to :func:`enable_special_methods()` to enable the use of this
    function (you probably don't want to call
    :func:`special_methods_callback()` directly).

    This function implements a callback for ``autodoc-skip-member`` events to
    include documented "special methods" (method names with two leading and two
    trailing underscores) in your documentation. The result is similar to the
    use of the ``special-members`` flag with one big difference: Special
    methods are included but other types of members are ignored. This means
    that attributes like ``__weakref__`` will always be ignored (this was my
    main annoyance with the ``special-members`` flag).

    The parameters expected by this function are those defined for Sphinx event
    callback functions (i.e. I'm not going to document them here :-).
    """
    if getattr(obj, '__doc__', None) and isinstance(obj, (types.FunctionType, types.MethodType)):
        return False
    else:
        return skip
Run Code Online (Sandbox Code Playgroud)

是的,文档比逻辑更多:-)。autodoc-skip-member相对于使用special-members选项(对我而言)定义这样的回调的优点在于,该special-members选项还可以记录属性__weakref__(例如,在所有新样式类AFAIK上可用)的文档,而我认为这些属性根本没有用。回调方法避免了这种情况(因为它仅适用于函数/方法,而忽略其他属性)。