fix: reorder class docstring sections per numpydoc 1.8+ standard#14434
Open
jlaportebot wants to merge 1 commit into
Open
fix: reorder class docstring sections per numpydoc 1.8+ standard#14434jlaportebot wants to merge 1 commit into
jlaportebot wants to merge 1 commit into
Conversation
For class and exception docstrings, the Attributes and Methods sections should appear directly after Parameters (and Other Parameters, Keyword Arguments), before Returns/Yields/Notes/etc., per the numpydoc 1.8.0 standard. Previously, napoleon would emit sections in document order, which meant Attributes and Methods would appear after Notes, Examples, Returns, etc. This change: - Adds a section buffer in _parse() for class/exception docstrings - Implements _reorder_class_sections() to rearrange section chunks - Handles edge cases: no params, already correct order, Google-style - Works with napoleon_use_ivar and napoleon_use_param=False Closes sphinx-doc#13180
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.
Problem
Per numpydoc 1.8.0 release notes, the section ordering for class docstrings was updated so that Attributes and Methods sections appear directly after Parameters (and Other Parameters, Keyword Arguments), before Returns/Yields/Notes/etc.
However,
sphinx.ext.napoleonstill emits sections in document order, so Attributes and Methods end up after Notes, Examples, Returns, etc.Closes #13180
Solution
This PR implements class/exception docstring section reordering in the napoleon extension:
Core changes (
sphinx/ext/napoleon/docstring.py)Section buffering in
_parse(): For class/exception docstrings (self._what in {"class", "exception"}), section content is collected intosection_chunksinstead of being immediately appended to_parsed_lines._reorder_class_sections()method: Reorders the collected section chunks into the correct order:Early exit optimization: If sections are already in the correct order, or if there are no Attributes/Methods sections, the original order is preserved without unnecessary processing.
Edge case handling:
napoleon_use_ivar=Trueandnapoleon_use_param=FalseconfigurationsTest changes (
tests/test_ext_napoleon/test_ext_napoleon_docstring.py)Added
TestClassSectionOrderingwith 11 test cases covering:napoleon_use_ivar=Truenapoleon_use_param=FalseTesting
All 79 napoleon tests pass (excluding one pre-existing failure in
test_napoleon_keyword_and_paramtypewhich is unrelated).