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