[pydocstyle] Skip section detection inside RST directive bodies (D214, D405, D413)#23635
Open
bxff wants to merge 1 commit intoastral-sh:mainfrom
Open
[pydocstyle] Skip section detection inside RST directive bodies (D214, D405, D413)#23635bxff wants to merge 1 commit intoastral-sh:mainfrom
pydocstyle] Skip section detection inside RST directive bodies (D214, D405, D413)#23635bxff wants to merge 1 commit intoastral-sh:mainfrom
Conversation
…214`, `D405`, `D413`) Closes astral-sh#23562 Content inside reStructuredText directives (e.g., `.. code-block:: yaml`) was incorrectly identified as docstring section headers. For example, `references:` inside a code-block would trigger D405 (capitalization), D214 (over-indentation), and D413 (missing blank line). This adds RST directive body tracking to `from_docstring` in the section parser. When a line starting with `.. ` is detected, all subsequent lines indented deeper than the directive are skipped from section detection. Real sections after directives continue to be detected correctly.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #23562.
Content inside reStructuredText directives (e.g.,
.. code-block:: yaml) was incorrectly identified as docstring section headers. For example,references:inside a code-block would trigger D405 (capitalization), D214 (over-indentation), and D413 (missing blank line).The root cause is that the section parser in
from_docstring(docstrings/sections.rs) iterates docstring lines and callsis_docstring_sectionwith no awareness of RST directive blocks. The wordreferencesmatchesSectionKind::References, the suffix:passes the section name check, and the preceding blank line (required by RST after the directive declaration) satisfies the end-of-paragraph heuristic.There is already existing RST awareness in
blanks_and_section_underline(theis_sphinxcheck at lines 1593 and 1691), but that serves a different purpose — it preserves blank lines when a real section header likeExample:has a.. code-block::directive as its body content. This fix is complementary: it prevents content inside a directive body from being misidentified as section headers in the first place.This adds RST directive body tracking to
from_docstring. When a line starting with..is detected (an RST directive), all subsequent lines indented deeper than the directive are skipped from section detection. Real sections after directives continue to be detected correctly.Test Plan
Added
sphinx_directive.pytest fixture with cases for:.. code-block:: yamlcontainingreferences:(the exact case from ruff incorrectly reformatting Sphinx docstrings #23562)Returns:).. code-block: yaml).. note::containing.. code-block::)Notes:) following a directive — verifies sections after directives are still detectedRegistered test cases for
D214,D405, andD413against the new fixture. All 72 pydocstyle tests pass:Also manually verified the original reproduction case no longer triggers D405/D214/D413 false positives.