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

Commit ef66fb3

Browse files
committed
#129 - CR fixes
1 parent 52034d2 commit ef66fb3

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

src/pydocstyle/checker.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -423,29 +423,42 @@ def _get_leading_words(line):
423423

424424
@staticmethod
425425
def _is_a_docstring_section(context):
426-
"""Check if the suspected line is really a section.
426+
"""Check if the suspected context is really a section header.
427427
428-
This is done by checking the following conditions:
429-
* Does the current line has a suffix after the suspected section name?
430-
* Is the previous line not empty?
431-
* Does the previous line end with a punctuation mark?
432-
433-
If so, this is probably not a real section name. For example:
428+
Lets have a look at the following example docstring:
434429
'''Title.
435430
436431
Some part of the docstring that specifies what the function
437-
returns. <----- Not a real section name.
432+
returns. <----- Not a real section name. It has a suffix and the
433+
previous line is not empty and does not end with
434+
a punctuation sign.
435+
436+
This is another line in the docstring. It describes stuff,
437+
but we forgot to add a blank line between it and the section name.
438+
Returns <----- A real section name. The previous line ends with
439+
------- a period, therefore it is in a new
440+
grammatical context.
441+
Bla.
442+
438443
'''
444+
445+
To make sure this is really a section we check these conditions:
446+
* There's no suffix to the section name.
447+
* The previous line ends with punctuation.
448+
* The previous line is empty.
449+
450+
If one of the conditions is true, we will consider the line as
451+
a section name.
439452
"""
440453
section_name_suffix = context.line.lstrip(context.section_name).strip()
441454

442455
punctuation = [',', ';', '.', '-', '\\', '/', ']', '}', ')']
443456
prev_line_ends_with_punctuation = \
444457
any(context.previous_line.strip().endswith(x) for x in punctuation)
445458

446-
return not (section_name_suffix != '' and not
447-
prev_line_ends_with_punctuation and not
448-
context.previous_line.strip() == '')
459+
return (is_blank(section_name_suffix) or
460+
prev_line_ends_with_punctuation or
461+
is_blank(context.previous_line))
449462

450463
@classmethod
451464
def _check_section_underline(cls, section_name, context, indentation):
@@ -466,7 +479,7 @@ def _check_section_underline(cls, section_name, context, indentation):
466479

467480
for line in context.following_lines:
468481
line_set = ''.join(set(line.strip()))
469-
if line_set != '':
482+
if not is_blank(line_set):
470483
dash_line_found = line_set == '-'
471484
break
472485
next_non_empty_line_offset += 1
@@ -481,13 +494,13 @@ def _check_section_underline(cls, section_name, context, indentation):
481494

482495
dash_line = context.following_lines[next_non_empty_line_offset]
483496
if dash_line.strip() != "-" * len(section_name):
484-
yield violations.D409(section_name,
485-
len(section_name),
497+
yield violations.D409(len(section_name),
498+
section_name,
486499
len(dash_line.strip()))
487500

488501
line_after_dashes = \
489502
context.following_lines[next_non_empty_line_offset + 1]
490-
if line_after_dashes.strip() != '':
503+
if not is_blank(line_after_dashes):
491504
yield violations.D410(section_name)
492505

493506
if leading_space(dash_line) > indentation:
@@ -516,10 +529,10 @@ def _check_section(cls, docstring, definition, context):
516529
yield violations.D214(capitalized_section)
517530

518531
suffix = context.line.strip().lstrip(context.section_name)
519-
if suffix != '':
532+
if suffix:
520533
yield violations.D406(capitalized_section, context.line.strip())
521534

522-
if context.previous_line.strip() != '':
535+
if not is_blank(context.previous_line):
523536
yield violations.D411(capitalized_section)
524537

525538
for err in cls._check_section_underline(capitalized_section,
@@ -564,15 +577,16 @@ def _suspected_as_section(_line):
564577
suspected_section_indices = [i for i, line in enumerate(lines) if
565578
_suspected_as_section(line)]
566579

567-
context = namedtuple('SectionContext', ('section_name',
568-
'previous_line',
569-
'line',
570-
'following_lines'))
580+
SectionContext = namedtuple('SectionContext', ('section_name',
581+
'previous_line',
582+
'line',
583+
'following_lines'))
571584

572-
contexts = (context(self._get_leading_words(lines[i].strip()),
573-
lines[i - 1],
574-
lines[i],
575-
lines[i + 1:]) for i in suspected_section_indices)
585+
contexts = (SectionContext(self._get_leading_words(lines[i].strip()),
586+
lines[i - 1],
587+
lines[i],
588+
lines[i + 1:])
589+
for i in suspected_section_indices)
576590

577591
for ctx in contexts:
578592
if self._is_a_docstring_section(ctx):

src/pydocstyle/violations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def to_rst(cls):
220220
'{0!r}')
221221
D409 = D4xx.create_error('D409', 'Section underline should match the length '
222222
'of its name',
223-
'len({0!r}) == {1!r}, got {2!r} dashes')
223+
'Expected {0!r} dashes in section {1!r}, got {2!r}')
224224
D410 = D4xx.create_error('D410', 'Missing blank line after section', '{0!r}')
225225
D411 = D4xx.create_error('D411', 'Missing blank line before section', '{0!r}')
226226

src/tests/test_cases/sections.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def no_underline():
4545

4646
@expect(_D213)
4747
@expect("D408: Section underline should be in the line following the "
48-
"section\'s name ('Returns')")
48+
"section's name ('Returns')")
4949
def blank_line_before_underline():
5050
"""Valid headline.
5151
@@ -58,7 +58,7 @@ def blank_line_before_underline():
5858

5959
@expect(_D213)
6060
@expect("D409: Section underline should match the length of its name "
61-
"(len('Returns') == 7, got 2 dashes)")
61+
"(Expected 7 dashes in section 'Returns', got 2)")
6262
def bad_underline_length():
6363
"""Valid headline.
6464
@@ -140,7 +140,7 @@ def section_name_in_first_line():
140140
@expect("D405: Section name should be properly capitalized "
141141
"('Short Summary', not 'Short summary')")
142142
@expect("D409: Section underline should match the length of its name "
143-
"(len('Returns') == 7, got 6 dashes)")
143+
"(Expected 7 dashes in section 'Returns', got 6)")
144144
@expect("D410: Missing blank line after section ('Returns')")
145145
@expect("D411: Missing blank line before section ('Raises')")
146146
@expect("D406: Section name should end with a newline "

src/tests/test_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def test_illegal_convention(env):
436436
assert "Illegal convention 'illegal_conv'." in err
437437
assert 'Possible conventions' in err
438438
assert 'pep257' in err
439+
assert 'numpy' in err
439440

440441

441442
def test_empty_select_cli(env):

0 commit comments

Comments
 (0)