| 1 |
.. _Sphinx: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html |
| 2 |
.. _Sphinx directives: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html |
| 3 |
.. _reStructuredText: https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html |
| 4 |
.. _Markdown: https://www.sphinx-doc.org/en/master/usage/markdown.html |
| 5 |
.. _docstring: https://docs.python.org/3/glossary.html#term-docstring |
| 6 |
.. _autodoc: https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html |
| 7 |
|
| 8 |
.. _sphinx_deco: |
| 9 |
|
| 10 |
The "Sphinx" decorators |
| 11 |
======================= |
| 12 |
|
| 13 |
Overview |
| 14 |
-------- |
| 15 |
|
| 16 |
Developers use the Deprecated library to decorate deprecated functions or classes. This is very practical, |
| 17 |
but you know that this library does more: you can also document your source code! How? |
| 18 |
It's very simple: instead of using the "classic" decorator, you can use one of the "Sphinx" decorators. |
| 19 |
|
| 20 |
The "Sphinx" decorators have the same function as the "classic" decorator but also allow you to add |
| 21 |
`Sphinx directives`_ in your functions or classes documentation (inside the docstring_). |
| 22 |
|
| 23 |
.. attention:: |
| 24 |
|
| 25 |
In Python 3.3 and previous versions, the docstring of a class is immutable [#f1]_, this means that you cannot |
| 26 |
use the "Sphinx" decorators. Naturally, this limitation does not exist in Python 3.4 and above. |
| 27 |
|
| 28 |
What is a Sphinx directive? |
| 29 |
--------------------------- |
| 30 |
|
| 31 |
Sphinx_ is a tool that makes it easy to create intelligent and beautiful documentation. |
| 32 |
This tool uses the reStructuredText_ (or Markdown_) syntax to generate the documentation in different formats, |
| 33 |
the most common being HTML. Developers generally use this syntax to document the source code of their applications. |
| 34 |
|
| 35 |
Sphinx_ offers several directives allowing to introduce a text block with a predefined role. |
| 36 |
Among all the directives, the ones that interest us are those related to the functions (or classes) |
| 37 |
life cycle, namely: ``versionadded``, ``versionchanged`` and ``deprecated``. |
| 38 |
|
| 39 |
In the following example, the *mean()* function can be documented as follows: |
| 40 |
|
| 41 |
.. literalinclude:: sphinx/calc_mean.py |
| 42 |
|
| 43 |
Therefore, the "Sphinx" decorators allow you to add a Sphinx directive to your functions |
| 44 |
or classes documentation. In the case of the ``deprecated`` directive, it obviously allows you to emit a |
| 45 |
:exc:`DeprecationWarning` warning. |
| 46 |
|
| 47 |
Using the "Sphinx" decorators |
| 48 |
----------------------------- |
| 49 |
|
| 50 |
The previous example can be writen using a "Sphinx" decorator: |
| 51 |
|
| 52 |
.. literalinclude:: sphinx/calc_mean_deco.py |
| 53 |
|
| 54 |
You can see the generated documentation with this simple call: |
| 55 |
|
| 56 |
.. literalinclude:: sphinx/use_calc_mean_deco.py |
| 57 |
|
| 58 |
The documentation of the *mean()* function looks like this: |
| 59 |
|
| 60 |
.. code-block:: rst |
| 61 |
|
| 62 |
Compute the arithmetic mean (“average”) of values. |
| 63 |
|
| 64 |
:type values: typing.List[float] |
| 65 |
:param values: List of floats |
| 66 |
:return: Mean of values. |
| 67 |
|
| 68 |
.. deprecated:: 2.5.0 |
| 69 |
Since Python 3.4, you can use the standard function |
| 70 |
:func:`statistics.mean`. |
| 71 |
|
| 72 |
More elaborate example |
| 73 |
---------------------- |
| 74 |
|
| 75 |
The Deprecated library offers you 3 decorators: |
| 76 |
|
| 77 |
- :func:`~deprecated.sphinx.deprecated`: insert a ``deprecated`` directive in docstring, and emit a warning on each call. |
| 78 |
- :func:`~deprecated.sphinx.versionadded`: insert a ``versionadded`` directive in docstring, don't emit warning. |
| 79 |
- :func:`~deprecated.sphinx.versionchanged`: insert a ``versionchanged`` directive in docstring, don't emit warning. |
| 80 |
|
| 81 |
The decorators can be combined to reflect the life cycle of a function: |
| 82 |
|
| 83 |
- When it is added in your API, with the ``@versionadded`` decorator, |
| 84 |
- When it has an important change, with the ``@versionchanged`` decorator, |
| 85 |
- When it is deprecated, with the ``@deprecated`` decorator. |
| 86 |
|
| 87 |
The example bellow illustrate this life cycle: |
| 88 |
|
| 89 |
.. literalinclude:: sphinx/sphinx_demo.py |
| 90 |
|
| 91 |
To see the result, you can use the builtin function :func:`help` to display a formatted help message |
| 92 |
of the *successor()* function. It is something like this: |
| 93 |
|
| 94 |
.. code-block:: text |
| 95 |
|
| 96 |
Help on function successor in module __main__: |
| 97 |
|
| 98 |
successor(n) |
| 99 |
Calculate the successor of a number. |
| 100 |
|
| 101 |
:param n: a number |
| 102 |
:return: number + 1 |
| 103 |
|
| 104 |
|
| 105 |
.. versionadded:: 0.1.0 |
| 106 |
Here is my new function. |
| 107 |
|
| 108 |
|
| 109 |
.. versionchanged:: 0.2.0 |
| 110 |
Well, I add a new feature in this function. It is very useful as |
| 111 |
you can see in the example below, so try it. This is a very very |
| 112 |
very very very long sentence. |
| 113 |
|
| 114 |
|
| 115 |
.. deprecated:: 0.3.0 |
| 116 |
This is deprecated, really. So you need to use another function. |
| 117 |
But I don't know which one. |
| 118 |
|
| 119 |
- The first, |
| 120 |
- The second. |
| 121 |
|
| 122 |
Just guess! |
| 123 |
|
| 124 |
.. note:: Decorators must be writen in reverse order: recent first, older last. |
| 125 |
|
| 126 |
Building the documentation |
| 127 |
-------------------------- |
| 128 |
|
| 129 |
The easiest way to build your API documentation is to use the autodoc_ plugin. |
| 130 |
The directives like ``automodule``, ``autoclass``, ``autofunction`` scan your source code |
| 131 |
and generate the documentation from your docstrings. |
| 132 |
|
| 133 |
Usually, the first thing that we need to do is indicate where the Python package that contains your |
| 134 |
source code is in relation to the ``conf.py`` file. |
| 135 |
|
| 136 |
But here, that will not work! The reason is that your modules must be imported during build: |
| 137 |
the Deprecated decorators must be interpreted. |
| 138 |
|
| 139 |
So, to build the API documentation of your project with Sphinx_ you need to setup a virtualenv, |
| 140 |
and install Sphinx, external themes and/or plugins and also your project. |
| 141 |
Nowadays, this is the right way to do it. |
| 142 |
|
| 143 |
For instance, you can configure a documentation building task in your ``tox.ini`` file, for instance: |
| 144 |
|
| 145 |
.. code-block:: ini |
| 146 |
|
| 147 |
[testenv:docs] |
| 148 |
basepython = python |
| 149 |
deps = |
| 150 |
sphinx |
| 151 |
commands = |
| 152 |
sphinx-build -b html -d {envtmpdir}/doctrees docs/source/ {envtmpdir}/html |
| 153 |
|
| 154 |
.. hint:: |
| 155 |
|
| 156 |
You can see a sample implementation of Sphinx directives in the demo project |
| 157 |
`Deprecated-Demo.Sphinx <https://github.com/tantale/deprecated-demo.sphinx>`_. |
| 158 |
|
| 159 |
.. rubric:: Footnotes |
| 160 |
|
| 161 |
.. [#f1] See Issue 12773: `classes should have mutable docstrings <https://bugs.python.org/issue12773>`_. |