mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
refactor: py domain: Add type_to_xref() utility
This commit is contained in:
parent
c13ecd2437
commit
8f4c2fb08d
@ -77,17 +77,19 @@ ModuleEntry = NamedTuple('ModuleEntry', [('docname', str),
|
|||||||
('deprecated', bool)])
|
('deprecated', bool)])
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
def _parse_annotation(annotation: str) -> List[Node]:
|
def _parse_annotation(annotation: str) -> List[Node]:
|
||||||
"""Parse type annotation."""
|
"""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=reftype, reftarget=text)
|
|
||||||
|
|
||||||
def unparse(node: ast.AST) -> List[Node]:
|
def unparse(node: ast.AST) -> List[Node]:
|
||||||
if isinstance(node, ast.Attribute):
|
if isinstance(node, ast.Attribute):
|
||||||
return [nodes.Text("%s.%s" % (unparse(node.value)[0], node.attr))]
|
return [nodes.Text("%s.%s" % (unparse(node.value)[0], node.attr))]
|
||||||
@ -133,10 +135,10 @@ def _parse_annotation(annotation: str) -> List[Node]:
|
|||||||
result = unparse(tree)
|
result = unparse(tree)
|
||||||
for i, node in enumerate(result):
|
for i, node in enumerate(result):
|
||||||
if isinstance(node, nodes.Text):
|
if isinstance(node, nodes.Text):
|
||||||
result[i] = make_xref(str(node))
|
result[i] = type_to_xref(str(node))
|
||||||
return result
|
return result
|
||||||
except SyntaxError:
|
except SyntaxError:
|
||||||
return [make_xref(annotation)]
|
return [type_to_xref(annotation)]
|
||||||
|
|
||||||
|
|
||||||
def _parse_arglist(arglist: str) -> addnodes.desc_parameterlist:
|
def _parse_arglist(arglist: str) -> addnodes.desc_parameterlist:
|
||||||
|
Loading…
Reference in New Issue
Block a user