Skip to content

Commit

Permalink
Fix #7461: py domain: fails with IndexError for empty tuple in type a…
Browse files Browse the repository at this point in the history
…nnotation
  • Loading branch information
tk0miya committed Apr 12, 2020
1 parent d8704c4 commit 2e21936
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -16,6 +16,8 @@ Features added
Bugs fixed
----------

* #7461: py domain: fails with IndexError for empty tuple in type annotation

Testing
--------

Expand Down
15 changes: 10 additions & 5 deletions sphinx/domains/python.py
Expand Up @@ -105,11 +105,16 @@ def unparse(node: ast.AST) -> 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
Expand Down
7 changes: 7 additions & 0 deletions tests/test_domain_py.py
Expand Up @@ -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, "["],
Expand Down

0 comments on commit 2e21936

Please sign in to comment.