如何使用 getter

时间:2021-04-26 13:03:14

标签: python python-sphinx python-typing

我正在使用 sphinx 从文档字符串和类型提示生成“自动化”文档。但是,我正在努力显示(在生成的文档中)带有 getter 的属性类型。


如果是简单的属性,我会得到所需的文档:

class Cls:
    def __init__(self):
        self.prop: int = 42

将生成此(所需的)文档:

propint


在方法的情况下,我们将得到类似有用的结果:

class Cls:
    def mth(self) -> int:
        return 42

mth() → int


但是,如果我们想使用 getter(@property 装饰器),我无法在那里获取类型:

class Cls:
    @property
    def prop(self) -> int:
        return 42

property prop

注意没有类型提示


你知道如何在最后一种情况下将类型提示添加到文档中吗?

1 个答案:

答案 0 :(得分:2)

这将在 sphinx 4 中修复。参见 this PRthis issue

Sphinx 4 尚未发布,但已经有 4.0.0b1 pre-release(现已发布)产生:

class Cls:
    @property
    def prop(self) -> int:
        return 42

property propint

如果它不会对您造成任何破坏,您可以使用此预发布版本。

相关问题