From e4319d487ce27cc30ec23ff52c9a90b0845a8424 Mon Sep 17 00:00:00 2001 From: theOehrly <23384863+theOehrly@users.noreply.github.com> Date: Wed, 14 Aug 2024 23:58:42 +0200 Subject: [PATCH] don't document class attributes that are an alias of another class --- autodocsumm/__init__.py | 6 +++++- tests/test-root/dummy.py | 8 ++++++++ .../test_class_with_ref_to_other_class.rst | 6 ++++++ tests/test_autodocsumm.py | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/test-root/test_class_with_ref_to_other_class.rst diff --git a/autodocsumm/__init__.py b/autodocsumm/__init__.py index 5808308..9727ec1 100755 --- a/autodocsumm/__init__.py +++ b/autodocsumm/__init__.py @@ -414,7 +414,11 @@ class AutoSummClassDocumenter(ClassDocumenter, AutosummaryDocumenter): def add_content(self, *args, **kwargs): super().add_content(*args, **kwargs) - self.add_autosummary(relative_ref_paths=True) + # If the class is already documented under another name, Sphinx + # documents it as data/attribute. In this case, we do not want to + # generate an autosummary of the class for the attribute (see #69). + if not self.doc_as_attr: + self.add_autosummary(relative_ref_paths=True) class CallableDataDocumenter(DataDocumenter): diff --git a/tests/test-root/dummy.py b/tests/test-root/dummy.py index 36be801..f980498 100644 --- a/tests/test-root/dummy.py +++ b/tests/test-root/dummy.py @@ -84,6 +84,14 @@ def test_method_of_inline_test(self): pass +class TestClassWithRefToOtherClass: + """Class test for the autodocsummary when a class attribute is a reference + to another class. No autosummary of the class should be generated for + the attribute. See also issue #69""" + + foo = TestClass + + #: data to be skipped large_data = 'Should also be skipped' diff --git a/tests/test-root/test_class_with_ref_to_other_class.rst b/tests/test-root/test_class_with_ref_to_other_class.rst new file mode 100644 index 0000000..4cbb5cf --- /dev/null +++ b/tests/test-root/test_class_with_ref_to_other_class.rst @@ -0,0 +1,6 @@ +Autoclasssumm of Dummy Class +============================ + +.. autoclass:: dummy.TestClassWithRefToOtherClass + :members: + :autosummary: diff --git a/tests/test_autodocsumm.py b/tests/test_autodocsumm.py index 1fdcb59..fd16399 100644 --- a/tests/test_autodocsumm.py +++ b/tests/test_autodocsumm.py @@ -322,6 +322,22 @@ def test_class_nosignatures(self, app): assert '()' not in html + def test_class_no_summary_for_reference_to_class(self, app): + # see also: issue #69 + app.build() + + html = get_html(app, 'test_class_with_ref_to_other_class.html') + + # assert that the class itself has an autosummary that contains its + # attributes + assert in_autosummary("foo", html) + + # Assert that there is no autosummary of the attribute that is an alias + # of another class. This autosummary would contain attrs/methods/... + # of the referenced class. + assert not in_autosummary("test_method", html) + assert not in_autosummary("test_attr", html) + def test_inherited(self, app): app.build() html = get_html(app, 'test_inherited.html')