Debian Codegraph @

docs_sphinx/developer/guidelines/documentation.rst

raw
1 Documentation
2 =============
3
4 It is very important to maintain documentation. We use the
5 `Sphinx documentation generator <http://www.sphinx-doc.org/en/stable/>`__
6 tools. The documentation is all hand written. Sphinx source files are stored in the
7 ``docs_sphinx`` folder. The HTML files can be generated via the script
8 ``dev/tools/docs/build_html_brian2.py`` and end
9 up in the ``docs`` folder.
10
11 Most of the documentation is stored directly in the Sphinx
12 source text files, but reference documentation for important Brian classes and
13 functions are kept in the documentation strings of those classes themselves.
14 This is automatically pulled from these classes for the reference manual
15 section of the documentation. The idea is to keep the definitive reference
16 documentation near the code that it documents, serving as both a comment for
17 the code itself, and to keep the documentation up to date with the code.
18
19 The reference documentation includes all classes, functions and other objects
20 that are defined in the modules and only documents them in the module where
21 they were defined. This makes it possible to document a class like
22 `~brian2.units.fundamentalunits.Quantity` only in `brian2.units.fundamentalunits`
23 and not additionally in `brian2.units` and `brian2`. This mechanism relies on
24 the ``__module__`` attribute, in some cases, in particular when wrapping a
25 function with a decorator (e.g. `~brian2.units.fundamentalunits.check_units`),
26 this attribute has to be set manually::
27
28 foo.__module__ = __name__
29
30 Without this manual setting, the function might not be documented at all or in
31 the wrong module.
32
33 In addition to the reference, all the examples in the examples folder are
34 automatically included in the documentation.
35
36 Note that you can directly link to github issues using ``:issue:`issue number```, e.g.
37 writing ``:issue:`33``` links to a github issue about running benchmarks for Brian 2:
38 :issue:`33`. This feature should rarely be used in the main documentation, reserve its
39 use for release notes and important known bugs.
40
41 Docstrings
42 ----------
43
44 Every module, class, method or function has to start with a docstring, unless
45 it is a private or special method (i.e. starting with ``_`` or ``__``) *and* it
46 is obvious what it does. For example, there is normally no need to document
47 ``__str__`` with "Return a string representation.".
48
49 For the docstring format, we use the our own sphinx extension (in
50 `brian2/sphinxext`) based on
51 `numpydoc <https://pypi.python.org/pypi/numpydoc/>`__, allowing to write
52 docstrings that are well readable both in sourcecode as well as in the
53 rendered HTML. We generally follow the `format used by numpy
54 <https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard>`__
55
56 When the docstring uses variable, class or function names, these should be
57 enclosed in single backticks. Class and function/method names will be
58 automatically linked to the corresponding documentation. For classes imported
59 in the main brian2 package, you do not have to add the package name, e.g.
60 writing ```NeuronGroup``` is enough. For other classes, you have to give the
61 full path, e.g. ```brian2.units.fundamentalunits.UnitRegistry```. If it is
62 clear from the context where the class is (e.g. within the documentation of
63 `~brian2.units.fundamentalunits.UnitRegistry`), consider using the ``~``
64 abbreviation: ```~brian2.units.fundamentalunits.UnitRegistry``` displays only
65 the class name: `~brian2.units.fundamentalunits.UnitRegistry`. Note that you do
66 not have to enclose the exception name in a "Raises" or "Warns" section, or
67 the class/method/function name in a "See Also" section in backticks, they will
68 be automatically linked (putting backticks there will lead to incorrect display
69 or an error message),
70
71 Inline source fragments should be enclosed in double backticks.
72
73 Class docstrings follow the same conventions as method docstrings and should
74 document the ``__init__`` method, the ``__init__`` method itself does not need
75 a docstring.
76
77 Documenting functions and methods
78 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79 The docstring for a function/method should start with a one-line description of
80 what the function does, without referring to the function name or the names of
81 variables. Use a "command style" for this summary, e.g. "Return the result."
82 instead of "Returns the result." If the signature of the function cannot be
83 automatically extracted because of an decorator (e.g. `check_units`), place a
84 signature in the very first row of the docstring, before the one-line
85 description.
86
87 For methods, do not document the ``self`` parameter, nor give information about
88 the method being static or a class method (this information will be
89 automatically added to the documentation).
90
91 Documenting classes
92 ~~~~~~~~~~~~~~~~~~~
93 Class docstrings should use the same "Parameters" and "Returns" sections as
94 method and function docstrings for documenting the ``__init__`` constructor. If
95 a class docstring does not have any "Attributes" or "Methods" section, these
96 sections will be automatically generated with all documented (i.e. having a
97 docstring), public (i.e. not starting with `_`) attributes respectively methods
98 of the class. Alternatively, you can provide these sections manually. This is
99 useful for example in the `Quantity` class, which would otherwise include the
100 documentation of many `ndarray` methods, or when you want to include
101 documentation for functions like ``__getitem__`` which would otherwise not be
102 documented. When specifying these sections, you only have to state the names of
103 documented methods/attributes but you can also provide direct documentation.
104 For example::
105
106 Attributes
107 ----------
108 foo
109 bar
110 baz
111 This is a description.
112
113 This can be used for example for class or instance attributes which do not
114 have "classical" docstrings. However, you can also use a special syntax: When
115 defining class attributes in the class body or instance attributes in
116 ``__init__`` you can use the following variants (here shown for instance
117 attributes)::
118
119 def __init__(a, b, c):
120 #: The docstring for the instance attribute a.
121 #: Can also span multiple lines
122 self.a = a
123
124 self.b = b #: The docstring for self.b (only one line).
125
126 self.c = c
127 'The docstring for self.c, directly *after* its definition'
128
129 Long example of a function docstring
130 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
132 This is a very long docstring, showing all the possible sections. Most of the
133 time no See Also, Notes or References section is needed::
134
135 def foo(var1, var2, long_var_name='hi') :
136 """
137 A one-line summary that does not use variable names or the function name.
138
139 Several sentences providing an extended description. Refer to
140 variables using back-ticks, e.g. `var1`.
141
142 Parameters
143 ----------
144 var1 : array_like
145 Array_like means all those objects -- lists, nested lists, etc. --
146 that can be converted to an array. We can also refer to
147 variables like `var1`.
148 var2 : int
149 The type above can either refer to an actual Python type
150 (e.g. ``int``), or describe the type of the variable in more
151 detail, e.g. ``(N,) ndarray`` or ``array_like``.
152 Long_variable_name : {'hi', 'ho'}, optional
153 Choices in brackets, default first when optional.
154
155 Returns
156 -------
157 describe : type
158 Explanation
159 output : type
160 Explanation
161 tuple : type
162 Explanation
163 items : type
164 even more explaining
165
166 Raises
167 ------
168 BadException
169 Because you shouldn't have done that.
170
171 See Also
172 --------
173 otherfunc : relationship (optional)
174 newfunc : Relationship (optional), which could be fairly long, in which
175 case the line wraps here.
176 thirdfunc, fourthfunc, fifthfunc
177
178 Notes
179 -----
180 Notes about the implementation algorithm (if needed).
181
182 This can have multiple paragraphs.
183
184 You may include some math:
185
186 .. math:: X(e^{j\omega } ) = x(n)e^{ - j\omega n}
187
188 And even use a greek symbol like :math:`omega` inline.
189
190 References
191 ----------
192 Cite the relevant literature, e.g. [1]_. You may also cite these
193 references in the notes section above.
194
195 .. [1] O. McNoleg, "The integration of GIS, remote sensing,
196 expert systems and adaptive co-kriging for environmental habitat
197 modelling of the Highland Haggis using object-oriented, fuzzy-logic
198 and neural-network techniques," Computers & Geosciences, vol. 22,
199 pp. 585-588, 1996.
200
201 Examples
202 --------
203 These are written in doctest format, and should illustrate how to
204 use the function.
205
206 >>> a=[1,2,3]
207 >>> print([x + 3 for x in a])
208 [4, 5, 6]
209 >>> print("a\nb")
210 a
211 b
212
213 """
214
215 pass