sphinx-autodoc-typehints icon indicating copy to clipboard operation
sphinx-autodoc-typehints copied to clipboard

Annotations should perhaps not be rendered in the docs using `:py:class:`

Open anntzer opened this issue 8 years ago • 10 comments

Consider the apidocs, using sphinx-autodoc-typehints, of

def f1(x):
    """
    A function.

    :param int x: The x.
    """

def f2(x: int):
    """
    A function.

    :param x: The x.
    """

By default, for f1, int is rendered in italic while for f2, it is rendered in bold. This is due to the fact that sphinx-autodoc-typehint adds a :py:class: in front of the typename.

While this may be semantically correct, this is inconsistent with sphinx's default recommendation (http://www.sphinx-doc.org/en/stable/domains.html#info-field-lists), so I think this should be off by default.

anntzer avatar Aug 28 '17 06:08 anntzer

I think it'd make sense to not add the :class: for builtins, since they're not going to link anyway for pretty much every project using Sphinx.

auscompgeek avatar Jan 01 '18 02:01 auscompgeek

Revisiting this, it looks like Sphinx tries to link unannotated types automatically, but it won't look in any intersphinx docs.

It also turns out the Python docs actually has an intersphinx database as well, so I guess this behaviour should be configurable.

auscompgeek avatar Jan 01 '19 10:01 auscompgeek

Please check again with latest release and report back if the problem is still active.

gaborbernat avatar Jan 08 '22 11:01 gaborbernat

Please check again with latest release and report back if the problem is still active.

The issue still persists. Please reopen this issue. @gaborbernat

vivodi avatar Mar 23 '25 14:03 vivodi

The type hints generated by sphinx.ext.autodoc or readthedocs/sphinx-autoapi do not have this issue.

Only sphinx-autodoc-typehints has this problem.

vivodi avatar Mar 23 '25 14:03 vivodi

We have a fixed list of things that we label with :py:data: and otherwise we label them with :py:class:: role = "data" if (module, class_name) in _PYDATA_ANNOTATIONS else "class"

For intersphinx stuff, we could probably look up the role in the intersphinx database. I think for internal references, we can't tell the role since it might not be available when we're generating the docstrings.

But you are suggesting that int should be rendered in italic when a class in that location would be bold? To me, this looks a bit ugly and inconsistent. I think it's better if all parts of every type are rendered in a consistent type face.

hoodmane avatar Mar 24 '25 14:03 hoodmane

I think both int and classes should be italicized rather than bold. This is the behavior of sphinx.ext.autodoc, and I find it more aesthetically pleasing and readable.

vivodi avatar Mar 24 '25 15:03 vivodi

For example, without using sphinx-autodoc-typehints, autodoc displays type annotations as "parameter (Callable[[Entry], None] | int)", whereas with sphinx-autodoc-typehints, they are shown as "parameter (Callable[[Entry], None] | int)".

I prefer the original autodoc display style and believe that sphinx-autodoc-typehints should remain consistent with it.

original autodoc style: parameter (Callable[[Entry], None] | int)

sphinx-autodoc-typehints style: parameter (Callable[[Entry], None] | int)

vivodi avatar Mar 24 '25 15:03 vivodi

What about adding to your css:

.sphinx_autodoc_typehints-type {
    font-style: italic;
    font-weight: normal !important;
}

hoodmane avatar Mar 24 '25 15:03 hoodmane

It's just italicized, but still bold.

autodoc style:

Image

sphinx-autodoc-typehints with your css code:

Image

I think using the inline code block style may affect readability when there are many [ ].

I'm using PyData Sphinx Theme 0.16.1

vivodi avatar Mar 24 '25 15:03 vivodi