Debian Codegraph @

doc/development/tutorials/autodoc_ext.rst

raw
1 .. _autodoc_ext_tutorial:
2
3 Developing autodoc extensions
4 =============================
5
6 The objective of this tutorial is to create an extension that adds
7 support for new type for autodoc. This autodoc extension will format
8 the ``IntEnum`` class from Python standard library. (module ``enum``)
9
10 Overview
11 --------
12
13 We want the extension that will create auto-documentation for IntEnum.
14 ``IntEnum`` is the integer enum class from standard library ``enum`` module.
15
16 Currently this class has no special auto documentation behavior.
17
18 We want to add following to autodoc:
19
20 * A new ``autointenum`` directive that will document the ``IntEnum`` class.
21 * The generated documentation will have all the enum possible values
22 with names.
23 * The ``autointenum`` directive will have an option ``:hex:`` which will
24 cause the integers be printed in hexadecimal form.
25
26
27 Prerequisites
28 -------------
29
30 We need the same setup as in
31 :ref:`the previous extensions <tutorial-extend-build>`.
32 This time, we will be putting out extension
33 in a file called :file:`autodoc_intenum.py`.
34 The :file:`my_enums.py` will contain the sample enums we will document.
35
36 Here is an example of the folder structure you might obtain:
37
38 .. code-block:: text
39
40 └── source
41    ├── _ext
42 │   └── autodoc_intenum.py
43    ├── conf.py
44    ├── index.rst
45    └── my_enums.py
46
47
48 Writing the extension
49 ---------------------
50
51 Start with ``setup`` function for the extension.
52
53 .. literalinclude:: examples/autodoc_intenum.py
54 :language: python
55 :linenos:
56 :pyobject: setup
57
58
59 The :meth:`~sphinx.application.Sphinx.setup_extension` method will pull the
60 autodoc extension because our new extension depends on autodoc.
61 :meth:`~sphinx.application.Sphinx.add_autodocumenter` is the method that
62 registers our new auto documenter class.
63
64 We want to import certain objects from the autodoc extension:
65
66 .. literalinclude:: examples/autodoc_intenum.py
67 :language: python
68 :linenos:
69 :lines: 1-7
70
71
72 There are several different documenter classes such as ``MethodDocumenter``
73 or ``AttributeDocumenter`` available in the autodoc extension but
74 our new class is the subclass of ``ClassDocumenter`` which a
75 documenter class used by autodoc to document classes.
76
77 This is the definition of our new the auto-documenter class:
78
79 .. literalinclude:: examples/autodoc_intenum.py
80 :language: python
81 :linenos:
82 :pyobject: IntEnumDocumenter
83
84
85 Important attributes of the new class:
86
87 **objtype**
88 This attribute determines the ``auto`` directive name. In
89 this case the auto directive will be ``autointenum``.
90
91 **directivetype**
92 This attribute sets the generated directive name. In
93 this example the generated directive will be ``.. :py:class::``.
94
95 **priority**
96 the larger the number the higher is the priority. We want our
97 documenter be higher priority than the parent.
98
99 **option_spec**
100 option specifications. We copy the parent class options and
101 add a new option *hex*.
102
103
104 Overridden members:
105
106 **can_document_member**
107 This member is important to override. It should
108 return *True* when the passed object can be documented by this class.
109
110 **add_directive_header**
111 This method generates the directive header. We add
112 **:final:** directive option. Remember to call **super** or no directive
113 will be generated.
114
115 **add_content**
116 This method generates the body of the class documentation.
117 After calling the super method we generate lines for enum description.
118
119
120 Using the extension
121 -------------------
122
123 You can now use the new autodoc directive to document any ``IntEnum``.
124
125 For example, you have the following ``IntEnum``:
126
127 .. code-block:: python
128 :caption: my_enums.py
129
130 class Colors(IntEnum):
131 """Colors enumerator"""
132 NONE = 0
133 RED = 1
134 GREEN = 2
135 BLUE = 3
136
137
138 This will be the documentation file with auto-documentation directive:
139
140 .. code-block:: rst
141 :caption: index.rst
142
143 .. autointenum:: my_enums.Colors
144
145 Further reading
146 ---------------
147
148 If you wish to share your extension across multiple projects or with others,
149 check out the :ref:`third-party-extensions` section.