example_google.py 9.6 KB

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