@@ -504,6 +504,9 @@ def check_imperative_mood(self, function, docstring): # def context
504
504
"Returns the pathname ...".
505
505
506
506
"""
507
+ ctxs = list (self ._get_section_contexts_autodetect (docstring ))
508
+ if ctxs and ctxs [0 ].is_docstring_start :
509
+ return
507
510
if (
508
511
docstring
509
512
and not function .is_test
@@ -587,6 +590,16 @@ def check_starts_with_this(self, function, docstring):
587
590
if first_word .lower () == 'this' :
588
591
return violations .D404 ()
589
592
593
+ @staticmethod
594
+ def _is_at_docstring_start (context ):
595
+ """Return whether a `SectionContext` occurs at the start of a docstring."""
596
+ return context .original_index == 1 and context .previous_line in [
597
+ '"' ,
598
+ "'" ,
599
+ '"""' ,
600
+ "'''" ,
601
+ ]
602
+
590
603
@staticmethod
591
604
def _is_docstring_section (context ):
592
605
"""Check if the suspected context is really a section header.
@@ -639,7 +652,9 @@ def _is_docstring_section(context):
639
652
)
640
653
641
654
prev_line_looks_like_end_of_paragraph = (
642
- prev_line_ends_with_punctuation or is_blank (context .previous_line )
655
+ prev_line_ends_with_punctuation
656
+ or is_blank (context .previous_line )
657
+ or context .is_docstring_start
643
658
)
644
659
645
660
return (
@@ -749,7 +764,10 @@ def _check_common_section(
749
764
else :
750
765
yield violations .D410 (capitalized_section )
751
766
752
- if not is_blank (context .previous_line ):
767
+ if (
768
+ not is_blank (context .previous_line )
769
+ and not context .is_docstring_start
770
+ ):
753
771
yield violations .D411 (capitalized_section )
754
772
755
773
yield from cls ._check_blanks_and_section_underline (
@@ -945,6 +963,7 @@ def _suspected_as_section(_line):
945
963
'line' ,
946
964
'following_lines' ,
947
965
'original_index' ,
966
+ 'is_docstring_start' ,
948
967
'is_last_section' ,
949
968
),
950
969
)
@@ -960,15 +979,18 @@ def _suspected_as_section(_line):
960
979
lines [i + 1 :],
961
980
i ,
962
981
False ,
982
+ False ,
963
983
)
964
984
for i in suspected_section_indices
965
985
)
966
-
967
- # Now that we have manageable objects - rule out false positives.
968
986
contexts = (
969
- c for c in contexts if ConventionChecker ._is_docstring_section (c )
987
+ c ._replace (is_docstring_start = cls ._is_at_docstring_start (c ))
988
+ for c in contexts
970
989
)
971
990
991
+ # Now that we have manageable objects - rule out false positives.
992
+ contexts = (c for c in contexts if cls ._is_docstring_section (c ))
993
+
972
994
# Now we shall trim the `following lines` field to only reach the
973
995
# next section name.
974
996
for a , b in pairwise (contexts , None ):
@@ -980,6 +1002,7 @@ def _suspected_as_section(_line):
980
1002
a .line ,
981
1003
lines [a .original_index + 1 : end ],
982
1004
a .original_index ,
1005
+ a .is_docstring_start ,
983
1006
b is None ,
984
1007
)
985
1008
0 commit comments