Skip to content

Commit e326ad6

Browse files
committed
Correctly parse and cross-reference unpacked type annotations
1 parent 78bb5a1 commit e326ad6

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

sphinx/domains/python/_annotations.py

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def unparse(node: ast.AST) -> list[Node]:
124124
return [nodes.Text(repr(node.value))]
125125
if isinstance(node, ast.Expr):
126126
return unparse(node.value)
127+
if isinstance(node, ast.Starred):
128+
result = [addnodes.desc_sig_operator('', '*')]
129+
result.extend(unparse(node.value))
130+
return result
127131
if isinstance(node, ast.Invert):
128132
return [addnodes.desc_sig_punctuation('', '~')]
129133
if isinstance(node, ast.USub):

sphinx/pycode/ast.py

+3
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,8 @@ def visit_Tuple(self, node: ast.Tuple) -> str:
202202
else:
203203
return '(' + ', '.join(self.visit(e) for e in node.elts) + ')'
204204

205+
def visit_Starred(self, node: ast.Starred) -> str:
206+
return f'*{self.visit(node.value)}'
207+
205208
def generic_visit(self, node: ast.AST) -> NoReturn:
206209
raise NotImplementedError('Unable to parse %s object' % type(node).__name__)

tests/test_domains/test_domain_py.py

+22
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,28 @@ def test_parse_annotation(app):
508508
),
509509
)
510510

511+
doctree = _parse_annotation('*tuple[str, int]', app.env)
512+
assert_node(
513+
doctree,
514+
(
515+
[desc_sig_operator, '*'],
516+
[pending_xref, 'tuple'],
517+
[desc_sig_punctuation, '['],
518+
[pending_xref, 'str'],
519+
[desc_sig_punctuation, ','],
520+
desc_sig_space,
521+
[pending_xref, 'int'],
522+
[desc_sig_punctuation, ']'],
523+
),
524+
)
525+
assert_node(
526+
doctree[1],
527+
pending_xref,
528+
refdomain='py',
529+
reftype='class',
530+
reftarget='tuple',
531+
)
532+
511533

512534
@pytest.mark.sphinx('html', testroot='_blank')
513535
def test_parse_annotation_suppress(app):

tests/test_pycode/test_pycode_ast.py

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
'x[:, np.newaxis, :, :]'), # Index, Subscript, numpy extended syntax
6363
('y[:, 1:3][np.array([0, 2, 4]), :]',
6464
'y[:, 1:3][np.array([0, 2, 4]), :]'), # Index, 2x Subscript, numpy extended syntax
65+
('*tuple[str, int]', '*tuple[str, int]'), # Starred
6566
],
6667
) # fmt: skip
6768
def test_unparse(source, expected):

0 commit comments

Comments
 (0)