sphinx-autodoc-typehints
sphinx-autodoc-typehints copied to clipboard
Annotations should perhaps not be rendered in the docs using `:py:class:`
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.
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.
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.
Please check again with latest release and report back if the problem is still active.
Please check again with latest release and report back if the problem is still active.
The issue still persists. Please reopen this issue. @gaborbernat
The type hints generated by sphinx.ext.autodoc or readthedocs/sphinx-autoapi do not have this issue.
Only sphinx-autodoc-typehints has this problem.
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.
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.
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)
What about adding to your css:
.sphinx_autodoc_typehints-type {
font-style: italic;
font-weight: normal !important;
}
It's just italicized, but still bold.
autodoc style:
sphinx-autodoc-typehints with your css code:
I think using the inline code block style may affect readability when there are many [ ].
I'm using PyData Sphinx Theme 0.16.1