|
| 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. |
0 commit comments