Skip to content

Commit

Permalink
Fix sphinx-doc#9194: autodoc: types in typing module are not hyperlinked
Browse files Browse the repository at this point in the history
This converts types in typing module to valid references when
`autodoc_unqualified_typehints` option enabled.
  • Loading branch information
tk0miya committed Dec 20, 2021
1 parent 8becfb8 commit da3f818
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -43,6 +43,7 @@ Bugs fixed
with Python 3.10
* #9968: autodoc: instance variables are not shown if __init__ method has
position-only-arguments
* #9194: autodoc: types in types module are not hyperlinked
* #9947: i18n: topic directive having a bullet list can't be translatable
* #9878: mathjax: MathJax configuration is placed after loading MathJax itself
* #9857: Generated RFC links use outdated base url
Expand Down
3 changes: 2 additions & 1 deletion sphinx/domains/python.py
Expand Up @@ -83,7 +83,8 @@ class ModuleEntry(NamedTuple):
def type_to_xref(target: str, env: BuildEnvironment = None, suppress_prefix: bool = False
) -> addnodes.pending_xref:
"""Convert a type string to a cross reference node."""
if target == 'None':
if target == 'None' or target.startswith('typing.'):
# typing module provides non-class types. Obj reference is good to refer them.
reftype = 'obj'
else:
reftype = 'class'
Expand Down
2 changes: 2 additions & 0 deletions tests/test_domain_py.py
Expand Up @@ -348,6 +348,7 @@ def test_parse_annotation(app):
assert_node(doctree, ([pending_xref, "None"],))
assert_node(doctree[0], pending_xref, refdomain="py", reftype="obj", reftarget="None")

# Literal type makes an object-reference (not a class reference)
doctree = _parse_annotation("typing.Literal['a', 'b']", app.env)
assert_node(doctree, ([pending_xref, "typing.Literal"],
[desc_sig_punctuation, "["],
Expand All @@ -356,6 +357,7 @@ def test_parse_annotation(app):
desc_sig_space,
[desc_sig_literal_string, "'b'"],
[desc_sig_punctuation, "]"]))
assert_node(doctree[0], pending_xref, refdomain="py", reftype="obj", reftarget="typing.Literal")


def test_parse_annotation_suppress(app):
Expand Down

0 comments on commit da3f818

Please sign in to comment.