From 51e7b6d2aa871c262e37f3fc9edbef67a2b3edd2 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sat, 25 Sep 2021 12:52:52 +0200 Subject: [PATCH] py nodes, PyClassLike, get_signature_prefix --- sphinx/domains/python.py | 7 ++++--- tests/test_domain_py.py | 27 +++++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py index 999a00dcf..f042274a7 100644 --- a/sphinx/domains/python.py +++ b/sphinx/domains/python.py @@ -714,11 +714,12 @@ class PyClasslike(PyObject): allow_nesting = True - def get_signature_prefix(self, sig: str) -> str: + def get_signature_prefix(self, sig: str) -> List[nodes.Node]: if 'final' in self.options: - return 'final %s ' % self.objtype + return [nodes.Text('final'), addnodes.desc_sig_space(), + nodes.Text(self.objtype), addnodes.desc_sig_space()] else: - return '%s ' % self.objtype + return [nodes.Text(self.objtype), addnodes.desc_sig_space()] def get_index_text(self, modname: str, name_cls: Tuple[str, str]) -> str: if self.objtype == 'class': diff --git a/tests/test_domain_py.py b/tests/test_domain_py.py index 36b327854..84a918b90 100644 --- a/tests/test_domain_py.py +++ b/tests/test_domain_py.py @@ -520,7 +520,7 @@ def test_pyexception_signature(app): text = ".. py:exception:: builtins.IOError" doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "exception "], + [desc, ([desc_signature, ([desc_annotation, ('exception', desc_sig_space)], [desc_addname, "builtins."], [desc_name, "IOError"])], desc_content)])) @@ -583,7 +583,7 @@ def test_pyobject_prefix(app): " .. py:method:: FooBar.say") doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ('class', desc_sig_space)], [desc_name, "Foo"])], [desc_content, (addnodes.index, desc, @@ -653,11 +653,14 @@ def test_pyclass_options(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_name, "Class1"])], [desc_content, ()])], addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "final class "], + [desc, ([desc_signature, ([desc_annotation, ("final", + desc_sig_space, + "class", + desc_sig_space)], [desc_name, "Class2"])], [desc_content, ()])])) @@ -693,7 +696,7 @@ def test_pymethod_options(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_name, "Class"])], [desc_content, (addnodes.index, desc, @@ -786,7 +789,7 @@ def test_pyclassmethod(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_name, "Class"])], [desc_content, (addnodes.index, desc)])])) @@ -807,7 +810,7 @@ def test_pystaticmethod(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_name, "Class"])], [desc_content, (addnodes.index, desc)])])) @@ -830,7 +833,7 @@ def test_pyattribute(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_name, "Class"])], [desc_content, (addnodes.index, desc)])])) @@ -868,7 +871,7 @@ def test_pyproperty(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_name, "Class"])], [desc_content, (addnodes.index, desc, @@ -932,7 +935,7 @@ def test_canonical(app): domain = app.env.get_domain('py') doctree = restructuredtext.parse(app, text) assert_node(doctree, (addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_addname, "io."], [desc_name, "StringIO"])], desc_content)])) @@ -990,7 +993,7 @@ def test_info_field_list(app): assert_node(doctree, (nodes.target, addnodes.index, addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_addname, "example."], [desc_name, "Class"])], [desc_content, nodes.field_list, nodes.field])])) @@ -1081,7 +1084,7 @@ def test_info_field_list_piped_type(app): (nodes.target, addnodes.index, addnodes.index, - [desc, ([desc_signature, ([desc_annotation, "class "], + [desc, ([desc_signature, ([desc_annotation, ("class", desc_sig_space)], [desc_addname, "example."], [desc_name, "Class"])], [desc_content, nodes.field_list, nodes.field, (nodes.field_name,