我研究了这个主题,但找不到明确的解决方案。有一个类似的SO问题
我的问题是我有一个带有注释的类attr.dataclass
,typing_extensions.final
我不希望记录它们,但我仍然想从如何调用该类的角度来描述该类。
例如,
@final
@dataclass(frozen=True, slots=True)
class Casting(object):
_int_converter_function = int
_float_converter_function = float
def __call__(self, casting, value_to_cast):
if casting['type'] == 'integer':
return self._int_converter_function(value_to_cast)
return self._float_converter_function(value_to_cast)
Run Code Online (Sandbox Code Playgroud)
这大约相当于这个(远不准确):
class Casting(object):
def __init__(
self,
int_converter_function = int,
float_converter_function = float,
):
self.int_converter_function = int_converter_function
self.float_converter_function = float_converter_function
def converter(self, casting, value):
self.value = value
yield
type = casting['type']
if type == 'integer':
yield self.int_converter_function(value)
else:
yield self.float_converter_function(value)
Run Code Online (Sandbox Code Playgroud)
最新的情况很明显,我可以使用文档字符串和Sphinx
do 来记录每个方法:
.. autoclass:: package.Casting
:members:
.. automethod:: …
Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以以与方法和函数文档字符串相同的方式自动生成数据类的文档字符串?我通过帮助/搜索没有找到任何有用的东西
from dataclasses import dataclass
@dataclass
class ExtractionConfig:
"""
"""
gcp_bucket: str = None
gcp_key: str = None
log_file: str = None
log_backup_count: int = 3
delete_remotely: bool = True
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