Search code examples
pythonpython-sphinxpython-typingstub

How to get Sphinx to use type annotations from Python stub files


I use stub files (i.e. "*.pyi" files) to manage type annotations in my project. My docstrings are in the actual source files. I've installed sphinx-autodoc-annotation to get Sphinx to look at type annotations, but it doesn't look at the stub files. When generating the documentation via Sphinx, how does one pull the typing from the stub files?

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

Solution

  • I was using stub files because I wanted to avoid cluttering my Python source files with type information. However, the simplest solution (for now) seems to be to put the type information into the source files anyway and do away with the stub files.