diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-12-31 10:43:20 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-31 10:43:20 +0900 |
commit | 1dd0cc8494e3320fa4fac70398ac044bc4604b87 (patch) | |
tree | bacdc87a032964ec22e3c9deb4f27260489f8d9b | |
parent | 3f560cd67239f75840cc7a439ab54d8509c855f6 (diff) | |
parent | e3b1fdeeebeca6b7c03cc16dce5d9b686292a1ac (diff) | |
download | sphinx-git-1dd0cc8494e3320fa4fac70398ac044bc4604b87.tar.gz |
Merge pull request #8611 from tk0miya/8602_process-docstring_for_nondatadescriptors
Fix #8602: autodoc: The ``autodoc-process-docstring`` event is emitted to the non-datadescriptors unexpectedly
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | sphinx/ext/autodoc/__init__.py | 22 | ||||
-rw-r--r-- | tests/test_ext_autodoc_events.py | 17 |
3 files changed, 39 insertions, 2 deletions
@@ -17,6 +17,8 @@ Bugs fixed ---------- * #8164: autodoc: Classes that inherit mocked class are not documented +* #8602: autodoc: The ``autodoc-process-docstring`` event is emitted to the + non-datadescriptors unexpectedly Testing -------- diff --git a/sphinx/ext/autodoc/__init__.py b/sphinx/ext/autodoc/__init__.py index a1c642703..4429a2516 100644 --- a/sphinx/ext/autodoc/__init__.py +++ b/sphinx/ext/autodoc/__init__.py @@ -2092,18 +2092,36 @@ class NonDataDescriptorMixin(DataDocumenterMixinBase): and :value: header will be suppressed unexpectedly. """ + def import_object(self, raiseerror: bool = False) -> bool: + ret = super().import_object(raiseerror) # type: ignore + if ret and not inspect.isattributedescriptor(self.object): + self.non_data_descriptor = True + else: + self.non_data_descriptor = False + + return ret + def should_suppress_value_header(self) -> bool: - return (inspect.isattributedescriptor(self.object) or + return (not getattr(self, 'non_data_descriptor', False) or super().should_suppress_directive_header()) def get_doc(self, encoding: str = None, ignore: int = None) -> List[List[str]]: - if not inspect.isattributedescriptor(self.object): + if getattr(self, 'non_data_descriptor', False): # the docstring of non datadescriptor is very probably the wrong thing # to display return [] else: return super().get_doc(encoding, ignore) # type: ignore + def add_content(self, more_content: Optional[StringList], no_docstring: bool = False + ) -> None: + if getattr(self, 'non_data_descriptor', False): + # the docstring of non datadescriptor is very probably the wrong thing + # to display + no_docstring = True + + super().add_content(more_content, no_docstring=no_docstring) # type: ignore + class SlotsMixin(DataDocumenterMixinBase): """ diff --git a/tests/test_ext_autodoc_events.py b/tests/test_ext_autodoc_events.py index ad6a81129..d00dea180 100644 --- a/tests/test_ext_autodoc_events.py +++ b/tests/test_ext_autodoc_events.py @@ -35,6 +35,23 @@ def test_process_docstring(app): @pytest.mark.sphinx('html', testroot='ext-autodoc') +def test_process_docstring_for_nondatadescriptor(app): + def on_process_docstring(app, what, name, obj, options, lines): + raise + + app.connect('autodoc-process-docstring', on_process_docstring) + + actual = do_autodoc(app, 'attribute', 'target.AttCls.a1') + assert list(actual) == [ + '', + '.. py:attribute:: AttCls.a1', + ' :module: target', + ' :value: hello world', + '', + ] + + +@pytest.mark.sphinx('html', testroot='ext-autodoc') def test_cut_lines(app): app.connect('autodoc-process-docstring', cut_lines(2, 2, ['function'])) |