python - 如何让 Sphinx 使用来自 Python stub 文件的类型注释

标签 python annotations python-sphinx stub python-typing

我使用 stub 文件(即“*.pyi”文件)来管理我项目中的类型注释。我的文档字符串在实际的源文件中。我已经安装了 sphinx-autodoc-annotation让 Sphinx 查看类型注释,但不查看 stub 文件。通过 Sphinx 生成文档时,如何从 stub 文件中提取类型?

foo.pyi

class Foo:
    def bar(self, baz: str) -> str:
        ...

foo.py
class Foo:
    def bar(self, baz):
        """Does some cool stuff

        :param baz: some parameter that we do stuff with
        """

        return baz

最佳答案

我使用 stub 文件是因为我想避免类型信息使我的 Python 源文件困惑。然而,最简单的解决方案(目前)似乎是无论如何都将类型信息放入源文件并取消 stub 文件。

关于python - 如何让 Sphinx 使用来自 Python stub 文件的类型注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68079405/

相关文章:

python - 如何在python中实现一个好的__hash__函数

python - 存储字典值的路径

groovy - 如何访问 groovy 闭包注释?

python - Sphinx 代码块中的替换不会被替换

python - 尝试使用Python使用MagickWand方法给图像加水印

python - 使用 python 的 Firestore 中的 GeoPoint

java - Spring 中的注解周围

java - Kotlin 自定义注释、参数

image - 将 sphinx 指向构建目录中的图像

python - 如何使用 Sphinx 的 autodoc 记录嵌套类?