Skip to content

Commit 9c3f450

Browse files
committed
Add section on automatic documentation generation
1 parent 1e4a223 commit 9c3f450

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Automatic documentation generation from code
2+
============================================
3+
4+
In the :ref:`previous section <tutorial-describing-objects>` of the tutorial
5+
you manually documented a Python function in Sphinx. However, the description
6+
was out of sync with the code itself, since the function signature was not
7+
the same. Besides, it would be nice to reuse `Python
8+
docstrings <https://www.python.org/dev/peps/pep-0257/#what-is-a-docstring>`_
9+
in the documentation, rather than having to write the information in two
10+
places.
11+
12+
Fortunately, :doc:`autodoc </usage/extensions/autodoc>` provides this
13+
functionality.
14+
15+
Reusing signatures and docstrings with autodoc
16+
----------------------------------------------
17+
18+
To use autodoc, first add it to the list of enabled extensions:
19+
20+
.. code-block:: python
21+
:caption: docs/source/conf.py
22+
:emphasize-lines: 4
23+
24+
extensions = [
25+
'sphinx.ext.duration',
26+
'sphinx.ext.doctest',
27+
'sphinx.ext.autodoc',
28+
]
29+
30+
Next, move the content of the ``.. py:function`` directive to the function
31+
docstring in the original Python file and add an optional ``kind`` argument,
32+
as follows:
33+
34+
.. code-block:: python
35+
:caption: lumache.py
36+
:emphasize-lines: 1-9
37+
38+
def get_random_ingredients(kind=None):
39+
"""
40+
Return a list of random ingredients as strings.
41+
42+
:param kind: Optional "kind" of ingredients.
43+
:type kind: list[str] or None
44+
:raise lumache.InvalidKindError: If the kind is invalid.
45+
:return: The ingredients list.
46+
:rtype: list[str]
47+
48+
"""
49+
return ["shells", "gorgonzola", "parsley"]
50+
51+
Finally, replace the ``.. py:function`` directive from the Sphinx documentation
52+
with :rst:dir:`autofunction`:
53+
54+
.. code-block:: rst
55+
:caption: docs/source/usage.rst
56+
:emphasize-lines: 3
57+
58+
you can use the ``lumache.get_random_ingredients()`` function:
59+
60+
.. autofunction:: lumache.get_random_ingredients
61+
62+
If you now build the HTML documentation, the output will be the same!
63+
Sphinx took the reStructuredText from the docstring and included it,
64+
also generating proper cross-references.
65+
66+
You can also autogenerate documentation from other objects. For example, add
67+
the code for the ``InvalidKindError`` exception:
68+
69+
.. code-block:: python
70+
:caption: lumache.py
71+
72+
class InvalidKindError(Exception):
73+
"""Raised if the kind is invalid."""
74+
pass
75+
76+
And replace the ``.. py:exception`` directive with :rst:dir:`autoexception`
77+
as follows:
78+
79+
.. code-block:: rst
80+
:caption: docs/source/usage.rst
81+
:emphasize-lines: 4
82+
83+
or ``"veggies"``. Otherwise, :py:func:`lumache.get_random_ingredients`
84+
will raise an exception.
85+
86+
.. autoexception:: lumache.InvalidKindError
87+
88+
And again, after running ``make html``, the output will be the same as before.

doc/tutorial/describing-code.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ In the previous sections of the tutorial you can read how to write narrative
55
or prose documentation in Sphinx. In this section you will describe code
66
objects instead.
77

8+
.. _tutorial-describing-objects:
9+
810
Documenting Python objects
911
--------------------------
1012

doc/tutorial/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ project.
3434
more-sphinx-customization
3535
narrative-documentation
3636
describing-code
37+
automatic-doc-generation
3738
end

0 commit comments

Comments
 (0)