Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit a1a4d0b

Browse files
johanfleuryNurdok
authored andcommitted
fix(checker): allow whitespace before inner functions and class (#426)
* fix(checker): allow whitespace before inner functions and class Changes D202: ("No blank lines allowed after function docstring") to allow space below function docstrings with inner functions. i.e. allows: ```python def outer(): """Valid docstring.""" def inner(): pass return pass ``` See comment from @cdeil in #361. * fix(checker): use raw string for all regex Avoid DeprecationWarning ("invalid escape sequence").
1 parent 4aa5d96 commit a1a4d0b

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

docs/release_notes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Bug Fixes
3333
* Fix decorator parsing (#411).
3434
* Google-style sections no longer cause false errors when used with
3535
Numpy-style sections (#388, #424).
36+
* D202: Allow a blank line after function docstring when followed by
37+
declaration of an inner function or class (#395, #426).
3638
* Fix D401 and D404 checks not working for docstrings containing only one word and ending with non-alpha character (#421)
3739

3840
4.0.1 - August 14th, 2019
@@ -48,7 +50,6 @@ Bug Fixes
4850
the violation code table (#396).
4951
* Fixed IndentationError when parsing function arguments (#392).
5052

51-
5253
4.0.0 - July 6th, 2019
5354
----------------------
5455

src/pydocstyle/checker.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ def check_one_liners(self, definition, docstring):
186186
def check_no_blank_before(self, function, docstring): # def
187187
"""D20{1,2}: No blank lines allowed around function/method docstring.
188188
189-
There's no blank line either before or after the docstring.
190-
189+
There's no blank line either before or after the docstring unless directly
190+
followed by an inner function or class.
191191
"""
192192
if docstring:
193193
before, _, after = function.source.partition(docstring)
@@ -198,7 +198,14 @@ def check_no_blank_before(self, function, docstring): # def
198198
if blanks_before_count != 0:
199199
yield violations.D201(blanks_before_count)
200200
if not all(blanks_after) and blanks_after_count != 0:
201-
yield violations.D202(blanks_after_count)
201+
# Report a D202 violation if the docstring is followed by a blank line
202+
# and the blank line is not itself followed by an inner function or
203+
# class.
204+
if not (
205+
blanks_after_count == 1 and
206+
re(r"\s+(?:(?:class|def)\s|@)").match(after)
207+
):
208+
yield violations.D202(blanks_after_count)
202209

203210
@check_for(Class)
204211
def check_blank_before_after_class(self, class_, docstring):
@@ -976,15 +983,15 @@ def is_ascii(string):
976983

977984
def leading_space(string):
978985
"""Return any leading space from `string`."""
979-
return re('\s*').match(string).group()
986+
return re(r'\s*').match(string).group()
980987

981988

982989
def get_leading_words(line):
983990
"""Return any leading set of words from `line`.
984991
985992
For example, if `line` is " Hello world!!!", returns "Hello world".
986993
"""
987-
result = re("[\w ]+").match(line.strip())
994+
result = re(r"[\w ]+").match(line.strip())
988995
if result is not None:
989996
return result.group()
990997

src/tests/test_cases/functions.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""A valid module docstrings."""
2+
3+
from .expected import Expectation
4+
5+
expectation = Expectation()
6+
expect = expectation.expect
7+
8+
9+
@expect("D201: No blank lines allowed before function docstring (found 1)")
10+
def func_with_space_before():
11+
12+
"""Test a function with space before docstring."""
13+
pass
14+
15+
16+
@expect("D202: No blank lines allowed after function docstring (found 1)")
17+
def func_with_space_after():
18+
"""Test a function with space after docstring."""
19+
20+
pass
21+
22+
23+
def func_with_inner_func_after():
24+
"""Test a function with inner function after docstring."""
25+
26+
def inner():
27+
pass
28+
29+
pass
30+
31+
32+
def fake_decorator(decorated):
33+
"""Fake decorator used to test decorated inner func."""
34+
return decorated
35+
36+
37+
def func_with_inner_decorated_func_after():
38+
"""Test a function with inner decorated function after docstring."""
39+
40+
@fake_decorator
41+
def inner():
42+
pass
43+
44+
pass
45+
46+
47+
def func_with_inner_class_after():
48+
"""Test a function with inner class after docstring."""
49+
50+
class inner():
51+
pass
52+
53+
pass

src/tests/test_definitions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'superfluous_quotes',
2020
'noqa',
2121
'sections',
22+
'functions',
2223
'canonical_google_examples',
2324
'canonical_numpy_examples',
2425
'canonical_pep257_examples',

0 commit comments

Comments
 (0)