-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs!: abbreviate annotations in API
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* cspell:ignore paren */ | ||
/* https://github.com/sphinx-doc/sphinx/issues/5868 */ | ||
span.sig-paren ~ em::before { | ||
content: "\a "; | ||
white-space: pre; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Abbreviated the annotations generated by sphinx-autodoc. | ||
It's not necessary to generate the full path of type hints, because they are | ||
rendered as clickable links. | ||
See also https://github.com/sphinx-doc/sphinx/issues/5868. | ||
""" | ||
|
||
# cspell:ignore docutils | ||
# pylint: disable=import-error | ||
# pyright: reportMissingImports=false | ||
import sphinx.domains.python | ||
from docutils import nodes | ||
from sphinx import addnodes | ||
from sphinx.environment import BuildEnvironment | ||
|
||
|
||
def replace_link(text: str) -> str: | ||
replacements = { | ||
"a set-like object providing a view on D's items": "typing.ItemsView", | ||
"a set-like object providing a view on D's keys": "typing.KeysView", | ||
"an object providing a view on D's values": "typing.ValuesView", | ||
"numpy.typing._array_like._SupportsArray": "numpy.typing.ArrayLike", | ||
"numpy.typing._dtype_like._DTypeDict": "numpy.typing.DTypeLike", | ||
"numpy.typing._dtype_like._SupportsDType": "numpy.typing.DTypeLike", | ||
"typing_extensions.Protocol": "typing.Protocol", | ||
} | ||
for old, new in replacements.items(): | ||
if text == old: | ||
return new | ||
return text | ||
|
||
|
||
def new_type_to_xref( | ||
text: str, env: BuildEnvironment = None | ||
) -> addnodes.pending_xref: | ||
"""Convert a type string to a cross reference node.""" | ||
if text == "None": | ||
reftype = "obj" | ||
else: | ||
reftype = "class" | ||
|
||
if env: | ||
kwargs = { | ||
"py:module": env.ref_context.get("py:module"), | ||
"py:class": env.ref_context.get("py:class"), | ||
} | ||
else: | ||
kwargs = {} | ||
|
||
text = replace_link(text) | ||
short_text = text.split(".")[-1] | ||
|
||
numpy_types = { | ||
"numpy.typing.ArrayLike", | ||
"numpy.typing.DTypeLike", | ||
} | ||
if text in numpy_types: | ||
text = "numpy.typing" | ||
reftype = "mod" | ||
|
||
return addnodes.pending_xref( | ||
"", | ||
nodes.Text(short_text), | ||
refdomain="py", | ||
reftype=reftype, | ||
reftarget=text, | ||
**kwargs, | ||
) | ||
|
||
|
||
sphinx.domains.python.type_to_xref = new_type_to_xref # noqa: F811 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters