118

默认情况下,Sphinx 不会为 __init__(self) 生成文档。我尝试了以下方法:

.. automodule:: mymodule
    :members:

..autoclass:: MyClass
    :members:

在 conf.py 中,设置以下内容仅将 __init__(self) 文档字符串附加到类文档字符串(Sphinx 自动文档文档似乎同意这是预期的行为,但没有提及我要解决的问题):

autoclass_content = 'both'
4

6 回答 6

120

以下是三种选择:

  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)
    

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

  2. special-members选项是在 Sphinx 1.1 中添加的。它使“特殊”成员(具有类似名称的成员__special__)由 autodoc 记录。

    从 Sphinx 1.2 开始,此选项采用参数,使其比以前更有用。

  3. 使用automethod

    .. autoclass:: MyClass     
       :members: 
    
       .. automethod:: __init__
    

    这必须为每个类添加(不能与 一起使用automodule,正如对该答案第一版的评论中所指出的那样)。

于 2011-04-08T19:08:27.400 回答
84

你很亲密。您可以autoclass_content在文件中使用该选项conf.py

autoclass_content = 'both'
于 2012-03-19T15:25:06.833 回答
7

在过去的几年里,我autodoc-skip-member为各种不相关的 Python 项目编写了几种回调变体,因为我想要像__init__(),__enter__()__exit__()出现在我的 API 文档中的方法(毕竟,这些“特殊方法”是 API 的一部分,还有什么更好的地方可以记录它们而不是在特殊方法的文档字符串中)。

最近,我采用了最好的实现,并将其作为我的 Python 项目之一(这里是文档)的一部分。实现基本上归结为:

import types

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

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

于 2016-02-18T22:02:40.563 回答
6

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

例子:

autodoc_default_options = {
    'members': True,
    'member-order': 'bysource',
    'special-members': '__init__',
    'undoc-members': True,
    'exclude-members': '__weakref__'
}
于 2020-05-11T14:24:37.023 回答
1

只要此提交获得批准:https://github.com/sphinx-doc/sphinx/pull/9154,在下一个 sphinx 版本(>4.1.2)中就可以:

..autoclass:: MyClass1
    :members:
    :class-doc-from: "class"


..autoclass:: MyClass2
    :members:
    :class-doc-from: "init"
于 2021-08-17T13:45:32.427 回答
0

这是一个变体,只有__init__当它有参数时才包括:

import inspect

def skip_init_without_args(app, what, name, obj, would_skip, options):
    if name == '__init__':
        func = getattr(obj, '__init__')
        spec = inspect.getfullargspec(func)
        return not spec.args and not spec.varargs and not spec.varkw and not spec.kwonlyargs
    return would_skip

def setup(app):
    app.connect("autodoc-skip-member", skip_init_without_args)
于 2020-08-01T20:36:09.320 回答