Skip to content

Commit 49827ad

Browse files
authored
Merge pull request pytest-dev#7510 from bluetech/docstrings
Format docstrings in a consistent style
2 parents 6882c03 + cbec0f8 commit 49827ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1633
-1697
lines changed

CONTRIBUTING.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,38 @@ without using a local copy. This can be convenient for small fixes.
8989
The built documentation should be available in ``doc/en/_build/html``,
9090
where 'en' refers to the documentation language.
9191

92+
Pytest has an API reference which in large part is
93+
`generated automatically <https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html>`_
94+
from the docstrings of the documented items. Pytest uses the
95+
`Sphinx docstring format <https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html>`_.
96+
For example:
97+
98+
.. code-block:: python
99+
100+
def my_function(arg: ArgType) -> Foo:
101+
"""Do important stuff.
102+
103+
More detailed info here, in separate paragraphs from the subject line.
104+
Use proper sentences -- start sentences with capital letters and end
105+
with periods.
106+
107+
Can include annotated documentation:
108+
109+
:param short_arg: An argument which determines stuff.
110+
:param long_arg:
111+
A long explanation which spans multiple lines, overflows
112+
like this.
113+
:returns: The result.
114+
:raises ValueError:
115+
Detailed information when this can happen.
116+
117+
.. versionadded:: 6.0
118+
119+
Including types into the annotations above is not necessary when
120+
type-hinting is being used (as in this example).
121+
"""
122+
123+
92124
.. _submitplugin:
93125

94126
Submitting Plugins to pytest-dev

doc/en/reference.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Mark a test function as using the given fixture names.
182182

183183
.. py:function:: pytest.mark.usefixtures(*names)
184184
185-
:param args: the names of the fixture to use, as strings
185+
:param args: The names of the fixture to use, as strings.
186186

187187
.. note::
188188

@@ -209,8 +209,10 @@ Marks a test function as *expected to fail*.
209209
Condition for marking the test function as xfail (``True/False`` or a
210210
:ref:`condition string <string conditions>`). If a bool, you also have
211211
to specify ``reason`` (see :ref:`condition string <string conditions>`).
212-
:keyword str reason: Reason why the test function is marked as xfail.
213-
:keyword Exception raises: Exception subclass expected to be raised by the test function; other exceptions will fail the test.
212+
:keyword str reason:
213+
Reason why the test function is marked as xfail.
214+
:keyword Type[Exception] raises:
215+
Exception subclass expected to be raised by the test function; other exceptions will fail the test.
214216
:keyword bool run:
215217
If the test function should actually be executed. If ``False``, the function will always xfail and will
216218
not be executed (useful if a function is segfaulting).
@@ -224,7 +226,7 @@ Marks a test function as *expected to fail*.
224226
a new release of a library fixes a known bug).
225227

226228

227-
custom marks
229+
Custom marks
228230
~~~~~~~~~~~~
229231

230232
Marks are created dynamically using the factory object ``pytest.mark`` and applied as a decorator.
@@ -473,7 +475,7 @@ caplog
473475
.. autofunction:: _pytest.logging.caplog()
474476
:no-auto-options:
475477

476-
This returns a :class:`_pytest.logging.LogCaptureFixture` instance.
478+
Returns a :class:`_pytest.logging.LogCaptureFixture` instance.
477479

478480
.. autoclass:: _pytest.logging.LogCaptureFixture
479481
:members:
@@ -491,7 +493,7 @@ monkeypatch
491493
.. autofunction:: _pytest.monkeypatch.monkeypatch()
492494
:no-auto-options:
493495

494-
This returns a :class:`MonkeyPatch` instance.
496+
Returns a :class:`MonkeyPatch` instance.
495497

496498
.. autoclass:: _pytest.monkeypatch.MonkeyPatch
497499
:members:

src/_pytest/_argcomplete.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""allow bash-completion for argparse with argcomplete if installed
2-
needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail
1+
"""Allow bash-completion for argparse with argcomplete if installed.
2+
3+
Needs argcomplete>=0.5.6 for python 3.2/3.3 (older versions fail
34
to find the magic string, so _ARGCOMPLETE env. var is never set, and
4-
this does not need special code.
5+
this does not need special code).
56
67
Function try_argcomplete(parser) should be called directly before
78
the call to ArgumentParser.parse_args().
@@ -10,8 +11,7 @@
1011
arguments specification, in order to get "dirname/" after "dirn<TAB>"
1112
instead of the default "dirname ":
1213
13-
optparser.add_argument(Config._file_or_dir, nargs='*'
14-
).completer=filescompleter
14+
optparser.add_argument(Config._file_or_dir, nargs='*').completer=filescompleter
1515
1616
Other, application specific, completers should go in the file
1717
doing the add_argument calls as they need to be specified as .completer
@@ -20,35 +20,43 @@
2020
2121
SPEEDUP
2222
=======
23+
2324
The generic argcomplete script for bash-completion
24-
(/etc/bash_completion.d/python-argcomplete.sh )
25+
(/etc/bash_completion.d/python-argcomplete.sh)
2526
uses a python program to determine startup script generated by pip.
2627
You can speed up completion somewhat by changing this script to include
2728
# PYTHON_ARGCOMPLETE_OK
2829
so the the python-argcomplete-check-easy-install-script does not
2930
need to be called to find the entry point of the code and see if that is
30-
marked with PYTHON_ARGCOMPLETE_OK
31+
marked with PYTHON_ARGCOMPLETE_OK.
3132
3233
INSTALL/DEBUGGING
3334
=================
35+
3436
To include this support in another application that has setup.py generated
3537
scripts:
36-
- add the line:
38+
39+
- Add the line:
3740
# PYTHON_ARGCOMPLETE_OK
38-
near the top of the main python entry point
39-
- include in the file calling parse_args():
41+
near the top of the main python entry point.
42+
43+
- Include in the file calling parse_args():
4044
from _argcomplete import try_argcomplete, filescompleter
41-
, call try_argcomplete just before parse_args(), and optionally add
42-
filescompleter to the positional arguments' add_argument()
45+
Call try_argcomplete just before parse_args(), and optionally add
46+
filescompleter to the positional arguments' add_argument().
47+
4348
If things do not work right away:
44-
- switch on argcomplete debugging with (also helpful when doing custom
49+
50+
- Switch on argcomplete debugging with (also helpful when doing custom
4551
completers):
4652
export _ARC_DEBUG=1
47-
- run:
53+
54+
- Run:
4855
python-argcomplete-check-easy-install-script $(which appname)
4956
echo $?
50-
will echo 0 if the magic line has been found, 1 if not
51-
- sometimes it helps to find early on errors using:
57+
will echo 0 if the magic line has been found, 1 if not.
58+
59+
- Sometimes it helps to find early on errors using:
5260
_ARGCOMPLETE=1 _ARC_DEBUG=1 appname
5361
which should throw a KeyError: 'COMPLINE' (which is properly set by the
5462
global argcomplete script).
@@ -63,29 +71,29 @@
6371

6472

6573
class FastFilesCompleter:
66-
"Fast file completer class"
74+
"""Fast file completer class."""
6775

6876
def __init__(self, directories: bool = True) -> None:
6977
self.directories = directories
7078

7179
def __call__(self, prefix: str, **kwargs: Any) -> List[str]:
72-
"""only called on non option completions"""
80+
# Only called on non option completions.
7381
if os.path.sep in prefix[1:]:
7482
prefix_dir = len(os.path.dirname(prefix) + os.path.sep)
7583
else:
7684
prefix_dir = 0
7785
completion = []
7886
globbed = []
7987
if "*" not in prefix and "?" not in prefix:
80-
# we are on unix, otherwise no bash
88+
# We are on unix, otherwise no bash.
8189
if not prefix or prefix[-1] == os.path.sep:
8290
globbed.extend(glob(prefix + ".*"))
8391
prefix += "*"
8492
globbed.extend(glob(prefix))
8593
for x in sorted(globbed):
8694
if os.path.isdir(x):
8795
x += "/"
88-
# append stripping the prefix (like bash, not like compgen)
96+
# Append stripping the prefix (like bash, not like compgen).
8997
completion.append(x[prefix_dir:])
9098
return completion
9199

0 commit comments

Comments
 (0)