@@ -504,6 +504,9 @@ def check_imperative_mood(self, function, docstring): # def context
504504 "Returns the pathname ...".
505505
506506 """
507+ ctxs = list (self ._get_section_contexts_autodetect (docstring ))
508+ if ctxs and ctxs [0 ].is_docstring_start :
509+ return
507510 if (
508511 docstring
509512 and not function .is_test
@@ -587,6 +590,16 @@ def check_starts_with_this(self, function, docstring):
587590 if first_word .lower () == 'this' :
588591 return violations .D404 ()
589592
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+
590603 @staticmethod
591604 def _is_docstring_section (context ):
592605 """Check if the suspected context is really a section header.
@@ -639,7 +652,9 @@ def _is_docstring_section(context):
639652 )
640653
641654 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
643658 )
644659
645660 return (
@@ -749,7 +764,10 @@ def _check_common_section(
749764 else :
750765 yield violations .D410 (capitalized_section )
751766
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+ ):
753771 yield violations .D411 (capitalized_section )
754772
755773 yield from cls ._check_blanks_and_section_underline (
@@ -945,6 +963,7 @@ def _suspected_as_section(_line):
945963 'line' ,
946964 'following_lines' ,
947965 'original_index' ,
966+ 'is_docstring_start' ,
948967 'is_last_section' ,
949968 ),
950969 )
@@ -960,15 +979,18 @@ def _suspected_as_section(_line):
960979 lines [i + 1 :],
961980 i ,
962981 False ,
982+ False ,
963983 )
964984 for i in suspected_section_indices
965985 )
966-
967- # Now that we have manageable objects - rule out false positives.
968986 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
970989 )
971990
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+
972994 # Now we shall trim the `following lines` field to only reach the
973995 # next section name.
974996 for a , b in pairwise (contexts , None ):
@@ -980,6 +1002,7 @@ def _suspected_as_section(_line):
9801002 a .line ,
9811003 lines [a .original_index + 1 : end ],
9821004 a .original_index ,
1005+ a .is_docstring_start ,
9831006 b is None ,
9841007 )
9851008
0 commit comments