|
16 | 16 | member_order_option, |
17 | 17 | members_option, |
18 | 18 | ) |
| 19 | +from sphinx.ext.autodoc._docstrings import _prepare_docstrings, _process_docstrings |
19 | 20 | from sphinx.ext.autodoc._member_finder import _document_members |
20 | 21 | from sphinx.ext.autodoc._renderer import _add_content, _directive_header_lines |
21 | 22 | from sphinx.ext.autodoc.importer import _load_object_by_name |
|
27 | 28 | from sphinx.util.typing import restify, stringify_annotation |
28 | 29 |
|
29 | 30 | if TYPE_CHECKING: |
30 | | - from collections.abc import Callable, Iterator, Sequence |
| 31 | + from collections.abc import Callable, Sequence |
31 | 32 | from typing import Any, ClassVar, Final, Literal, NoReturn |
32 | 33 |
|
33 | 34 | from sphinx.config import Config |
@@ -181,12 +182,13 @@ def add_directive_header(self, *, indent: str) -> None: |
181 | 182 |
|
182 | 183 | def add_content(self, more_content: StringList | None, *, indent: str) -> None: |
183 | 184 | """Add content from docstrings, attribute documentation and user.""" |
184 | | - analyzer_source = '' if self.analyzer is None else self.analyzer.srcname |
185 | 185 | # add content from docstrings |
| 186 | + attr_docs = {} if self.analyzer is None else self.analyzer.attr_docs |
| 187 | + analyzer_source = '' if self.analyzer is None else self.analyzer.srcname |
186 | 188 | processed_doc = StringList( |
187 | 189 | list( |
188 | | - self._process_docstrings( |
189 | | - self._get_docstrings(), |
| 190 | + _process_docstrings( |
| 191 | + _prepare_docstrings(props=self.props, attr_docs=attr_docs), |
190 | 192 | events=self._events, |
191 | 193 | props=self.props, |
192 | 194 | obj=self.props._obj, |
@@ -214,74 +216,6 @@ def add_content(self, more_content: StringList | None, *, indent: str) -> None: |
214 | 216 | indent=indent + ' ' * (self.props.obj_type != 'module'), |
215 | 217 | ) |
216 | 218 |
|
217 | | - def _get_docstrings(self) -> list[list[str]] | None: |
218 | | - """Add content from docstrings, attribute documentation and user.""" |
219 | | - if self.props._docstrings is not None: |
220 | | - docstrings = [list(doc) for doc in self.props._docstrings] |
221 | | - else: |
222 | | - docstrings = None |
223 | | - props = self.props |
224 | | - |
225 | | - if docstrings is not None and len(docstrings) == 0: |
226 | | - # append at least a dummy docstring, so that the event |
227 | | - # autodoc-process-docstring is fired and can add some |
228 | | - # content if desired |
229 | | - docstrings.append([]) |
230 | | - |
231 | | - if props.obj_type in {'data', 'attribute'}: |
232 | | - return docstrings |
233 | | - |
234 | | - attr_docs = None if self.analyzer is None else self.analyzer.find_attr_docs() |
235 | | - if props.obj_type in {'class', 'exception'}: |
236 | | - real_module = props._obj___module__ or props.module_name |
237 | | - if props.module_name != real_module: |
238 | | - try: |
239 | | - # override analyzer to obtain doc-comment around its definition. |
240 | | - ma = ModuleAnalyzer.for_module(props.module_name) |
241 | | - ma.analyze() |
242 | | - attr_docs = ma.attr_docs |
243 | | - except PycodeError: |
244 | | - pass |
245 | | - |
246 | | - # add content from attribute documentation |
247 | | - if attr_docs is not None and props.parts: |
248 | | - key = ('.'.join(props.parent_names), props.name) |
249 | | - if key in attr_docs: |
250 | | - # make a copy of docstring for attributes to avoid cache |
251 | | - # the change of autodoc-process-docstring event. |
252 | | - return [list(attr_docs[key])] |
253 | | - |
254 | | - return docstrings |
255 | | - |
256 | | - @staticmethod |
257 | | - def _process_docstrings( |
258 | | - docstrings: list[list[str]] | None, |
259 | | - *, |
260 | | - events: EventManager, |
261 | | - props: _ItemProperties, |
262 | | - obj: Any, |
263 | | - options: _AutoDocumenterOptions, |
264 | | - ) -> Iterator[str]: |
265 | | - """Let the user process the docstrings before adding them.""" |
266 | | - if docstrings is None: |
267 | | - return |
268 | | - for docstring_lines in docstrings: |
269 | | - # let extensions preprocess docstrings |
270 | | - events.emit( |
271 | | - 'autodoc-process-docstring', |
272 | | - props.obj_type, |
273 | | - props.full_name, |
274 | | - obj, |
275 | | - options, |
276 | | - docstring_lines, |
277 | | - ) |
278 | | - |
279 | | - if docstring_lines and docstring_lines[-1]: |
280 | | - # append a blank line to the end of the docstring |
281 | | - docstring_lines.append('') |
282 | | - |
283 | | - yield from docstring_lines |
284 | | - |
285 | 219 | @staticmethod |
286 | 220 | def _assemble_more_content( |
287 | 221 | more_content: StringList, |
|
0 commit comments