mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #7462 from tk0miya/7461_empty_tuple
Fix #7461: py domain: fails with IndexError for empty tuple in type annotation
This commit is contained in:
commit
a4edbc1104
3
CHANGES
3
CHANGES
@ -47,6 +47,9 @@ Features added
|
|||||||
Bugs fixed
|
Bugs fixed
|
||||||
----------
|
----------
|
||||||
|
|
||||||
|
* #7461: py domain: fails with IndexError for empty tuple in type annotation
|
||||||
|
* #7461: autodoc: empty tuple in type annotation is not shown correctly
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
@ -114,11 +114,16 @@ def _parse_annotation(annotation: str) -> List[Node]:
|
|||||||
result.append(addnodes.desc_sig_punctuation('', ']'))
|
result.append(addnodes.desc_sig_punctuation('', ']'))
|
||||||
return result
|
return result
|
||||||
elif isinstance(node, ast.Tuple):
|
elif isinstance(node, ast.Tuple):
|
||||||
result = []
|
if node.elts:
|
||||||
for elem in node.elts:
|
result = []
|
||||||
result.extend(unparse(elem))
|
for elem in node.elts:
|
||||||
result.append(addnodes.desc_sig_punctuation('', ', '))
|
result.extend(unparse(elem))
|
||||||
result.pop()
|
result.append(addnodes.desc_sig_punctuation('', ', '))
|
||||||
|
result.pop()
|
||||||
|
else:
|
||||||
|
result = [addnodes.desc_sig_punctuation('', '('),
|
||||||
|
addnodes.desc_sig_punctuation('', ')')]
|
||||||
|
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
raise SyntaxError # unsupported syntax
|
raise SyntaxError # unsupported syntax
|
||||||
|
@ -114,7 +114,10 @@ def unparse(node: ast.AST) -> str:
|
|||||||
elif isinstance(node, ast.UnaryOp):
|
elif isinstance(node, ast.UnaryOp):
|
||||||
return "%s %s" % (unparse(node.op), unparse(node.operand))
|
return "%s %s" % (unparse(node.op), unparse(node.operand))
|
||||||
elif isinstance(node, ast.Tuple):
|
elif isinstance(node, ast.Tuple):
|
||||||
return ", ".join(unparse(e) for e in node.elts)
|
if node.elts:
|
||||||
|
return ", ".join(unparse(e) for e in node.elts)
|
||||||
|
else:
|
||||||
|
return "()"
|
||||||
elif sys.version_info > (3, 6) and isinstance(node, ast.Constant):
|
elif sys.version_info > (3, 6) and isinstance(node, ast.Constant):
|
||||||
# this branch should be placed at last
|
# this branch should be placed at last
|
||||||
return repr(node.value)
|
return repr(node.value)
|
||||||
|
@ -255,6 +255,13 @@ def test_parse_annotation():
|
|||||||
[pending_xref, "int"],
|
[pending_xref, "int"],
|
||||||
[desc_sig_punctuation, "]"]))
|
[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]")
|
doctree = _parse_annotation("Callable[[int, int], int]")
|
||||||
assert_node(doctree, ([pending_xref, "Callable"],
|
assert_node(doctree, ([pending_xref, "Callable"],
|
||||||
[desc_sig_punctuation, "["],
|
[desc_sig_punctuation, "["],
|
||||||
|
@ -54,6 +54,7 @@ from sphinx.pycode import ast
|
|||||||
("- 1", "- 1"), # UnaryOp
|
("- 1", "- 1"), # UnaryOp
|
||||||
("- a", "- a"), # USub
|
("- a", "- a"), # USub
|
||||||
("(1, 2, 3)", "1, 2, 3"), # Tuple
|
("(1, 2, 3)", "1, 2, 3"), # Tuple
|
||||||
|
("()", "()"), # Tuple (empty)
|
||||||
])
|
])
|
||||||
def test_unparse(source, expected):
|
def test_unparse(source, expected):
|
||||||
module = ast.parse(source)
|
module = ast.parse(source)
|
||||||
|
Loading…
Reference in New Issue
Block a user