1
- NumPy Docstrings
2
- ================
1
+ Numpydoc docstrings
2
+ ###################
3
3
4
4
When writing docstrings for PyAnsys libraries, use the `numpydoc `_
5
5
style, regardless as to whether you are using this Sphinx extension or the
@@ -40,10 +40,11 @@ classes, methods, and variables. For example::
40
40
41
41
42
42
Required Docstring Sections
43
- ---------------------------
43
+ ===========================
44
+
44
45
PyAnsys library docstrings contain these `numpydoc `_ sections as a minimum:
45
46
46
- * `Short description <https://numpydoc.readthedocs.io/en/latest/format.html#short-summary >`_
47
+ * `Short Summary <https://numpydoc.readthedocs.io/en/latest/format.html#short-summary >`_
47
48
* `Extended Summary <https://numpydoc.readthedocs.io/en/latest/format.html#extended-summary >`_ if applicable
48
49
* `Parameters <https://numpydoc.readthedocs.io/en/latest/format.html#parameters >`_ if applicable
49
50
* `Returns <https://numpydoc.readthedocs.io/en/latest/format.html#returns >`_ if applicable
@@ -52,11 +53,29 @@ PyAnsys library docstrings contain these `numpydoc`_ sections as a minimum:
52
53
These sections should follow numpydoc style. To avoid inconsistencies between
53
54
PyAnsys libraries, adhere to the additional style guidelines that follow.
54
55
55
- Classes
56
- ~~~~~~~
56
+
57
+ Short Summary
58
+ -------------
59
+ This is a single-line which goes right after the declaration of the function or
60
+ class and provides a quick overview about the final goal of the code. The
61
+ ``short summary `` is mandatory. If not present, :ref: `Doc Style Tools ` will
62
+ raise an error.
63
+
64
+ The short summary can be declared on the same line as the opening quotes or on
65
+ the next line. Both ways are accepted by `PEP 257
66
+ <https://peps.python.org/pep-0257> `_ but you must be consistent across your
67
+ project. In case you decide to declare the short summary on the same line,
68
+ please refer to :ref: `Numpydoc Validation `, as ``"GL01" `` check needs to be
69
+ disabled.
70
+
71
+ Depending in whether you are documenting a ``Class `` or a ``function ``, you will
72
+ need to apply different ``short-summary `` guidelines.
73
+
74
+ Short Summary for Classes
75
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
57
76
A class is a 'noun' representing a collection of methods. For consistency within PyAnsys libraries,
58
77
always start the brief description for a class with a verb ending in 's', followed by an extended
59
- summary if applicable::
78
+ summary in a new line if applicable::
60
79
61
80
class FieldAnalysis3D(Analysis):
62
81
"""Manages 3D field analysis setup in HFSS, Maxwell 3D, and Q3D.
@@ -67,19 +86,20 @@ summary if applicable::
67
86
...
68
87
"""
69
88
70
-
71
89
Ensure that there is a line break between the end of a class docstring and the subsequent methods.
72
90
73
- Methods
74
- ~~~~~~~
91
+ Short Summary for Methods
92
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
75
93
A method is a 'verb' representing an action that can be performed. For consistency within PyAnsys
76
94
libraries, always start the brief description for a method with a verb not ending in 's', followed
77
- by an extended summary if applicable::
95
+ by an extended summary in a new line if applicable::
78
96
79
97
def export_mesh_stats(self, setup_name, variation_string="", mesh_path=None):
80
- """Export mesh statistics to a file."""
98
+ """Export mesh statistics to a file.
99
+
100
+ ...
101
+ """
81
102
82
-
83
103
Methods with a leading underscore (_) are 'protected' methods, meaning that they are not rendered in the
84
104
documentation unless an explicit request is made to add them using Sphinx directives. However, clearly
85
105
written descriptions for private methods are still important.
@@ -91,11 +111,13 @@ add a docstring for the setter. A setter simply exposes both the GET and SET met
91
111
just the GET method. Examples should be included to demonstrate usage.
92
112
93
113
Parameters
94
- ~~~~~~~~~~
114
+ ----------
95
115
Both classes and methods have parameters in their function signatures. All parameters in a function
96
- signature should appear in the 'Parameters' section for the class or method.
116
+ signature should appear in the ``Parameters `` section for the class or method.
117
+
118
+ Here is an example of a ``Parameters `` section for a class in PyAEDT:
97
119
98
- Here is an example of a 'Parameters' section for a class in PyAEDT::
120
+ .. code-block :: rst
99
121
100
122
Parameters
101
123
----------
@@ -144,41 +166,81 @@ parameter, the description specifies the default along with any information that
144
166
be needed about the behavior that occurs when the default is used.
145
167
146
168
Returns
147
- ~~~~~~~
148
- The 'Returns' section contains only the return data type and a brief description
149
- that concludes with a period::
169
+ -------
170
+ The ``Returns `` section contains only the return data type and a brief description
171
+ that concludes with a period:
172
+
173
+ .. code-block :: rst
150
174
151
175
Returns
152
176
-------
153
- dict
177
+ dict
154
178
Dictionary of components with their absolute paths.
155
179
156
180
157
- A class does not have a 'Returns' section. If a Boolean is returned, format the
158
- 'Returns` section like this::
181
+ A class does not have a ``Returns `` section. If a ``Boolean `` is returned, format the
182
+ ``Returns `` section like this:
183
+
184
+ .. code-block :: rst
159
185
160
186
Returns
161
- --------
162
- bool
187
+ -------
188
+ bool
163
189
``True`` when successful, ``False`` when failed.
164
190
191
+ It is possible for the ``Returns `` section to look like the ``Parameters `` one
192
+ if variable names are provided:
165
193
166
- It is possible for more than one item to be returned::
194
+ .. code-block :: rst
167
195
168
196
Returns
169
- --------
170
- type
197
+ -------
198
+ has_succeeded : bool
199
+ ``True`` when successful, ``False`` when failed.
200
+
201
+ It is possible for more than one item to be returned:
202
+
203
+ .. code-block :: rst
204
+
205
+ Returns
206
+ -------
207
+ type
171
208
Ground object.
172
- str
209
+ str
173
210
Ground name.
174
211
175
-
176
212
If a method does not have a decorator, the basic implementation of Python
177
213
methods is used. In this case, while ``None `` is returned, you do not document it.
178
214
Consequently, such a method does not have a 'Returns' section.
179
215
216
+ Examples
217
+ --------
218
+
219
+ The ``Examples `` section provides a quick reference on how to use a method or
220
+ function. This section needs to be compliant with the `doctest
221
+ <https://docs.python.org/3/library/doctest.html> `_ format and is not supposed to
222
+ be a replacement of your test suite but a complement to it. An an example,
223
+ consider the following function:
224
+
225
+ .. code-block :: rst
226
+
227
+ Examples
228
+ --------
229
+ Create an instance of HFSS and connect to an existing HFSS
230
+ design or create a new HFSS design if one does not exist.
231
+
232
+ >>> from pyaedt import Hfss
233
+ >>> hfss = Hfss()
234
+ pyaedt info: No project is defined...
235
+ pyaedt info: Active design is set to...
236
+
237
+
238
+ Notice that if the definition of the function gets updated, this
239
+ section needs to be updated too.
240
+
241
+
180
242
Example Docstrings
181
- ------------------
243
+ ==================
182
244
Methods and functions should generally be documented within the
183
245
'Examples' section to make the usage of the method or function clear.
184
246
Here is a sample function:
@@ -196,26 +258,8 @@ This directive renders the sample function as:
196
258
197
259
.. autofunction :: pyansys_sphinx_theme.sample_func.func
198
260
199
-
200
- Validation
201
- ----------
202
- Enable validation of docstrings during the Sphinx build by adding the
203
- following line to the ``conf.py `` file::
204
-
205
- numpydoc_validation_checks = {"GL08"}
206
-
207
- This will issue the following warning for any object without a docstring::
208
-
209
- "The object does not have a docstring"
210
-
211
- The ``"GL08" `` code is required at minimum for PyAnsys libraries.
212
- Other codes may be enforced at a later date. For a full listing,
213
- see `Validation <https://numpydoc.readthedocs.io/en/latest/validation.html#validation >`_
214
- in the `numpydoc `_.
215
-
216
-
217
261
Additional Information
218
- ----------------------
262
+ ======================
219
263
You can find additional information and examples at `numpydoc `_. Reference
220
264
this documentation as the primary source regarding docstring styles for directives
221
265
that are not covered here. For example, you use the ``note:: `` directive to highlight
0 commit comments