Autolinks for type annotations

24 views
Skip to first unread message

Yury Gorishniy

unread,
Jun 24, 2020, 7:21:31 AM6/24/20
to sphinx-users
Hi! I have the following setup (the questions are at the end of the post):

sphinx==3.1.1 (also using sphinx.ext.napoleon and sphinx_autodoc_typehints(typehints_fully_qualified=True, always_document_param_types=True), but turning the extensions on/off changes literally nothing).
------------------
- docs
    - source
        - bar.rst
        - baz.rst
- foo
    - bar.py
    - baz.py
------------------

foo/bar.py:
class Bar:
   
"""..."""
   
pass

foo/baz.py:
from third_part import Qux
from .bar import Bar

def baz(x: Bar, y: Qux):
   
"""..."""
   
...

docs/source/baz.rst:
foo.baz
=======

.. currentmodule:: foo.baz

.. automodule:: foo.baz
   
:members:

The problem: everything is correctly parsed in displayed, but "Bar" and "Qux" in the signature of "baz" are not links, it is plain text. Intersphinx is correctly configured for "third_party".

1. I've seen somewhere that with third-party modules there are no ways to make this work. Is it correct?

2. Ok, even if true, is the problem also unsolvable even for .Bar, which is a part of my package?

I know that I can "solve" 1-2 by putting the types manually in the docstring, but the whole point is not to duplicate types in the signature and in the docstring.

3. A related (I think) question. Example: https://pytorch.org/docs/stable/nn.html#parameters We see that the argument "data" has the type "Tensor", which is a link(!).
In the source code, they don't do anything special, the just say "data (Tensor): ...". How did they do that?

Eric Larson

unread,
Jun 24, 2020, 9:39:04 AM6/24/20
to sphinx...@googlegroups.com
3. A related (I think) question. Example: https://pytorch.org/docs/stable/nn.html#parameters We see that the argument "data" has the type "Tensor", which is a link(!).
In the source code, they don't do anything special, the just say "data (Tensor): ...". How did they do that?

I think napoleon might take care of it. numpydoc can also be configured to try to do this, though it might require a bit more manual configuration (and requires using numpy-style docstrings).

Eric

CaptFugu

unread,
Jun 25, 2020, 6:03:04 AM6/25/20
to sphinx-users
Just for reference: I've asked practically the same thing here but got no response yet. I included an example repository if someone wants to have a go at this.
Reply all
Reply to author
Forward
0 new messages