From d9d381de11ee02512bd85d4982449923919968d1 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 10 Apr 2020 02:07:02 +0900 Subject: [PATCH] Fix #7445: a return annotation ``None`` is not converted to a hyperlink --- CHANGES | 2 ++ sphinx/domains/python.py | 7 ++++++- tests/test_domain_py.py | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index e0923b433ca..835dca01833 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,8 @@ Bugs fixed ---------- * #7428: py domain: a reference to class ``None`` emits a nitpicky warning +* #7445: py domain: a return annotation ``None`` in the function signature is + not converted to a hyperlink when using intersphinx * #7418: std domain: duplication warning for glossary terms is case insensitive * #7438: C++, fix merging overloaded functions in parallel builds. * #7422: autodoc: fails with ValueError when using autodoc_mock_imports diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 893bb0a22dd..07a66fe22a7 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -71,8 +71,13 @@ def _parse_annotation(annotation: str) -> List[Node]: """Parse type annotation.""" def make_xref(text: str) -> addnodes.pending_xref: + if text == 'None': + reftype = 'obj' + else: + reftype = 'class' + return pending_xref('', nodes.Text(text), - refdomain='py', reftype='class', reftarget=text) + refdomain='py', reftype=reftype, reftarget=text) def unparse(node: ast.AST) -> List[Node]: if isinstance(node, ast.Attribute): diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index dc3d51d0a64..cc3d3cf5364 100644 --- a/tests/test_domain_py.py +++ b/tests/test_domain_py.py @@ -239,6 +239,7 @@ def test_get_full_qualified_name(): def test_parse_annotation(): doctree = _parse_annotation("int") assert_node(doctree, ([pending_xref, "int"],)) + assert_node(doctree[0], pending_xref, refdomain="py", reftype="class", reftarget="int") doctree = _parse_annotation("List[int]") assert_node(doctree, ([pending_xref, "List"], @@ -266,6 +267,12 @@ def test_parse_annotation(): [pending_xref, "int"], [desc_sig_punctuation, "]"])) + # None type makes an object-reference (not a class reference) + doctree = _parse_annotation("None") + assert_node(doctree, ([pending_xref, "None"],)) + assert_node(doctree[0], pending_xref, refdomain="py", reftype="obj", reftarget="None") + + def test_pyfunction_signature(app): text = ".. py:function:: hello(name: str) -> str"