sphinx
sphinx copied to clipboard
Add richer mark up to type hints in description
trafficstars
When having an object with type hints, e.g. like this function:
def f(x: typing.Union[float, str]):
...
The type hint is converted to the following HTML code in the signature of the function (with autodoc_typehints_format = 'short')
<span class="n">
<span class="pre">Union</span>
<span class="p">
<span class="pre">[</span>
</span>
<span class="pre">float</span>
<span class="p">
<span class="pre">,</span>
</span>
<span class="w"> </span>
<span class="pre">str</span>
<span class="p">
<span class="pre">]</span>
</span>
</span>
whereas if the type hints should be stored as part of the description in the docstring with autodoc_typehints = 'description' only the follwing HTML code is returned containing the type hints:
<em>Union</em>
<em>[</em>
<em>float</em>
<em>, </em>
<em>str</em>
<em>]</em>
This makes it impossible to apply some CSS styles to the type hints.
Would be nice if in the case of description, also some span elements could be added.
Here is an example output when using the third party sphinx-autodoc-typehints plugin to store type hints in the description:
<code class="xref py py-data docutils literal notranslate">
<span class="pre">Union</span>
</code>
[
<code class="xref py py-data docutils literal notranslate">
<span class="pre">float</span>
</code>
", "
<code class="xref py py-data docutils literal notranslate">
<span class="pre">str</span>
</code>
]