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