[pydocstyle] Add rule D420 to enforce docstring section ordering#23537
[pydocstyle] Add rule D420 to enforce docstring section ordering#23537amyreese merged 4 commits intoastral-sh:mainfrom
pydocstyle] Add rule D420 to enforce docstring section ordering#23537Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| D420 | 508 | 508 | 0 | 0 | 0 |
| DOC201 | 4 | 2 | 2 | 0 | 0 |
amyreese
left a comment
There was a problem hiding this comment.
Looking at ecosystem results, there are some cases where this fires on private (prefixed with _) functions, or functions that don't have any marked sections at all. Both cases feel like they should be ignored by this rule, and we should have test cases covering them: https://github.com/langchain-ai/langchain/blob/4ffb584ddf09440184dda63d11187b0bdd2b63b2/libs/core/langchain_core/messages/utils.py#L69
- Fix let-chain syntax (stray `if` in commit suggestion) - Move SectionOrderIncorrect struct up with other violation structs - Combine tests with #[test_case] and explicit snapshot names - Add edge case tests: private functions, no-section docstrings, section-like prose
bd8a1d6 to
654a763
Compare
|
Thanks for the review @amyreese
Regarding the linked langchain file ( |
I was going to say that we should make sure not to change other rules while adding |
ntBre
left a comment
There was a problem hiding this comment.
Nice, this is looking good! I just had a few minor suggestions beyond Amy's comments.
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
|
Thanks @ntBre Should I squash all the commits once the review is done? |
|
We'll squash when we merge the PR. |
|
Yes, please don't squash or rebase after we've reviewed because it doesn't play well with GitHub's review interface! We always squash-merge as Amy said. |
[pydocstyle] Add rule to enforce docstring section ordering (D420)pydocstyle] Add rule D420 to enforce docstring section ordering
Summary
Docstring sections like
Parameters,Returns,Notes,Examplesetc. have a canonical ordering defined by the convention. Contributors frequently get this wrong, puttingNotesbeforeReturns, orExamplesbeforeParameters.This PR adds a new preview rule
D420(SectionOrderIncorrect) that flags sections appearing out of order.For NumPy, the full ordering from the numpydoc spec is enforced. For Google, only the constraints explicitly documented in the style guide are enforced (Args before Returns/Yields, Raises after those), all other sections are unordered. This was discussed in the issue with @ntBre.
PEP 257 convention ignores the rule. When no convention is set, it auto-detects the style, matching D405-D417 behavior.
No autofix, reordering sections risks mangling content between them.
Closes #9641
Test Plan
Other Params,Arguments,Keyword Args), class docstrings, and unrecognized sectionscargo test -p ruff_linter,cargo clippy, anduvx prek run -a