diff --git a/CHANGES b/CHANGES index 77a901e853c..c363a75b204 100644 --- a/CHANGES +++ b/CHANGES @@ -46,6 +46,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 diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 67b52c187e2..55545f4d274 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -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' diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index eeb4f78a643..d986b62f334 100644 --- a/tests/test_domain_py.py +++ b/tests/test_domain_py.py @@ -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, "["], @@ -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): @@ -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.')