正如本文所述,即使 napoleon 将字段添加为文档,autodoc 也会急切地将类变量添加到文档中:
from dataclasses import dataclass
@dataclass
class Foo():
"""Some class
Attributes:
a: foo
b: bar
"""
a: str
b: int
def c(self) -> int:
"""Here's a doc'd function
"""
return 3
Run Code Online (Sandbox Code Playgroud)
我想明确告诉 autodoc 不要记录任何类变量(在数据类的情况下也是实例变量) - 我只希望 autodoc 显示给定类的声明函数并让 napoleon 处理它的所有类的类/实例变量发现. 这是否可以在没有:exclude-members:每堂课的情况下实现(这是一个巨大的麻烦)?
我已经尝试过:
autodoc_default_options = {
'members': True,
'undoc-members': False,
}
Run Code Online (Sandbox Code Playgroud)
在我的conf.py文件中.rst:
.. automodule:: some.module
:members:
:show-inheritance:
Run Code Online (Sandbox Code Playgroud)
这应该隐藏无证成员,但他们仍然出现:
我注意到 Sphinx 呈现类描述的行为发生了变化。鉴于此代码
# my example happens to be a dataclass, but the behavior for
# regular classes is the same
@dataclass
class TestClass:
"""This is a test class for dataclasses.
This is the body of the docstring description.
"""
var_int: int
var_str: str
Run Code Online (Sandbox Code Playgroud)
加上一些通用的狮身人面像设置,我大约两年前就得到了这个
现在我得到了这个
有没有办法告诉 Sphinx 不要将类变量添加到类定义的底部?尤其令人烦恼的是,它假设它们的值为None,只是因为它们没有默认值。
这个问题是在这篇文章的讨论中出现的,其中还包含有关 Sphinx 配置等的评论中的更多上下文。
python python-sphinx autodoc sphinx-napoleon python-dataclasses
我正在尝试记录一个具有一些类级成员变量的 Python 类,但我无法使用 reST/Sphinx 对其进行适当记录。
代码是这样的:
class OSM:
"""Some blah and examples"""
url = 'http://overpass-api.de/api/interpreter' # URL of the Overpass API
sleep_time = 10 # pause between successive queries when assembling OSM dataset
Run Code Online (Sandbox Code Playgroud)
但我得到了这个输出(请参见绿色圆圈区域,我希望在其中有一些描述这两个变量的文本,如上所述)。

对于模糊之处,我深表歉意,但示例的一部分有些敏感
restructuredtext docstring python-sphinx autodoc sphinx-napoleon
我有一个mod带有一些子模块的模块submod,并用于.. automodule:: mod.submod为其生成文档。
模块中元素(函数、类等)的签名现在显示限定名称,例如mod.submod.my_function(*args, **kwargs).
我希望 Sphinx 仅显示函数的名称,即签名my_function(*args, **kwargs)。
我有什么办法可以删除签名中的主要模块和子模块吗?