Debian Codegraph @

tests/test_ext_autodoc/test_ext_autodoc_automodule.py

raw
1 """Test the autodoc extension.
2
3 This tests mainly the Documenters; the auto directives are tested in a test
4 source file translated by test_build.
5 """
6
7 from __future__ import annotations
8
9 import inspect
10 import sys
11 import typing
12
13 import pytest
14
15 from sphinx.ext.autodoc._dynamic._mock import _MockObject
16 from sphinx.ext.autodoc._shared import _AutodocConfig
17
18 from tests.test_ext_autodoc.autodoc_util import do_autodoc
19
20 pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path')
21
22
23 def test_empty_all() -> None:
24 options = {'members': None}
25 actual = do_autodoc('module', 'target.empty_all', options=options)
26 assert actual == [
27 '',
28 '.. py:module:: target.empty_all',
29 '',
30 ' docsting of empty_all module.',
31 '',
32 ]
33
34
35 def test_automodule() -> None:
36 options = {'members': None}
37 actual = do_autodoc('module', 'target.module', options=options)
38 assert actual == [
39 '',
40 '.. py:module:: target.module',
41 '',
42 '',
43 '.. py:data:: annotated',
44 ' :module: target.module',
45 ' :type: int',
46 '',
47 ' docstring',
48 '',
49 '',
50 '.. py:data:: documented',
51 ' :module: target.module',
52 ' :value: 1',
53 '',
54 ' docstring',
55 '',
56 ]
57
58
59 def test_automodule_undoc_members() -> None:
60 options = {
61 'members': None,
62 'undoc-members': None,
63 }
64 actual = do_autodoc('module', 'target.module', options=options)
65 assert actual == [
66 '',
67 '.. py:module:: target.module',
68 '',
69 '',
70 '.. py:data:: annotated',
71 ' :module: target.module',
72 ' :type: int',
73 '',
74 ' docstring',
75 '',
76 '',
77 '.. py:data:: documented',
78 ' :module: target.module',
79 ' :value: 1',
80 '',
81 ' docstring',
82 '',
83 '',
84 '.. py:data:: undoc_annotated',
85 ' :module: target.module',
86 ' :type: int',
87 '',
88 ]
89
90
91 def test_automodule_special_members() -> None:
92 options = {
93 'members': None,
94 'special-members': None,
95 }
96 actual = do_autodoc('module', 'target.module', options=options)
97 assert actual == [
98 '',
99 '.. py:module:: target.module',
100 '',
101 '',
102 '.. py:data:: __documented_special__',
103 ' :module: target.module',
104 ' :value: 1',
105 '',
106 ' docstring',
107 '',
108 '',
109 '.. py:data:: annotated',
110 ' :module: target.module',
111 ' :type: int',
112 '',
113 ' docstring',
114 '',
115 '',
116 '.. py:data:: documented',
117 ' :module: target.module',
118 ' :value: 1',
119 '',
120 ' docstring',
121 '',
122 ]
123
124
125 def test_automodule_inherited_members() -> None:
126 options = {
127 'members': None,
128 'undoc-members': None,
129 'inherited-members': 'Base, list',
130 }
131 actual = do_autodoc('module', 'target.inheritance', options=options)
132 assert actual == [
133 '',
134 '.. py:module:: target.inheritance',
135 '',
136 '',
137 '.. py:class:: AnotherBase()',
138 ' :module: target.inheritance',
139 '',
140 '',
141 ' .. py:method:: AnotherBase.another_inheritedmeth()',
142 ' :module: target.inheritance',
143 '',
144 ' Another inherited function.',
145 '',
146 '',
147 '.. py:class:: Base()',
148 ' :module: target.inheritance',
149 '',
150 '',
151 ' .. py:attribute:: Base.inheritedattr',
152 ' :module: target.inheritance',
153 ' :value: None',
154 '',
155 ' docstring',
156 '',
157 '',
158 ' .. py:method:: Base.inheritedclassmeth()',
159 ' :module: target.inheritance',
160 ' :classmethod:',
161 '',
162 ' Inherited class method.',
163 '',
164 '',
165 ' .. py:method:: Base.inheritedmeth()',
166 ' :module: target.inheritance',
167 '',
168 ' Inherited function.',
169 '',
170 '',
171 ' .. py:method:: Base.inheritedstaticmeth(cls)',
172 ' :module: target.inheritance',
173 ' :staticmethod:',
174 '',
175 ' Inherited static method.',
176 '',
177 '',
178 '.. py:class:: Derived()',
179 ' :module: target.inheritance',
180 '',
181 '',
182 ' .. py:method:: Derived.another_inheritedmeth()',
183 ' :module: target.inheritance',
184 '',
185 ' Another inherited function.',
186 '',
187 '',
188 ' .. py:method:: Derived.inheritedmeth()',
189 ' :module: target.inheritance',
190 '',
191 ' Inherited function.',
192 '',
193 '',
194 '.. py:class:: MyList(iterable=(), /)',
195 ' :module: target.inheritance',
196 '',
197 '',
198 ' .. py:method:: MyList.meth()',
199 ' :module: target.inheritance',
200 '',
201 ' docstring',
202 '',
203 ]
204
205
206 @pytest.mark.usefixtures('rollback_sysmodules')
207 def test_subclass_of_mocked_object() -> None:
208 config = _AutodocConfig(
209 autodoc_mock_imports=[
210 'missing_module',
211 'missing_package1',
212 'missing_package2',
213 'missing_package3',
214 'sphinx.missing_module4',
215 ]
216 )
217
218 sys.modules.pop('target', None) # unload target module to clear the module cache
219
220 options = {'members': None}
221 actual = do_autodoc('module', 'target.need_mocks', config=config, options=options)
222 # ``typing.Any`` is not available at runtime on ``_MockObject.__new__``
223 assert actual[10:11] == [
224 '.. py:class:: Inherited(*args: Any, **kwargs: Any)',
225 ]
226
227 # make ``typing.Any`` available at runtime on ``_MockObject.__new__``
228 sig = inspect.signature(_MockObject.__new__)
229 parameters = sig.parameters.copy()
230 for name in ('args', 'kwargs'):
231 parameters[name] = parameters[name].replace(annotation=typing.Any)
232 sig = sig.replace(parameters=tuple(parameters.values()))
233 _MockObject.__new__.__signature__ = sig # type: ignore[attr-defined]
234
235 options = {'members': None}
236 actual = do_autodoc('module', 'target.need_mocks', config=config, options=options)
237 assert actual[10:11] == [
238 '.. py:class:: Inherited(*args: ~typing.Any, **kwargs: ~typing.Any)',
239 ]