Debian Codegraph @

tests/test_ext_autodoc/autodoc_util.py

raw
1 from __future__ import annotations
2
3 from types import SimpleNamespace
4 from typing import TYPE_CHECKING
5
6 from sphinx.environment import _CurrentDocument
7 from sphinx.events import EventManager
8 from sphinx.ext.autodoc._directive_options import _process_documenter_options
9 from sphinx.ext.autodoc._generate import _auto_document_object
10 from sphinx.ext.autodoc._shared import _AutodocConfig
11 from sphinx.util.inspect import safe_getattr
12
13 if TYPE_CHECKING:
14 from collections.abc import Callable, Mapping
15 from typing import Any
16
17 from sphinx.ext.autodoc._property_types import _AutodocObjType
18
19 _DEFAULT_CONFIG = _AutodocConfig()
20
21
22 class FakeEvents(EventManager):
23 def __init__(self) -> None:
24 super().__init__(SimpleNamespace(pdb=False)) # type: ignore[arg-type]
25
26 self.add('autodoc-before-process-signature')
27 self.add('autodoc-process-docstring')
28 self.add('autodoc-process-signature')
29 self.add('autodoc-skip-member')
30 self.add('autodoc-process-bases')
31 self.add('object-description-transform')
32
33 def connect(
34 self, name: str, callback: Callable[..., Any], priority: int = 500
35 ) -> int:
36 return super().connect(name, callback, priority)
37
38
39 def do_autodoc(
40 obj_type: _AutodocObjType,
41 name: str,
42 *,
43 config: _AutodocConfig = _DEFAULT_CONFIG,
44 current_document: _CurrentDocument | None = None,
45 events: FakeEvents | None = None,
46 expect_import_error: bool = False,
47 options: dict[str, Any] | None = None,
48 ref_context: Mapping[str, str | None] | None = None,
49 ) -> list[str]:
50 if current_document is None:
51 current_document = _CurrentDocument(docname='index')
52 if events is None:
53 events = FakeEvents()
54 if ref_context is None:
55 ref_context = {}
56 reread_always: set[str] = set()
57
58 options = {} if options is None else options.copy()
59 doc_options = _process_documenter_options(
60 obj_type=obj_type,
61 default_options=config.autodoc_default_options,
62 options=options,
63 )
64
65 content = _auto_document_object(
66 name=name,
67 obj_type=obj_type,
68 config=config,
69 current_document=current_document,
70 events=events,
71 get_attr=safe_getattr,
72 more_content=None,
73 options=doc_options,
74 record_dependencies=set(),
75 ref_context=ref_context,
76 reread_always=reread_always,
77 )
78 if expect_import_error:
79 assert content is None
80 return []
81
82 assert content is not None
83 return content.data