首页 > 解决方案 > 如何让 Sphinx 使用 Python 存根文件中的类型注释

问题描述

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

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

标签: pythonannotationspython-sphinxstubpython-typing

解决方案


我使用存根文件是因为我想避免 Python 源文件与类型信息混淆。然而,最简单的解决方案(目前)似乎是将类型信息放入源文件并取消存根文件。


推荐阅读