Skip to content

Commit 0dd5e16

Browse files
authored
Merge pull request #7603 from bluetech/flake8-docstrings
Enforce some pydocstyle lints with flake8-docstrings & docstring fixes in testing/
2 parents 701998b + 9a18b57 commit 0dd5e16

Some content is hidden

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

42 files changed

+189
-297
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ repos:
2525
hooks:
2626
- id: flake8
2727
language_version: python3
28-
additional_dependencies: [flake8-typing-imports==1.9.0]
28+
additional_dependencies:
29+
- flake8-typing-imports==1.9.0
30+
- flake8-docstrings==1.5.0
2931
- repo: https://github.com/asottile/reorder_python_imports
3032
rev: v2.3.0
3133
hooks:

doc/en/example/nonpython/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def pytest_collect_file(parent, path):
99

1010
class YamlFile(pytest.File):
1111
def collect(self):
12-
import yaml # we need a yaml parser, e.g. PyYAML
12+
# We need a yaml parser, e.g. PyYAML.
13+
import yaml
1314

1415
raw = yaml.safe_load(self.fspath.open())
1516
for name, spec in sorted(raw.items()):
@@ -23,12 +24,12 @@ def __init__(self, name, parent, spec):
2324

2425
def runtest(self):
2526
for name, value in sorted(self.spec.items()):
26-
# some custom test execution (dumb example follows)
27+
# Some custom test execution (dumb example follows).
2728
if name != value:
2829
raise YamlException(self, name, value)
2930

3031
def repr_failure(self, excinfo):
31-
""" called when self.runtest() raises an exception. """
32+
"""Called when self.runtest() raises an exception."""
3233
if isinstance(excinfo.value, YamlException):
3334
return "\n".join(
3435
[
@@ -43,4 +44,4 @@ def reportinfo(self):
4344

4445

4546
class YamlException(Exception):
46-
""" custom exception for error reporting. """
47+
"""Custom exception for error reporting."""

scripts/release.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Invoke development tasks.
3-
"""
1+
"""Invoke development tasks."""
42
import argparse
53
import os
64
from pathlib import Path

testing/acceptance_test.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,12 @@ def pytest_sessionfinish(exitstatus):
403403
result.stdout.fnmatch_lines(["pytest_sessionfinish_called"])
404404
assert result.ret == ExitCode.USAGE_ERROR
405405

406-
@pytest.mark.usefixtures("recwarn")
407406
def test_namespace_import_doesnt_confuse_import_hook(self, testdir):
408-
"""
409-
Ref #383. Python 3.3's namespace package messed with our import hooks
407+
"""Ref #383.
408+
409+
Python 3.3's namespace package messed with our import hooks.
410410
Importing a module that didn't exist, even if the ImportError was
411411
gracefully handled, would make our test crash.
412-
413-
Use recwarn here to silence this warning in Python 2.7:
414-
ImportWarning: Not importing directory '...\not_a_package': missing __init__.py
415412
"""
416413
testdir.mkdir("not_a_package")
417414
p = testdir.makepyfile(
@@ -457,10 +454,8 @@ def test_foo(invalid_fixture):
457454
)
458455

459456
def test_plugins_given_as_strings(self, tmpdir, monkeypatch, _sys_snapshot):
460-
"""test that str values passed to main() as `plugins` arg
461-
are interpreted as module names to be imported and registered.
462-
#855.
463-
"""
457+
"""Test that str values passed to main() as `plugins` arg are
458+
interpreted as module names to be imported and registered (#855)."""
464459
with pytest.raises(ImportError) as excinfo:
465460
pytest.main([str(tmpdir)], plugins=["invalid.module"])
466461
assert "invalid" in str(excinfo.value)
@@ -664,8 +659,7 @@ def test_cmdline_python_package(self, testdir, monkeypatch):
664659
result.stderr.fnmatch_lines(["*not*found*test_missing*"])
665660

666661
def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
667-
"""
668-
test --pyargs option with namespace packages (#1567)
662+
"""Test --pyargs option with namespace packages (#1567).
669663
670664
Ref: https://packaging.python.org/guides/packaging-namespace-packages/
671665
"""
@@ -1011,9 +1005,7 @@ def test_pytest_plugins_as_module(testdir):
10111005

10121006

10131007
def test_deferred_hook_checking(testdir):
1014-
"""
1015-
Check hooks as late as possible (#1821).
1016-
"""
1008+
"""Check hooks as late as possible (#1821)."""
10171009
testdir.syspathinsert()
10181010
testdir.makepyfile(
10191011
**{
@@ -1089,8 +1081,7 @@ def test2():
10891081

10901082

10911083
def test_fixture_order_respects_scope(testdir):
1092-
"""Ensure that fixtures are created according to scope order, regression test for #2405
1093-
"""
1084+
"""Ensure that fixtures are created according to scope order (#2405)."""
10941085
testdir.makepyfile(
10951086
"""
10961087
import pytest
@@ -1115,7 +1106,8 @@ def test_value():
11151106

11161107

11171108
def test_frame_leak_on_failing_test(testdir):
1118-
"""pytest would leak garbage referencing the frames of tests that failed that could never be reclaimed (#2798)
1109+
"""Pytest would leak garbage referencing the frames of tests that failed
1110+
that could never be reclaimed (#2798).
11191111
11201112
Unfortunately it was not possible to remove the actual circles because most of them
11211113
are made of traceback objects which cannot be weakly referenced. Those objects at least

testing/code/test_excinfo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def f(x):
464464
assert lines[1] == " pass"
465465

466466
def test_repr_source_excinfo(self) -> None:
467-
""" check if indentation is right """
467+
"""Check if indentation is right."""
468468
try:
469469

470470
def f():

testing/freeze/create_executable.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Generates an executable with pytest runner embedded using PyInstaller.
3-
"""
1+
"""Generate an executable with pytest runner embedded using PyInstaller."""
42
if __name__ == "__main__":
53
import pytest
64
import subprocess

testing/logging/test_fixture.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ def test_log_level_override(request, caplog):
268268

269269

270270
def test_log_report_captures_according_to_config_option_upon_failure(testdir):
271-
""" Test that upon failure:
272-
(1) `caplog` succeeded to capture the DEBUG message and assert on it => No `Exception` is raised
273-
(2) The `DEBUG` message does NOT appear in the `Captured log call` report
274-
(3) The stdout, `INFO`, and `WARNING` messages DO appear in the test reports due to `--log-level=INFO`
271+
"""Test that upon failure:
272+
(1) `caplog` succeeded to capture the DEBUG message and assert on it => No `Exception` is raised.
273+
(2) The `DEBUG` message does NOT appear in the `Captured log call` report.
274+
(3) The stdout, `INFO`, and `WARNING` messages DO appear in the test reports due to `--log-level=INFO`.
275275
"""
276276
testdir.makepyfile(
277277
"""

testing/logging/test_reporting.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,10 +1066,8 @@ def test_second():
10661066

10671067

10681068
def test_colored_captured_log(testdir):
1069-
"""
1070-
Test that the level names of captured log messages of a failing test are
1071-
colored.
1072-
"""
1069+
"""Test that the level names of captured log messages of a failing test
1070+
are colored."""
10731071
testdir.makepyfile(
10741072
"""
10751073
import logging
@@ -1092,9 +1090,7 @@ def test_foo():
10921090

10931091

10941092
def test_colored_ansi_esc_caplogtext(testdir):
1095-
"""
1096-
Make sure that caplog.text does not contain ANSI escape sequences.
1097-
"""
1093+
"""Make sure that caplog.text does not contain ANSI escape sequences."""
10981094
testdir.makepyfile(
10991095
"""
11001096
import logging
@@ -1111,8 +1107,7 @@ def test_foo(caplog):
11111107

11121108

11131109
def test_logging_emit_error(testdir: Testdir) -> None:
1114-
"""
1115-
An exception raised during emit() should fail the test.
1110+
"""An exception raised during emit() should fail the test.
11161111
11171112
The default behavior of logging is to print "Logging error"
11181113
to stderr with the call stack and some extra details.
@@ -1138,10 +1133,8 @@ def test_bad_log():
11381133

11391134

11401135
def test_logging_emit_error_supressed(testdir: Testdir) -> None:
1141-
"""
1142-
If logging is configured to silently ignore errors, pytest
1143-
doesn't propagate errors either.
1144-
"""
1136+
"""If logging is configured to silently ignore errors, pytest
1137+
doesn't propagate errors either."""
11451138
testdir.makepyfile(
11461139
"""
11471140
import logging

testing/python/approx.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,7 @@ def test_expected_value_type_error(self, x):
488488
],
489489
)
490490
def test_comparison_operator_type_error(self, op):
491-
"""
492-
pytest.approx should raise TypeError for operators other than == and != (#2003).
493-
"""
491+
"""pytest.approx should raise TypeError for operators other than == and != (#2003)."""
494492
with pytest.raises(TypeError):
495493
op(1, approx(1, rel=1e-6, abs=1e-12))
496494

testing/python/collect.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -984,8 +984,7 @@ def test_traceback_error_during_import(self, testdir):
984984
result.stdout.fnmatch_lines([">*asd*", "E*NameError*"])
985985

986986
def test_traceback_filter_error_during_fixture_collection(self, testdir):
987-
"""integration test for issue #995.
988-
"""
987+
"""Integration test for issue #995."""
989988
testdir.makepyfile(
990989
"""
991990
import pytest
@@ -1011,8 +1010,9 @@ def test_failing_fixture(fail_fixture):
10111010
result.stdout.fnmatch_lines(["*ValueError: fail me*", "* 1 error in *"])
10121011

10131012
def test_filter_traceback_generated_code(self) -> None:
1014-
"""test that filter_traceback() works with the fact that
1013+
"""Test that filter_traceback() works with the fact that
10151014
_pytest._code.code.Code.path attribute might return an str object.
1015+
10161016
In this case, one of the entries on the traceback was produced by
10171017
dynamically generated code.
10181018
See: https://bitbucket.org/pytest-dev/py/issues/71
@@ -1033,8 +1033,9 @@ def test_filter_traceback_generated_code(self) -> None:
10331033
assert not filter_traceback(traceback[-1])
10341034

10351035
def test_filter_traceback_path_no_longer_valid(self, testdir) -> None:
1036-
"""test that filter_traceback() works with the fact that
1036+
"""Test that filter_traceback() works with the fact that
10371037
_pytest._code.code.Code.path attribute might return an str object.
1038+
10381039
In this case, one of the files in the traceback no longer exists.
10391040
This fixes #1133.
10401041
"""
@@ -1250,8 +1251,7 @@ def test_injection(self):
12501251

12511252

12521253
def test_syntax_error_with_non_ascii_chars(testdir):
1253-
"""Fix decoding issue while formatting SyntaxErrors during collection (#578)
1254-
"""
1254+
"""Fix decoding issue while formatting SyntaxErrors during collection (#578)."""
12551255
testdir.makepyfile("☃")
12561256
result = testdir.runpytest()
12571257
result.stdout.fnmatch_lines(["*ERROR collecting*", "*SyntaxError*", "*1 error in*"])

testing/python/fixtures.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,10 +1684,8 @@ def test_func2(request):
16841684
reprec.assertoutcome(passed=2)
16851685

16861686
def test_callables_nocode(self, testdir):
1687-
"""
1688-
an imported mock.call would break setup/factory discovery
1689-
due to it being callable and __code__ not being a code object
1690-
"""
1687+
"""An imported mock.call would break setup/factory discovery due to
1688+
it being callable and __code__ not being a code object."""
16911689
testdir.makepyfile(
16921690
"""
16931691
class _call(tuple):
@@ -3333,9 +3331,7 @@ def fixture1(self):
33333331
)
33343332

33353333
def test_show_fixtures_different_files(self, testdir):
3336-
"""
3337-
#833: --fixtures only shows fixtures from first file
3338-
"""
3334+
"""`--fixtures` only shows fixtures from first file (#833)."""
33393335
testdir.makepyfile(
33403336
test_a='''
33413337
import pytest

0 commit comments

Comments
 (0)