diff options
Diffstat (limited to 'sphinx/domains/python.py')
-rw-r--r-- | sphinx/domains/python.py | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 0f3f089a7..f348a6f3a 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -74,17 +74,19 @@ ModuleEntry = NamedTuple('ModuleEntry', [('docname', str), ('deprecated', bool)]) -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' +def type_to_xref(text: str) -> addnodes.pending_xref: + """Convert a type string to a cross reference node.""" + if text == 'None': + reftype = 'obj' + else: + reftype = 'class' - return pending_xref('', nodes.Text(text), - refdomain='py', reftype=reftype, reftarget=text) + return pending_xref('', nodes.Text(text), + refdomain='py', reftype=reftype, reftarget=text) + +def _parse_annotation(annotation: str) -> List[Node]: + """Parse type annotation.""" def unparse(node: ast.AST) -> List[Node]: if isinstance(node, ast.Attribute): return [nodes.Text("%s.%s" % (unparse(node.value)[0], node.attr))] @@ -130,10 +132,10 @@ def _parse_annotation(annotation: str) -> List[Node]: result = unparse(tree) for i, node in enumerate(result): if isinstance(node, nodes.Text): - result[i] = make_xref(str(node)) + result[i] = type_to_xref(str(node)) return result except SyntaxError: - return [make_xref(annotation)] + return [type_to_xref(annotation)] def _parse_arglist(arglist: str) -> addnodes.desc_parameterlist: @@ -590,7 +592,7 @@ class PyVariable(PyObject): typ = self.options.get('type') if typ: - signode += addnodes.desc_annotation(typ, ': ' + typ) + signode += addnodes.desc_annotation(typ, '', nodes.Text(': '), type_to_xref(typ)) value = self.options.get('value') if value: @@ -750,7 +752,7 @@ class PyAttribute(PyObject): typ = self.options.get('type') if typ: - signode += addnodes.desc_annotation(typ, ': ' + typ) + signode += addnodes.desc_annotation(typ, '', nodes.Text(': '), type_to_xref(typ)) value = self.options.get('value') if value: |