diff --git a/CHANGES b/CHANGES index 84d3c266944..31bc95082b7 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 07a66fe22a7..19da49ac644 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -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 diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index cc3d3cf5364..e330ba6349c 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, "["],