Debian Codegraph @

doc/usage/extensions/example_google.py

raw
1 """Example Google style docstrings.
2
3 This module demonstrates documentation as specified by the `Google Python
4 Style Guide`_. Docstrings may extend over multiple lines. Sections are created
5 with a section header and a colon followed by a block of indented text.
6
7 Example:
8 Examples can be given using either the ``Example`` or ``Examples``
9 sections. Sections support any reStructuredText formatting, including
10 literal blocks::
11
12 $ python example_google.py
13
14 Section breaks are created by resuming unindented text. Section breaks
15 are also implicitly created anytime a new section starts.
16
17 Attributes:
18 module_level_variable1 (int): Module level variables may be documented in
19 either the ``Attributes`` section of the module docstring, or in an
20 inline docstring immediately following the variable.
21
22 Either form is acceptable, but the two should not be mixed. Choose
23 one convention to document module level variables and be consistent
24 with it.
25
26 Todo:
27 * For module TODOs
28 * You have to also use ``sphinx.ext.todo`` extension
29
30 .. _Google Python Style Guide:
31 https://google.github.io/styleguide/pyguide.html
32
33 """
34
35 module_level_variable1 = 12345
36
37 module_level_variable2 = 98765
38 """int: Module level variable documented inline.
39
40 The docstring may span multiple lines. The type may optionally be specified
41 on the first line, separated by a colon.
42 """
43
44
45 def function_with_types_in_docstring(param1, param2):
46 """Example function with types documented in the docstring.
47
48 :pep:`484` type annotations are supported. If attribute, parameter, and
49 return types are annotated according to `PEP 484`_, they do not need to be
50 included in the docstring:
51
52 Args:
53 param1 (int): The first parameter.
54 param2 (str): The second parameter.
55
56 Returns:
57 bool: The return value. True for success, False otherwise.
58 """
59
60
61 def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
62 """Example function with PEP 484 type annotations.
63
64 Args:
65 param1: The first parameter.
66 param2: The second parameter.
67
68 Returns:
69 The return value. True for success, False otherwise.
70
71 """
72
73
74 def module_level_function(param1, param2=None, *args, **kwargs):
75 """This is an example of a module level function.
76
77 Function parameters should be documented in the ``Args`` section. The name
78 of each parameter is required. The type and description of each parameter
79 is optional, but should be included if not obvious.
80
81 If ``*args`` or ``**kwargs`` are accepted,
82 they should be listed as ``*args`` and ``**kwargs``.
83
84 The format for a parameter is::
85
86 name (type): description
87 The description may span multiple lines. Following
88 lines should be indented. The "(type)" is optional.
89
90 Multiple paragraphs are supported in parameter
91 descriptions.
92
93 Args:
94 param1 (int): The first parameter.
95 param2 (:obj:`str`, optional): The second parameter. Defaults to None.
96 Second line of description should be indented.
97 *args: Variable length argument list.
98 **kwargs: Arbitrary keyword arguments.
99
100 Returns:
101 bool: True if successful, False otherwise.
102
103 The return type is optional and may be specified at the beginning of
104 the ``Returns`` section followed by a colon.
105
106 The ``Returns`` section may span multiple lines and paragraphs.
107 Following lines should be indented to match the first line.
108
109 The ``Returns`` section supports any reStructuredText formatting,
110 including literal blocks::
111
112 {
113 'param1': param1,
114 'param2': param2,
115 }
116
117 Raises:
118 AttributeError: The ``Raises`` section is a list of all exceptions
119 that are relevant to the interface.
120 ValueError: If `param2` is equal to `param1`.
121
122 """
123 if param1 == param2:
124 msg = 'param1 may not be equal to param2'
125 raise ValueError(msg)
126 return True
127
128
129 def example_generator(n):
130 """Generators have a ``Yields`` section instead of a ``Returns`` section.
131
132 Args:
133 n (int): The upper limit of the range to generate, from 0 to `n` - 1.
134
135 Yields:
136 int: The next number in the range of 0 to `n` - 1.
137
138 Examples:
139 Examples should be written in doctest format, and should illustrate how
140 to use the function.
141
142 >>> print([i for i in example_generator(4)])
143 [0, 1, 2, 3]
144
145 """
146 yield from range(n)
147
148
149 class ExampleError(Exception):
150 """Exceptions are documented in the same way as classes.
151
152 The __init__ method may be documented in either the class level
153 docstring, or as a docstring on the __init__ method itself.
154
155 Either form is acceptable, but the two should not be mixed. Choose one
156 convention to document the __init__ method and be consistent with it.
157
158 Note:
159 Do not include the `self` parameter in the ``Args`` section.
160
161 Args:
162 msg (str): Human readable string describing the exception.
163 code (:obj:`int`, optional): Error code.
164
165 Attributes:
166 msg (str): Human readable string describing the exception.
167 code (int): Exception error code.
168
169 """
170
171 def __init__(self, msg, code):
172 self.msg = msg
173 self.code = code
174
175
176 class ExampleClass:
177 """The summary line for a class docstring should fit on one line.
178
179 If the class has public attributes, they may be documented here
180 in an ``Attributes`` section and follow the same formatting as a
181 function's ``Args`` section. Alternatively, attributes may be documented
182 inline with the attribute's declaration (see __init__ method below).
183
184 Properties created with the ``@property`` decorator should be documented
185 in the property's getter method.
186
187 Attributes:
188 attr1 (str): Description of `attr1`.
189 attr2 (:obj:`int`, optional): Description of `attr2`.
190
191 """
192
193 def __init__(self, param1, param2, param3):
194 """Example of docstring on the __init__ method.
195
196 The __init__ method may be documented in either the class level
197 docstring, or as a docstring on the __init__ method itself.
198
199 Either form is acceptable, but the two should not be mixed. Choose one
200 convention to document the __init__ method and be consistent with it.
201
202 Note:
203 Do not include the `self` parameter in the ``Args`` section.
204
205 Args:
206 param1 (str): Description of `param1`.
207 param2 (:obj:`int`, optional): Description of `param2`. Multiple
208 lines are supported.
209 param3 (list(str)): Description of `param3`.
210
211 """
212 self.attr1 = param1
213 self.attr2 = param2
214 self.attr3 = param3 #: Doc comment *inline* with attribute
215
216 #: list(str): Doc comment *before* attribute, with type specified
217 self.attr4 = ['attr4']
218
219 self.attr5 = None
220 """str: Docstring *after* attribute, with type specified."""
221
222 @property
223 def readonly_property(self):
224 """str: Properties should be documented in their getter method."""
225 return 'readonly_property'
226
227 @property
228 def readwrite_property(self):
229 """list(str): Properties with both a getter and setter
230 should only be documented in their getter method.
231
232 If the setter method contains notable behavior, it should be
233 mentioned here.
234 """
235 return ['readwrite_property']
236
237 @readwrite_property.setter
238 def readwrite_property(self, value):
239 _ = value
240
241 def example_method(self, param1, param2):
242 """Class methods are similar to regular functions.
243
244 Note:
245 Do not include the `self` parameter in the ``Args`` section.
246
247 Args:
248 param1: The first parameter.
249 param2: The second parameter.
250
251 Returns:
252 True if successful, False otherwise.
253
254 """
255 return True
256
257 def __special__(self):
258 """By default special members with docstrings are not included.
259
260 Special members are any methods or attributes that start with and
261 end with a double underscore. Any special member with a docstring
262 will be included in the output, if
263 ``napoleon_include_special_with_doc`` is set to True.
264
265 This behavior can be enabled by changing the following setting in
266 Sphinx's conf.py::
267
268 napoleon_include_special_with_doc = True
269
270 """
271 pass
272
273 def __special_without_docstring__(self):
274 pass
275
276 def _private(self):
277 """By default private members are not included.
278
279 Private members are any methods or attributes that start with an
280 underscore and are *not* special. By default they are not included
281 in the output.
282
283 This behavior can be changed such that private members *are* included
284 by changing the following setting in Sphinx's conf.py::
285
286 napoleon_include_private_with_doc = True
287
288 """
289 pass
290
291 def _private_without_docstring(self):
292 pass
293
294
295 class ExamplePEP526Class:
296 """The summary line for a class docstring should fit on one line.
297
298 If the class has public attributes, they may be documented here
299 in an ``Attributes`` section and follow the same formatting as a
300 function's ``Args`` section. If ``napoleon_attr_annotations``
301 is True, types can be specified in the class body using ``PEP 526``
302 annotations.
303
304 Attributes:
305 attr1: Description of `attr1`.
306 attr2: Description of `attr2`.
307
308 """
309
310 attr1: str
311 attr2: int