diff options
| author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-05-02 22:41:29 +0900 |
|---|---|---|
| committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-05-04 14:29:55 +0900 |
| commit | 8f4c2fb08df49e13aab32bbac7eb87963f1536bd (patch) | |
| tree | 8391d87289b016f5117917729602fa622cff7962 /sphinx/domains | |
| parent | c13ecd243709d1e210a030be5aa09b7714e35730 (diff) | |
| download | sphinx-git-8f4c2fb08df49e13aab32bbac7eb87963f1536bd.tar.gz | |
refactor: py domain: Add type_to_xref() utility
Diffstat (limited to 'sphinx/domains')
| -rw-r--r-- | sphinx/domains/python.py | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 39c7de142..9b34d27c8 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -77,17 +77,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))] @@ -133,10 +135,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: |
