From b75f248345c002f6dc9534370e22715630f7d4d3 Mon Sep 17 00:00:00 2001 From: theOehrly Date: Thu, 24 Mar 2022 16:27:59 +0100 Subject: [PATCH 1/3] fix: no links generated in summary table (#65, #67) --- autodocsumm/__init__.py | 25 ++++++++++++++----- tests/test-root/dummy_submodule/__init__.py | 0 tests/test-root/dummy_submodule/submodule1.py | 9 +++++++ tests/test-root/dummy_submodule/submodule2.py | 9 +++++++ tests/test-root/index.rst | 2 ++ tests/test-root/test_class_submodule.rst | 4 +++ tests/test-root/test_module_submodule.rst | 4 +++ tests/test_autodocsumm.py | 22 ++++++++++++++++ 8 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 tests/test-root/dummy_submodule/__init__.py create mode 100644 tests/test-root/dummy_submodule/submodule1.py create mode 100644 tests/test-root/dummy_submodule/submodule2.py create mode 100644 tests/test-root/test_class_submodule.rst create mode 100644 tests/test-root/test_module_submodule.rst diff --git a/autodocsumm/__init__.py b/autodocsumm/__init__.py index f1cedbe..12f425e 100755 --- a/autodocsumm/__init__.py +++ b/autodocsumm/__init__.py @@ -279,8 +279,15 @@ def get_grouped_documenters(self, all_members=False): self.options.update(options_save) return documenters - def add_autosummary(self): - """Add the autosammary table of this documenter.""" + def add_autosummary(self, relative_ref_paths=False): + """Add the autosammary table of this documenter. + + Parameters + ========== + relative_ref_paths: bool + Use paths relative to the current module instead of + absolute import paths for each object + """ if ( self.options.get("autosummary") and not self.options.get("no-autosummary") @@ -303,8 +310,14 @@ def add_autosummary(self): indent = ' ' for (documenter, _) in documenters: - self.add_line( - indent + '~' + documenter.fullname, sourcename) + if relative_ref_paths: + obj_ref_path = documenter.fullname.lstrip( + self.modname + '.') + else: + obj_ref_path = documenter.fullname + + self.add_line(indent + '~' + obj_ref_path, sourcename) + self.add_line('', sourcename) @@ -351,7 +364,7 @@ class AutoSummModuleDocumenter(ModuleDocumenter, AutosummaryDocumenter): def add_content(self, *args, **kwargs): super().add_content(*args, **kwargs) - self.add_autosummary() + self.add_autosummary(relative_ref_paths=True) if self.options.get("autosummary-no-nesting"): self.options["no-autosummary"] = "True" @@ -400,7 +413,7 @@ class AutoSummClassDocumenter(ClassDocumenter, AutosummaryDocumenter): def add_content(self, *args, **kwargs): super().add_content(*args, **kwargs) - self.add_autosummary() + self.add_autosummary(relative_ref_paths=True) class CallableDataDocumenter(DataDocumenter): diff --git a/tests/test-root/dummy_submodule/__init__.py b/tests/test-root/dummy_submodule/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test-root/dummy_submodule/submodule1.py b/tests/test-root/dummy_submodule/submodule1.py new file mode 100644 index 0000000..29c2102 --- /dev/null +++ b/tests/test-root/dummy_submodule/submodule1.py @@ -0,0 +1,9 @@ +import dummy_submodule.submodule2 + + +class SubmoduleClass1: + """Docu for myclass 1""" + + def func1(self): + """Docu for func 1""" + pass diff --git a/tests/test-root/dummy_submodule/submodule2.py b/tests/test-root/dummy_submodule/submodule2.py new file mode 100644 index 0000000..5030b43 --- /dev/null +++ b/tests/test-root/dummy_submodule/submodule2.py @@ -0,0 +1,9 @@ +import dummy_submodule.submodule1 + + +class SubmoduleClass2: + """Docu for myclass 1""" + + def func2(self): + """Docu for func 1""" + pass diff --git a/tests/test-root/index.rst b/tests/test-root/index.rst index 8c63c8b..4f6b787 100644 --- a/tests/test-root/index.rst +++ b/tests/test-root/index.rst @@ -17,3 +17,5 @@ Example documentation test_automodulesumm test_automodulesumm_some_sections test_empty + test_class_submodule + test_module_submodule \ No newline at end of file diff --git a/tests/test-root/test_class_submodule.rst b/tests/test-root/test_class_submodule.rst new file mode 100644 index 0000000..e27c626 --- /dev/null +++ b/tests/test-root/test_class_submodule.rst @@ -0,0 +1,4 @@ +Test if links in summary are correctly generated +================================================ + +.. autoclass:: dummy_submodule.submodule1.SubmoduleClass1 diff --git a/tests/test-root/test_module_submodule.rst b/tests/test-root/test_module_submodule.rst new file mode 100644 index 0000000..25f598a --- /dev/null +++ b/tests/test-root/test_module_submodule.rst @@ -0,0 +1,4 @@ +Test if links in summary are correctly generated +================================================ + +.. automodule:: dummy_submodule.submodule2 diff --git a/tests/test_autodocsumm.py b/tests/test_autodocsumm.py index e7b2af5..9c7cf78 100644 --- a/tests/test_autodocsumm.py +++ b/tests/test_autodocsumm.py @@ -366,6 +366,28 @@ def test_autoclasssumm_inline(self, app): assert docstring_end > methods_start + def test_class_submodule(self, app): + app.build() + + html = get_html(app, '/test_class_submodule.html') + + # check that hyperlink for instance method exists in summary table + assert re.findall(r'.*href="#dummy_submodule\.submodule1' + r'\.SubmoduleClass1\.func1".*', html) + + def test_module_submodule(self, app): + app.build() + + html = get_html(app, '/test_module_submodule.html') + + # check that hyperlink for class exists in summary table + assert re.findall(r'.*href="#dummy_submodule\.submodule2' + r'\.SubmoduleClass2".*', html) + + # check that hyperlink for instance method exists in summary table + assert re.findall(r'.*href="#dummy_submodule\.submodule2' + r'\.SubmoduleClass2\.func2".*', html) + class TestAutoDocSummDirective: """Test case for the :class:`autodocsumm.AutoDocSummDirective`.""" From d49fd8fcd7eecc5f97d4be1304a369e12a733641 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Tue, 19 Apr 2022 12:42:38 +0200 Subject: [PATCH 2/3] avoid lstrip --- autodocsumm/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/autodocsumm/__init__.py b/autodocsumm/__init__.py index 12f425e..4788445 100755 --- a/autodocsumm/__init__.py +++ b/autodocsumm/__init__.py @@ -310,11 +310,11 @@ def add_autosummary(self, relative_ref_paths=False): indent = ' ' for (documenter, _) in documenters: + obj_ref_path = documenter.fullname if relative_ref_paths: - obj_ref_path = documenter.fullname.lstrip( - self.modname + '.') - else: - obj_ref_path = documenter.fullname + modname = self.modname + "." + if documenter.fullname.startswith(modname): + obj_ref_path = documenter.fullname[len(modname):] self.add_line(indent + '~' + obj_ref_path, sourcename) From 93a7473742bd4cd15318b836cabe36ead867a1ab Mon Sep 17 00:00:00 2001 From: Philipp Sommer Date: Tue, 19 Apr 2022 12:52:20 +0200 Subject: [PATCH 3/3] minor compatibility fixes --- tests/test_autodocsumm.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/tests/test_autodocsumm.py b/tests/test_autodocsumm.py index 9c7cf78..f7f2636 100644 --- a/tests/test_autodocsumm.py +++ b/tests/test_autodocsumm.py @@ -212,10 +212,7 @@ def test_class(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_method", html) assert in_autosummary("test_attr", html) @@ -267,10 +264,7 @@ def test_class_order(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_attr", html) assert in_autosummary("large_data", html) @@ -287,10 +281,7 @@ def test_class_summary_only(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_method", html) assert in_autosummary("test_attr", html) @@ -314,10 +305,7 @@ def test_class_nosignatures(self, app): if sphinx_version[:2] > [3, 1]: assert in_autosummary("instance_attribute", html) elif sphinx_version[:2] < [3, 1]: - assert ( - 'dummy.TestClass.instance_attribute' - in html - ) + assert in_autosummary("TestClass.instance_attribute", html) assert in_autosummary("test_method", html) assert in_autosummary("test_attr", html)