@@ -423,29 +423,42 @@ def _get_leading_words(line):
423
423
424
424
@staticmethod
425
425
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 .
427
427
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:
434
429
'''Title.
435
430
436
431
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
+
438
443
'''
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.
439
452
"""
440
453
section_name_suffix = context .line .lstrip (context .section_name ).strip ()
441
454
442
455
punctuation = [',' , ';' , '.' , '-' , '\\ ' , '/' , ']' , '}' , ')' ]
443
456
prev_line_ends_with_punctuation = \
444
457
any (context .previous_line .strip ().endswith (x ) for x in punctuation )
445
458
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 ) )
449
462
450
463
@classmethod
451
464
def _check_section_underline (cls , section_name , context , indentation ):
@@ -466,7 +479,7 @@ def _check_section_underline(cls, section_name, context, indentation):
466
479
467
480
for line in context .following_lines :
468
481
line_set = '' .join (set (line .strip ()))
469
- if line_set != '' :
482
+ if not is_blank ( line_set ) :
470
483
dash_line_found = line_set == '-'
471
484
break
472
485
next_non_empty_line_offset += 1
@@ -481,13 +494,13 @@ def _check_section_underline(cls, section_name, context, indentation):
481
494
482
495
dash_line = context .following_lines [next_non_empty_line_offset ]
483
496
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 ,
486
499
len (dash_line .strip ()))
487
500
488
501
line_after_dashes = \
489
502
context .following_lines [next_non_empty_line_offset + 1 ]
490
- if line_after_dashes . strip () != '' :
503
+ if not is_blank ( line_after_dashes ) :
491
504
yield violations .D410 (section_name )
492
505
493
506
if leading_space (dash_line ) > indentation :
@@ -516,10 +529,10 @@ def _check_section(cls, docstring, definition, context):
516
529
yield violations .D214 (capitalized_section )
517
530
518
531
suffix = context .line .strip ().lstrip (context .section_name )
519
- if suffix != '' :
532
+ if suffix :
520
533
yield violations .D406 (capitalized_section , context .line .strip ())
521
534
522
- if context .previous_line . strip () != '' :
535
+ if not is_blank ( context .previous_line ) :
523
536
yield violations .D411 (capitalized_section )
524
537
525
538
for err in cls ._check_section_underline (capitalized_section ,
@@ -564,15 +577,16 @@ def _suspected_as_section(_line):
564
577
suspected_section_indices = [i for i , line in enumerate (lines ) if
565
578
_suspected_as_section (line )]
566
579
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' ))
571
584
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 )
576
590
577
591
for ctx in contexts :
578
592
if self ._is_a_docstring_section (ctx ):
0 commit comments