mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
py nodes, PyObject anno
This commit is contained in:
parent
d16d9a6661
commit
01ef6a437f
@ -124,6 +124,12 @@ def _parse_annotation(annotation: str, env: BuildEnvironment = None) -> List[Nod
|
||||
elif isinstance(node, ast.Constant): # type: ignore
|
||||
if node.value is Ellipsis:
|
||||
return [addnodes.desc_sig_punctuation('', "...")]
|
||||
elif isinstance(node.value, bool):
|
||||
return [addnodes.desc_sig_keyword('', repr(node.value))]
|
||||
elif isinstance(node.value, int):
|
||||
return [addnodes.desc_sig_literal_number('', repr(node.value))]
|
||||
elif isinstance(node.value, str):
|
||||
return [addnodes.desc_sig_literal_string('', repr(node.value))]
|
||||
else:
|
||||
return [nodes.Text(repr(node.value))]
|
||||
elif isinstance(node, ast.Expr):
|
||||
@ -513,7 +519,9 @@ class PyObject(ObjectDescription[Tuple[str, str]]):
|
||||
|
||||
anno = self.options.get('annotation')
|
||||
if anno:
|
||||
signode += addnodes.desc_annotation(' ' + anno, ' ' + anno)
|
||||
signode += addnodes.desc_annotation(' ' + anno, '',
|
||||
addnodes.desc_sig_space(),
|
||||
nodes.Text(anno))
|
||||
|
||||
return fullname, prefix
|
||||
|
||||
|
@ -18,7 +18,7 @@ from docutils import nodes
|
||||
from sphinx import addnodes
|
||||
from sphinx.addnodes import (desc, desc_addname, desc_annotation, desc_content, desc_name,
|
||||
desc_optional, desc_parameter, desc_parameterlist, desc_returns,
|
||||
desc_sig_keyword,
|
||||
desc_sig_keyword, desc_sig_literal_number, desc_sig_literal_string,
|
||||
desc_sig_name, desc_sig_operator, desc_sig_punctuation,
|
||||
desc_signature, desc_sig_space, pending_xref)
|
||||
from sphinx.domains import IndexEntry
|
||||
@ -354,22 +354,22 @@ def test_parse_annotation_Literal(app):
|
||||
doctree = _parse_annotation("Literal[True, False]", app.env)
|
||||
assert_node(doctree, ([pending_xref, "Literal"],
|
||||
[desc_sig_punctuation, "["],
|
||||
"True",
|
||||
[desc_sig_keyword, "True"],
|
||||
[desc_sig_punctuation, ","],
|
||||
desc_sig_space,
|
||||
"False",
|
||||
[desc_sig_keyword, "False"],
|
||||
[desc_sig_punctuation, "]"]))
|
||||
|
||||
doctree = _parse_annotation("typing.Literal[0, 1, 'abc']", app.env)
|
||||
assert_node(doctree, ([pending_xref, "typing.Literal"],
|
||||
[desc_sig_punctuation, "["],
|
||||
"0",
|
||||
[desc_sig_literal_number, "0"],
|
||||
[desc_sig_punctuation, ","],
|
||||
desc_sig_space,
|
||||
"1",
|
||||
[desc_sig_literal_number, "1"],
|
||||
[desc_sig_punctuation, ","],
|
||||
desc_sig_space,
|
||||
"'abc'",
|
||||
[desc_sig_literal_string, "'abc'"],
|
||||
[desc_sig_punctuation, "]"]))
|
||||
|
||||
|
||||
@ -555,7 +555,8 @@ def test_pydata_signature_old(app):
|
||||
doctree = restructuredtext.parse(app, text)
|
||||
assert_node(doctree, (addnodes.index,
|
||||
[desc, ([desc_signature, ([desc_name, "version"],
|
||||
[desc_annotation, " = 1"])],
|
||||
[desc_annotation, (desc_sig_space,
|
||||
"= 1")])],
|
||||
desc_content)]))
|
||||
assert_node(doctree[1], addnodes.desc, desctype="data",
|
||||
domain="py", objtype="data", noindex=False)
|
||||
|
Loading…
Reference in New Issue
Block a user