diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-04-12 12:29:54 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-04-12 12:57:22 +0900 |
commit | 2e219366c433deb56fd3311a9245cdbabac05ca6 (patch) | |
tree | 4cb26f55908a8fc2e7a1c710234e134acdcea323 | |
parent | d8704c4f5823389c2ac220a161eaa07a7458da51 (diff) | |
download | sphinx-git-2e219366c433deb56fd3311a9245cdbabac05ca6.tar.gz |
Fix #7461: py domain: fails with IndexError for empty tuple in type annotation
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | sphinx/domains/python.py | 15 | ||||
-rw-r--r-- | tests/test_domain_py.py | 7 |
3 files changed, 19 insertions, 5 deletions
@@ -16,6 +16,8 @@ Features added Bugs fixed ---------- +* #7461: py domain: fails with IndexError for empty tuple in type annotation + Testing -------- diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 07a66fe22..19da49ac6 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -105,11 +105,16 @@ def _parse_annotation(annotation: str) -> List[Node]: result.append(addnodes.desc_sig_punctuation('', ']')) return result elif isinstance(node, ast.Tuple): - result = [] - for elem in node.elts: - result.extend(unparse(elem)) - result.append(addnodes.desc_sig_punctuation('', ', ')) - result.pop() + if node.elts: + result = [] + for elem in node.elts: + result.extend(unparse(elem)) + result.append(addnodes.desc_sig_punctuation('', ', ')) + result.pop() + else: + result = [addnodes.desc_sig_punctuation('', '('), + addnodes.desc_sig_punctuation('', ')')] + return result else: raise SyntaxError # unsupported syntax diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index cc3d3cf53..e330ba634 100644 --- a/tests/test_domain_py.py +++ b/tests/test_domain_py.py @@ -255,6 +255,13 @@ def test_parse_annotation(): [pending_xref, "int"], [desc_sig_punctuation, "]"])) + doctree = _parse_annotation("Tuple[()]") + assert_node(doctree, ([pending_xref, "Tuple"], + [desc_sig_punctuation, "["], + [desc_sig_punctuation, "("], + [desc_sig_punctuation, ")"], + [desc_sig_punctuation, "]"])) + doctree = _parse_annotation("Callable[[int, int], int]") assert_node(doctree, ([pending_xref, "Callable"], [desc_sig_punctuation, "["], |