Skip to content

Commit d60943b

Browse files
committed
📝 Update docstring section
1 parent f49938e commit d60943b

File tree

1 file changed

+73
-75
lines changed

1 file changed

+73
-75
lines changed

docs/document/sphinx/docstrings.rst

Lines changed: 73 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ Docstrings
33

44
With the Sphinx extension `sphinx.ext.autodoc
55
<https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html>`_,
6-
docstrings can also be included in the documentation. The following three
7-
directives can be specified:
6+
docstrings can also be included in the documentation. The following directives
7+
can be specified
88

99
.. rst:directive:: automodule
1010
autoclass
1111
autoexception
1212
13+
… for function-like objects:
14+
15+
.. rst:directive:: autofunction
16+
automethod
17+
autoproperty
18+
autodecorator
19+
20+
… for data and attributes:
21+
22+
23+
.. rst:directive:: autodata
24+
autoattribute
25+
1326
These document a module, a class or an exception using the docstring of the
1427
respective object.
1528

@@ -58,126 +71,111 @@ Here you can find some examples from the documentation of the Python
5871

5972
This leads to the :doc:`autodoc-examples`.
6073

61-
.. note::
62-
You should follow these guidelines when writing docstrings:
74+
Guidelines
75+
----------
6376

64-
* :pep:`8#comments`
65-
* :pep:`257#specification`
77+
In :pep:`8` there is only a brief reference to conventions for a good docstring:
78+
:pep:`Documentation Strings <8#comments>`. Other :abbr:`PEPs (Python Enhancement
79+
Proposals)` refer to the :pep:`Docstring Processing System Framework <256>`:
6680

67-
``sphinx-autodoc-typehints``
68-
----------------------------
81+
:pep:`257`
82+
describes docstring conventions:
6983

70-
With :pep:`484` a standard method for expressing types in Python code was
71-
introduced. This also allows types to be expressed differently in docstrings.
72-
The variant with types according to :pep:`484` has the advantage that type
73-
testers and IDEs can be used for static code analysis.
84+
* What should be documented where?
85+
* The first line should be a one-line summary.
86+
87+
:pep:`258`
88+
specifies the docstring processing system.
89+
90+
:pep:`287`
91+
specifies the docstring syntax.
7492

75-
Python 3 type annotations:
93+
The `Google Python Style Guide
94+
<https://google.github.io/styleguide/pyguide.html>`_ also provides more specific
95+
guidelines for `RPython comments and docstrings
96+
<https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings>`_.
97+
The `NumPy style guide <https://numpydoc.readthedocs.io/en/latest/format.html>`_
98+
also provides further `docstring standards
99+
<https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard>`_.
100+
The main difference between the two is that Google uses indentation and NumPy
101+
uses underlining:
102+
103+
Google Python Style Guide:
76104
.. code-block:: python
77105
78-
def func(arg1: int, arg2: str) -> bool:
106+
def func(arg1, arg2):
79107
"""Summary line.
80108
81109
Extended description of function.
82110
83111
Args:
84-
arg1: Description of arg1
85-
arg2: Description of arg2
112+
arg1 (int): Description of arg1
113+
arg2 (str): Description of arg2
86114
87115
Returns:
88-
Description of return value
116+
bool: Description of return value
89117
90118
"""
91119
return True
92120
93-
Types in Docstrings:
121+
NumPy Style Guide:
94122
.. code-block:: python
95123
96124
def func(arg1, arg2):
97125
"""Summary line.
98126
99127
Extended description of function.
100128
101-
Args:
102-
arg1 (int): Description of arg1
103-
arg2 (str): Description of arg2
129+
Parameters
130+
----------
131+
arg1 : int
132+
Description of arg1
133+
arg2 : str
134+
Description of arg2
104135
105-
Returns:
106-
bool: Description of return value
136+
Returns
137+
-------
138+
bool
139+
Description of return value
107140
108141
"""
109142
return True
110143
111-
.. note::
112-
:pep:`484#suggested-syntax-for-python-2-7-and-straddling-code` are currently
113-
not supported by Sphinx and do not appear in the generated documentation.
114-
115144
.. _napoleon:
116145

117146
``sphinx.ext.napoleon``
118-
-----------------------
147+
~~~~~~~~~~~~~~~~~~~~~~~
119148

120-
The sphinx extension `sphinx.ext.napoleon
121-
<https://sphinxcontrib-napoleon.readthedocs.io/>`_ allows you to define
122-
different sections in docstrings, including:
149+
The Sphinx extension `sphinx.ext.napoleon
150+
<https://sphinxcontrib-napoleon.readthedocs.io/>`_ processes docstrings that
151+
correspond to both the Google Python Style Guide and the NumPy Style Guide:
123152

124-
* ``Attributes``
125-
* ``Example``
126-
* ``Keyword Arguments``
127-
* ``Methods``
128-
* ``Parameters``
129-
* ``Warning``
130-
* ``Yield``
131-
132-
There are two styles of docstrings in ``sphinx.ext.napoleon``:
153+
You can find the detailed configuration options in
154+
`sphinxcontrib.napoleon.Config
155+
<https://sphinxcontrib-napoleon.readthedocs.io/en/latest/sphinxcontrib.napoleon.html#sphinxcontrib.napoleon.Config>`_.
133156

134-
* `Google <https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html>`_
135-
* `NumPy <https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_numpy.html>`_
157+
``sphinx-autodoc-typehints``
158+
----------------------------
136159

137-
The main differences are that Google uses indentations and NumPy uses
138-
underscores:
160+
With :pep:`484` a standard method for expressing types in Python code was
161+
introduced. This also allows types to be expressed differently in docstrings.
162+
The variant with types according to :pep:`484` has the advantage that type
163+
testers and IDEs can be used for static code analysis.
139164

140-
Google:
165+
Python 3 Type annotations in Docstrings:
141166
.. code-block:: python
142167
143-
def func(arg1, arg2):
168+
def func(arg1: int, arg2: str) -> bool:
144169
"""Summary line.
145170
146171
Extended description of function.
147172
148173
Args:
149-
arg1 (int): Description of arg1
150-
arg2 (str): Description of arg2
174+
arg1: Description of arg1
175+
arg2: Description of arg2
151176
152177
Returns:
153-
bool: Description of return value
154-
155-
"""
156-
return True
157-
158-
NumPy:
159-
.. code-block:: python
160-
161-
def func(arg1, arg2):
162-
"""Summary line.
163-
164-
Extended description of function.
165-
166-
Parameters
167-
----------
168-
arg1 : int
169-
Description of arg1
170-
arg2 : str
171-
Description of arg2
172-
173-
Returns
174-
-------
175-
bool
176178
Description of return value
177179
178180
"""
179181
return True
180-
181-
You can find the detailed configuration options in
182-
`sphinxcontrib.napoleon.Config
183-
<https://sphinxcontrib-napoleon.readthedocs.io/en/latest/sphinxcontrib.napoleon.html#sphinxcontrib.napoleon.Config>`_.

0 commit comments

Comments
 (0)