summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-04-12 12:29:54 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-04-12 12:57:22 +0900
commit2e219366c433deb56fd3311a9245cdbabac05ca6 (patch)
tree4cb26f55908a8fc2e7a1c710234e134acdcea323
parentd8704c4f5823389c2ac220a161eaa07a7458da51 (diff)
downloadsphinx-git-2e219366c433deb56fd3311a9245cdbabac05ca6.tar.gz
Fix #7461: py domain: fails with IndexError for empty tuple in type annotation
-rw-r--r--CHANGES2
-rw-r--r--sphinx/domains/python.py15
-rw-r--r--tests/test_domain_py.py7
3 files changed, 19 insertions, 5 deletions
diff --git a/CHANGES b/CHANGES
index 84d3c2669..31bc95082 100644
--- a/CHANGES
+++ b/CHANGES
@@ -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, "["],