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 authored and AA-Turner committed Dec 29, 2021
1 parent 052f643 commit e49a254
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -84,6 +84,7 @@ Bugs fixed
with Python 3.10
* #9968: autodoc: instance variables are not shown if __init__ method has
position-only-arguments
* #9194: autodoc: types under the "typing" 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
4 changes: 3 additions & 1 deletion 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 All @@ -367,7 +369,7 @@ def test_parse_annotation_suppress(app):
desc_sig_space,
[pending_xref, "str"],
[desc_sig_punctuation, "]"]))
assert_node(doctree[0], pending_xref, refdomain="py", reftype="class", reftarget="typing.Dict")
assert_node(doctree[0], pending_xref, refdomain="py", reftype="obj", reftarget="typing.Dict")


@pytest.mark.skipif(sys.version_info < (3, 8), reason='python 3.8+ is required.')
Expand Down

0 comments on commit e49a254

Please sign in to comment.