Debian Codegraph @

doc/usage/extensions/autosummary.rst

raw
1 .. highlight:: rst
2
3 :mod:`sphinx.ext.autosummary` -- Generate autodoc summaries
4 ===========================================================
5
6 .. module:: sphinx.ext.autosummary
7 :synopsis: Generate autodoc summaries
8
9 .. versionadded:: 0.6
10
11 .. role:: code-py(code)
12 :language: Python
13
14 This extension generates function/method/attribute summary lists, similar to
15 those output e.g. by Epydoc and other API doc generation tools. This is
16 especially useful when your docstrings are long and detailed, and putting each
17 one of them on a separate page makes them easier to read.
18
19 The :mod:`sphinx.ext.autosummary` extension does this in two parts:
20
21 1. There is an :rst:dir:`autosummary` directive for generating summary listings
22 that contain links to the documented items, and short summary blurbs
23 extracted from their docstrings.
24
25 2. A :rst:dir:`autosummary` directive also generates short "stub" files for the
26 entries listed in its content. These files by default contain only the
27 corresponding :mod:`sphinx.ext.autodoc` directive, but can be customized with
28 templates.
29
30 The :program:`sphinx-autogen` script is also able to generate "stub" files
31 from command line.
32
33 .. rst:directive:: autosummary
34
35 Insert a table that contains links to documented items, and a short summary
36 blurb (the first sentence of the docstring) for each of them.
37
38 The :rst:dir:`autosummary` directive can also optionally serve as a
39 :rst:dir:`toctree` entry for the included items. Optionally, stub
40 ``.rst`` files for these items can also be automatically generated
41 when :confval:`autosummary_generate` is `True`.
42
43 For example, ::
44
45 .. currentmodule:: sphinx
46
47 .. autosummary::
48
49 environment.BuildEnvironment
50 util.relative_uri
51
52 produces a table like this:
53
54 .. currentmodule:: sphinx
55
56 .. autosummary::
57
58 environment.BuildEnvironment
59 util.relative_uri
60
61 .. currentmodule:: sphinx.ext.autosummary
62
63 Autosummary preprocesses the docstrings and signatures with the same
64 :event:`autodoc-process-docstring` and :event:`autodoc-process-signature`
65 hooks as :mod:`~sphinx.ext.autodoc`.
66
67 .. rubric:: Options
68
69 .. rst:directive:option:: class: class names
70 :type: a list of class names, separated by spaces
71
72 Assign `class attributes`_ to the table.
73 This is a :dudir:`common option <common-options>`.
74
75 .. _class attributes: https://docutils.sourceforge.io/docs/ref/doctree.html#classes
76
77 .. versionadded:: 8.2
78
79 .. rst:directive:option:: toctree: optional directory name
80
81 If you want the :rst:dir:`autosummary` table to also serve as a
82 :rst:dir:`toctree` entry, use the ``toctree`` option, for example::
83
84 .. autosummary::
85 :toctree: DIRNAME
86
87 sphinx.environment.BuildEnvironment
88 sphinx.util.relative_uri
89
90 The ``toctree`` option also signals to the :program:`sphinx-autogen` script
91 that stub pages should be generated for the entries listed in this
92 directive. The option accepts a directory name as an argument;
93 :program:`sphinx-autogen` will by default place its output in this
94 directory. If no argument is given, output is placed in the same directory
95 as the file that contains the directive.
96
97 .. versionadded:: 0.6
98
99 .. rst:directive:option:: caption: caption of ToC
100
101 Add a caption to the toctree.
102
103 .. versionadded:: 3.1
104
105 .. rst:directive:option:: signatures: format
106
107 How to display signatures. Valid values are
108
109 - ``long`` (*default*): use a long signature. This is still cut off so that name
110 plus signature do not exceed a certain length.
111 - ``short``: Function and class signatures are displayed as ``(…)`` if they have
112 arguments and as ``()`` if they don't have arguments.
113 - ``none``: do not show signatures.
114
115 .. versionadded:: 8.2
116
117 .. rst:directive:option:: nosignatures
118
119 Do not show function signatures in the summary.
120
121 This is equivalent to ``:signatures: none``.
122
123 .. versionadded:: 0.6
124
125 .. versionchanged:: 8.2
126
127 The directive option is superseded by the more general ``:signatures: none``.
128
129 It will be deprecated and removed
130 in a future version of Sphinx.
131
132 .. rst:directive:option:: template: filename
133
134 Specify a custom template for rendering the summary.
135 For example, ::
136
137 .. autosummary::
138 :template: mytemplate.rst
139
140 sphinx.environment.BuildEnvironment
141
142 would use the template :file:`mytemplate.rst` in your
143 :confval:`templates_path` to generate the pages for all entries
144 listed. See `Customizing templates`_ below.
145
146 .. versionadded:: 1.0
147
148 .. rst:directive:option:: recursive
149
150 Generate documents for modules and sub-packages recursively.
151 For example, ::
152
153 .. autosummary::
154 :recursive:
155
156 sphinx.environment.BuildEnvironment
157
158 .. versionadded:: 3.1
159
160
161 :program:`sphinx-autogen` -- generate autodoc stub pages
162 --------------------------------------------------------
163
164 The :program:`sphinx-autogen` script can be used to conveniently generate stub
165 documentation pages for items included in :rst:dir:`autosummary` listings.
166
167 For example, the command ::
168
169 $ sphinx-autogen -o generated *.rst
170
171 will read all :rst:dir:`autosummary` tables in the :file:`*.rst` files that have
172 the ``:toctree:`` option set, and output corresponding stub pages in directory
173 ``generated`` for all documented items. The generated pages by default contain
174 text of the form::
175
176 sphinx.util.relative_uri
177 ========================
178
179 .. autofunction:: sphinx.util.relative_uri
180
181 If the ``-o`` option is not given, the script will place the output files in the
182 directories specified in the ``:toctree:`` options.
183
184 For more information, refer to the :doc:`sphinx-autogen documentation
185 </man/sphinx-autogen>`
186
187
188 Generating stub pages automatically
189 -----------------------------------
190
191 If you do not want to create stub pages with :program:`sphinx-autogen`, you can
192 also use these config values:
193
194 .. confval:: autosummary_context
195 :type: :code-py:`dict[str, Any]`
196 :default: :code-py:`{}`
197
198 A dictionary of values to pass into the template engine's context for
199 autosummary stubs files.
200
201 .. versionadded:: 3.1
202
203 .. confval:: autosummary_generate
204 :type: :code-py:`bool`
205 :default: :code-py:`True`
206
207 Boolean indicating whether to scan all found documents for autosummary
208 directives, and to generate stub pages for each.
209
210 Can also be a list of documents for which stub pages should be generated.
211
212 The new files will be placed in the directories specified in the
213 ``:toctree:`` options of the directives.
214
215 .. versionchanged:: 2.3
216
217 Emits :event:`autodoc-skip-member` event as :mod:`~sphinx.ext.autodoc`
218 does.
219
220 .. versionchanged:: 4.0
221
222 Enabled by default.
223
224 .. confval:: autosummary_generate_overwrite
225 :type: :code-py:`bool`
226 :default: :code-py:`True`
227
228 If true, autosummary overwrites existing files by generated stub pages.
229
230 .. versionadded:: 3.0
231
232 .. confval:: autosummary_mock_imports
233 :type: :code-py:`list[str]`
234 :default: :code-py:`[]`
235
236 This value contains a list of modules to be mocked up.
237 See :confval:`autodoc_mock_imports` for more details.
238 It defaults to :confval:`autodoc_mock_imports`.
239
240 .. versionadded:: 2.0
241
242 .. confval:: autosummary_imported_members
243 :type: :code-py:`bool`
244 :default: :code-py:`False`
245
246 A boolean flag indicating whether to document classes and functions imported
247 in modules.
248
249 .. versionadded:: 2.1
250
251 .. versionchanged:: 4.4
252
253 If ``autosummary_ignore_module_all`` is ``False``, this configuration
254 value is ignored for members listed in ``__all__``.
255
256 .. confval:: autosummary_ignore_module_all
257 :type: :code-py:`bool`
258 :default: :code-py:`True`
259
260 If ``False`` and a module has the ``__all__`` attribute set, autosummary
261 documents every member listed in ``__all__`` and no others.
262
263 Note that if an imported member is listed in ``__all__``, it will be
264 documented regardless of the value of ``autosummary_imported_members``. To
265 match the behaviour of ``from module import *``, set
266 ``autosummary_ignore_module_all`` to False and
267 ``autosummary_imported_members`` to True.
268
269 .. versionadded:: 4.4
270
271 .. confval:: autosummary_filename_map
272 :type: :code-py:`dict[str, str]`
273 :default: :code-py:`{}`
274
275 A dict mapping object names to filenames. This is necessary to avoid
276 filename conflicts where multiple objects have names that are
277 indistinguishable when case is ignored, on file systems where filenames
278 are case-insensitive.
279
280 .. versionadded:: 3.2
281
282 .. _autosummary-customizing-templates:
283
284 Customizing templates
285 ---------------------
286
287 .. versionadded:: 1.0
288
289 You can customize the stub page templates, in a similar way as the HTML Jinja
290 templates, see :ref:`templating`. (:class:`~sphinx.application.TemplateBridge`
291 is not supported.)
292
293 .. note::
294
295 If you find yourself spending much time tailoring the stub templates, this
296 may indicate that it's a better idea to write custom narrative documentation
297 instead.
298
299 Autosummary uses the following Jinja template files:
300
301 - :file:`autosummary/base.rst` -- fallback template
302 - :file:`autosummary/module.rst` -- template for modules
303 - :file:`autosummary/class.rst` -- template for classes
304 - :file:`autosummary/function.rst` -- template for functions
305 - :file:`autosummary/attribute.rst` -- template for class attributes
306 - :file:`autosummary/method.rst` -- template for class methods
307
308 The following variables are available in the templates:
309
310 .. currentmodule:: None
311
312 .. data:: name
313
314 Name of the documented object, excluding the module and class parts.
315
316 .. data:: objname
317
318 Name of the documented object, excluding the module parts.
319
320 .. data:: fullname
321
322 Full name of the documented object, including module and class parts.
323
324 .. data:: objtype
325
326 Type of the documented object, one of ``"module"``, ``"function"``,
327 ``"class"``, ``"method"``, ``"attribute"``, ``"data"``, ``"object"``,
328 ``"exception"``, ``"newvarattribute"``, ``"newtypedata"``, ``"property"``.
329
330 .. data:: module
331
332 Name of the module the documented object belongs to.
333
334 .. data:: class
335
336 Name of the class the documented object belongs to. Only available for
337 methods and attributes.
338
339 .. data:: underline
340
341 A string containing ``len(full_name) * '='``. Use the ``underline`` filter
342 instead.
343
344 .. data:: members
345
346 List containing names of all members of the module or class. Only available
347 for modules and classes.
348
349 .. data:: inherited_members
350
351 List containing names of all inherited members of class. Only available for
352 classes.
353
354 .. versionadded:: 1.8.0
355
356 .. data:: functions
357
358 List containing names of "public" functions in the module. Here, "public"
359 means that the name does not start with an underscore. Only available
360 for modules.
361
362 .. data:: classes
363
364 List containing names of "public" classes in the module. Only available for
365 modules.
366
367 .. data:: exceptions
368
369 List containing names of "public" exceptions in the module. Only available
370 for modules.
371
372 .. data:: methods
373
374 List containing names of "public" methods in the class. Only available for
375 classes.
376
377 .. data:: attributes
378
379 List containing names of "public" attributes in the class/module. Only
380 available for classes and modules.
381
382 .. versionchanged:: 3.1
383
384 Attributes of modules are supported.
385
386 .. data:: modules
387
388 List containing names of "public" modules in the package. Only available for
389 modules that are packages and the ``recursive`` option is on.
390
391 .. versionadded:: 3.1
392
393 Additionally, the following filters are available
394
395 .. function:: escape(s)
396
397 Escape any special characters in the text to be used in formatting RST
398 contexts. For instance, this prevents asterisks making things bold. This
399 replaces the builtin Jinja `escape filter`_ that does html-escaping.
400
401 .. function:: underline(s, line='=')
402 :no-index:
403
404 Add a title underline to a piece of text.
405
406 For instance, ``{{ fullname | escape | underline }}`` should be used to produce
407 the title of a page.
408
409 .. note::
410
411 You can use the :rst:dir:`autosummary` directive in the stub pages.
412 Stub pages are generated also based on these directives.
413
414 .. _`escape filter`: https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.escape
415
416 Autolink role
417 -------------
418
419 .. rst:role:: autolink
420
421 The ``:autolink:`` role functions as ``:py:obj:`` when the referenced *name*
422 can be resolved to a Python object, and otherwise it becomes simple emphasis.
423
424 There are some known design flaws.
425 For example, in the case of multiple objects having the same name,
426 :rst:role:`!autolink` could resolve to the wrong object.
427 It will fail silently if the referenced object is not found,
428 for example due to a spelling mistake or renaming.
429 This is sometimes unwanted behaviour.
430
431 Some users choose to configure their :confval:`default_role` to ``autolink``
432 for 'smart' referencing using the default interpreted text role (```content```).
433
434 .. seealso::
435
436 :rst:role:`any`
437
438 :rst:role:`py:obj`