Skip to content

Commit a6e8395

Browse files
committed
Add tutorial section on documenting Python objects
1 parent db8ab00 commit a6e8395

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed
70.3 KB
Loading
40.5 KB
Loading

doc/tutorial/describing-code.rst

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
Describing code in Sphinx
2+
=========================
3+
4+
In the previous sections of the tutorial you can read how to write narrative
5+
or prose documentation in Sphinx. In this section you will describe code
6+
objects instead.
7+
8+
Documenting Python objects
9+
--------------------------
10+
11+
Sphinx offers several roles and directives to document Python objects,
12+
all grouped together in the Python
13+
:doc:`domain </usage/restructuredtext/domains>`. For example, you can
14+
use the :rst:dir:`py:function` directive to document a Python function,
15+
as follows:
16+
17+
.. code-block:: rst
18+
:caption: docs/source/usage.rst
19+
20+
Creating recipes
21+
----------------
22+
23+
To retrieve a list of random ingredients,
24+
you can use the ``lumache.get_random_ingredients()`` function:
25+
26+
.. py:function:: lumache.get_random_ingredients([kind=None])
27+
28+
Return a list of random ingredients as strings.
29+
30+
:param kind: Optional "kind" of ingredients.
31+
:type kind: list[str] or None
32+
:return: The ingredients list.
33+
:rtype: list[str]
34+
35+
Which will render like this:
36+
37+
.. figure:: /_static/tutorial/lumache-py-function.png
38+
:width: 80%
39+
:align: center
40+
:alt: HTML result of documenting a Python function in Sphinx
41+
42+
HTML result of documenting a Python function in Sphinx
43+
44+
Notice several things:
45+
46+
- Sphinx parsed the argument of the ``.. py:function`` directive and
47+
highlighted the module, the function name, and the parameters appropriately.
48+
- Putting a parameter inside square brackets usually conveys that it is
49+
optional (although it is not mandatory to use this syntax).
50+
- The directive content includes a one-line description of the function,
51+
as well as a :ref:`field list <rst-field-lists>` containing the function
52+
parameter, its expected type, the return value, and the return type.
53+
54+
.. note::
55+
56+
Since Python is the default :term:`domain`, you can write
57+
``.. function::`` directly without having to prefix the directive with
58+
``py:``.
59+
60+
Cross-referencing Python objects
61+
--------------------------------
62+
63+
By default, most of these directives generate entities that can be
64+
cross-referenced from any part of the documentation by using
65+
:ref:`a corresponding role <python-roles>`. For the case of functions,
66+
you can use :rst:role:`py:func` for that, as follows:
67+
68+
.. code-block:: rst
69+
:caption: docs/source/usage.rst
70+
71+
The ``kind`` parameter should be either ``"meat"``, ``"fish"``,
72+
or ``"veggies"``. Otherwise, :py:func:`lumache.get_random_ingredients`
73+
will raise an exception.
74+
75+
In some contexts, Sphinx will generate a cross-reference automatically just
76+
by using the name of the object, without you having to explicitly use a role
77+
for that. For example, you can describe the custom exception raised by the
78+
function using the :rst:dir:`py:exception` directive:
79+
80+
.. code-block:: rst
81+
:caption: docs/source/usage.rst
82+
83+
.. py:exception:: lumache.InvalidKindError
84+
85+
Raised if the kind is invalid.
86+
87+
Then, add this exception to the original description of the function:
88+
89+
.. code-block:: rst
90+
:caption: docs/source/usage.rst
91+
:emphasize-lines: 7
92+
93+
.. py:function:: lumache.get_random_ingredients([kind=None])
94+
95+
Return a list of random ingredients as strings.
96+
97+
:param kind: Optional "kind" of ingredients.
98+
:type kind: list[str] or None
99+
:raise lumache.InvalidKindError: If the kind is invalid.
100+
:return: The ingredients list.
101+
:rtype: list[str]
102+
103+
And finally, this is how the result would look like:
104+
105+
.. figure:: /_static/tutorial/lumache-py-function-full.png
106+
:width: 80%
107+
:align: center
108+
:alt: HTML result of documenting a Python function in Sphinx
109+
with cross-references
110+
111+
HTML result of documenting a Python function in Sphinx with cross-references
112+
113+
Beautiful, isn't it?

doc/tutorial/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ project.
3333
first-steps
3434
more-sphinx-customization
3535
narrative-documentation
36+
describing-code
3637
end

doc/usage/restructuredtext/domains.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ In short:
125125
component of the target. For example, ``:py:meth:`~Queue.Queue.get``` will
126126
refer to ``Queue.Queue.get`` but only display ``get`` as the link text.
127127

128+
.. _python-domain:
128129

129130
The Python Domain
130131
-----------------

0 commit comments

Comments
 (0)