3
:mod:`sphinx.ext.autodoc` -- Include documentation from docstrings
4
==================================================================
6
.. module:: sphinx.ext.autodoc
7
:synopsis: Include documentation from docstrings.
9
.. index:: pair: automatic; documentation
12
This extension can import the modules you are documenting, and pull in
13
documentation from docstrings in a semi-automatic way.
17
For Sphinx (actually, the Python interpreter that executes Sphinx) to find
18
your module, it must be importable. That means that the module or the
19
package must be in one of the directories on :data:`sys.path` -- adapt your
20
:data:`sys.path` in the configuration file accordingly.
22
For this to work, the docstrings must of course be written in correct
23
reStructuredText. You can then use all of the usual Sphinx markup in the
24
docstrings, and it will end up correctly in the documentation. Together with
25
hand-written documentation, this technique eases the pain of having to maintain
26
two locations for documentation, while at the same time avoiding
27
auto-generated-looking pure API documentation.
29
:mod:`autodoc` provides several directives that are versions of the usual
30
:rst:dir:`py:module`, :rst:dir:`py:class` and so forth. On parsing time, they
31
import the corresponding module and extract the docstring of the given objects,
32
inserting them into the page source under a suitable :rst:dir:`py:module`,
33
:rst:dir:`py:class` etc. directive.
37
Just as :rst:dir:`py:class` respects the current :rst:dir:`py:module`,
38
:rst:dir:`autoclass` will also do so. Likewise, :rst:dir:`automethod` will
39
respect the current :rst:dir:`py:class`.
42
.. rst:directive:: automodule
46
Document a module, class or exception. All three directives will by default
47
only insert the docstring of the object itself::
51
will produce source like this::
57
The "auto" directives can also contain content of their own, it will be
58
inserted into the resulting non-auto directive source after the docstring
59
(but before any automatic member documentation).
61
Therefore, you can also mix automatic and non-automatic member documentation,
67
.. method:: boil(time=10)
69
Boil the noodle *time* minutes.
71
**Options and advanced usage**
73
* If you want to automatically document members, there's a ``members``
76
.. automodule:: noodle
79
will document all module members (recursively), and ::
84
will document all non-private member functions and properties (that is,
85
those whose name doesn't start with ``_``).
87
For modules, ``__all__`` will be respected when looking for members; the
88
order of the members will also be the order in ``__all__``.
90
You can also give an explicit list of members; only these will then be
96
* If you want to make the ``members`` option the default, see
97
:confval:`autodoc_default_flags`.
99
* Members without docstrings will be left out, unless you give the
100
``undoc-members`` flag option::
102
.. automodule:: noodle
106
* For classes and exceptions, members inherited from base classes will be
107
left out when documenting all members, unless you give the
108
``inherited-members`` flag option, in addition to ``members``::
110
.. autoclass:: Noodle
114
This can be combined with ``undoc-members`` to document *all* available
115
members of the class or module.
117
Note: this will lead to markup errors if the inherited members come from a
118
module whose docstrings are not reST formatted.
120
.. versionadded:: 0.3
122
* It's possible to override the signature for explicitly documented callable
123
objects (functions, methods, classes) with the regular syntax that will
124
override the signature gained from introspection::
126
.. autoclass:: Noodle(type)
128
.. automethod:: eat(persona)
130
This is useful if the signature from the method is hidden by a decorator.
132
.. versionadded:: 0.4
134
* The :rst:dir:`automodule`, :rst:dir:`autoclass` and
135
:rst:dir:`autoexception` directives also support a flag option called
136
``show-inheritance``. When given, a list of base classes will be inserted
137
just below the class signature (when used with :rst:dir:`automodule`, this
138
will be inserted for every class that is documented in the module).
140
.. versionadded:: 0.4
142
* All autodoc directives support the ``noindex`` flag option that has the
143
same effect as for standard :rst:dir:`py:function` etc. directives: no
144
index entries are generated for the documented object (and all
145
autodocumented members).
147
.. versionadded:: 0.4
149
* :rst:dir:`automodule` also recognizes the ``synopsis``, ``platform`` and
150
``deprecated`` options that the standard :rst:dir:`py:module` directive
153
.. versionadded:: 0.5
155
* :rst:dir:`automodule` and :rst:dir:`autoclass` also has an ``member-order`` option
156
that can be used to override the global value of
157
:confval:`autodoc_member_order` for one directive.
159
.. versionadded:: 0.6
161
* The directives supporting member documentation also have a
162
``exclude-members`` option that can be used to exclude single member names
163
from documentation, if all members are to be documented.
165
.. versionadded:: 0.6
169
In an :rst:dir:`automodule` directive with the ``members`` option set, only
170
module members whose ``__module__`` attribute is equal to the module name
171
as given to ``automodule`` will be documented. This is to prevent
172
documentation of imported classes or functions.
175
.. rst:directive:: autofunction
180
These work exactly like :rst:dir:`autoclass` etc., but do not offer the options
181
used for automatic member documentation.
183
For module data members and class attributes, documentation can either be put
184
into a special-formatted comment *before* the attribute definition, or in a
185
docstring *after* the definition. This means that in the following class
186
definition, both attributes can be autodocumented::
189
"""Docstring for class Foo."""
191
#: Doc comment for attribute Foo.bar.
195
"""Docstring for attribute Foo.baz."""
197
.. versionchanged:: 0.6
198
:rst:dir:`autodata` and :rst:dir:`autoattribute` can now extract docstrings.
202
If you document decorated functions or methods, keep in mind that autodoc
203
retrieves its docstrings by importing the module and inspecting the
204
``__doc__`` attribute of the given function or method. That means that if
205
a decorator replaces the decorated function with another, it must copy the
206
original ``__doc__`` to the new function.
208
From Python 2.5, :func:`functools.wraps` can be used to create
209
well-behaved decorating functions.
212
There are also new config values that you can set:
214
.. confval:: autoclass_content
216
This value selects what content will be inserted into the main body of an
217
:rst:dir:`autoclass` directive. The possible values are:
220
Only the class' docstring is inserted. This is the default. You can
221
still document ``__init__`` as a separate method using
222
:rst:dir:`automethod` or the ``members`` option to :rst:dir:`autoclass`.
224
Both the class' and the ``__init__`` method's docstring are concatenated
227
Only the ``__init__`` method's docstring is inserted.
229
.. versionadded:: 0.3
231
.. confval:: autodoc_member_order
233
This value selects if automatically documented members are sorted
234
alphabetical (value ``'alphabetical'``), by member type (value
235
``'groupwise'``) or by source order (value ``'bysource'``). The default is
238
Note that for source order, the module must be a Python module with the
239
source code available.
241
.. versionadded:: 0.6
242
.. versionchanged:: 1.0
243
Support for ``'bysource'``.
245
.. confval:: autodoc_default_flags
247
This value is a list of autodoc directive flags that should be automatically
248
applied to all autodoc directives. The supported flags are ``'members'``,
249
``'undoc-members'``, ``'inherited-members'`` and ``'show-inheritance'``.
251
If you set one of these flags in this config value, you can use a negated
252
form, :samp:`'no-{flag}'`, in an autodoc directive, to disable it once.
253
For example, if ``autodoc_default_flags`` is set to ``['members',
254
'undoc-members']``, and you write a directive like this::
259
the directive will be interpreted as if only ``:members:`` was given.
261
.. versionadded:: 1.0
264
Docstring preprocessing
265
-----------------------
267
autodoc provides the following additional events:
269
.. event:: autodoc-process-docstring (app, what, name, obj, options, lines)
271
.. versionadded:: 0.4
273
Emitted when autodoc has read and processed a docstring. *lines* is a list
274
of strings -- the lines of the processed docstring -- that the event handler
275
can modify **in place** to change what Sphinx puts into the output.
277
:param app: the Sphinx application object
278
:param what: the type of the object which the docstring belongs to (one of
279
``"module"``, ``"class"``, ``"exception"``, ``"function"``, ``"method"``,
281
:param name: the fully qualified name of the object
282
:param obj: the object itself
283
:param options: the options given to the directive: an object with attributes
284
``inherited_members``, ``undoc_members``, ``show_inheritance`` and
285
``noindex`` that are true if the flag option of same name was given to the
287
:param lines: the lines of the docstring, see above
289
.. event:: autodoc-process-signature (app, what, name, obj, options, signature, return_annotation)
291
.. versionadded:: 0.5
293
Emitted when autodoc has formatted a signature for an object. The event
294
handler can return a new tuple ``(signature, return_annotation)`` to change
295
what Sphinx puts into the output.
297
:param app: the Sphinx application object
298
:param what: the type of the object which the docstring belongs to (one of
299
``"module"``, ``"class"``, ``"exception"``, ``"function"``, ``"method"``,
301
:param name: the fully qualified name of the object
302
:param obj: the object itself
303
:param options: the options given to the directive: an object with attributes
304
``inherited_members``, ``undoc_members``, ``show_inheritance`` and
305
``noindex`` that are true if the flag option of same name was given to the
307
:param signature: function signature, as a string of the form
308
``"(parameter_1, parameter_2)"``, or ``None`` if introspection didn't succeed
309
and signature wasn't specified in the directive.
310
:param return_annotation: function return annotation as a string of the form
311
``" -> annotation"``, or ``None`` if there is no return annotation
313
The :mod:`sphinx.ext.autodoc` module provides factory functions for commonly
314
needed docstring processing in event :event:`autodoc-process-docstring`:
316
.. autofunction:: cut_lines
317
.. autofunction:: between
323
autodoc allows the user to define a custom method for determining whether a
324
member should be included in the documentation by using the following event:
326
.. event:: autodoc-skip-member (app, what, name, obj, skip, options)
328
.. versionadded:: 0.5
330
Emitted when autodoc has to decide whether a member should be included in the
331
documentation. The member is excluded if a handler returns ``True``. It is
332
included if the handler returns ``False``.
334
:param app: the Sphinx application object
335
:param what: the type of the object which the docstring belongs to (one of
336
``"module"``, ``"class"``, ``"exception"``, ``"function"``, ``"method"``,
338
:param name: the fully qualified name of the object
339
:param obj: the object itself
340
:param skip: a boolean indicating if autodoc will skip this member if the user
341
handler does not override the decision
342
:param options: the options given to the directive: an object with attributes
343
``inherited_members``, ``undoc_members``, ``show_inheritance`` and
344
``noindex`` that are true if the flag option of same name was given to the