Skip to content

Commit

Permalink
Fix for D403 to split on first whitespace character when extracting f…
Browse files Browse the repository at this point in the history
…irst word of docstring.
  • Loading branch information
my1e5 committed Dec 20, 2024
1 parent c1eaf6f commit 61effe6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pydocstyle/D403.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ def first_word_lots_of_whitespace():
What do you think?
"""

def single_word_then_newline():
"""singleword
"""

def single_word_on_second_line():
"""
singleword
"""
12 changes: 4 additions & 8 deletions crates/ruff_linter/src/rules/pydocstyle/rules/capitalized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ pub(crate) fn capitalized(checker: &mut Checker, docstring: &Docstring) {

let body = docstring.body();
let trim_start_body = body.trim_start();
let first_word = trim_start_body.split_once(' ').map_or_else(
|| {
// If the docstring is a single word, trim the punctuation marks because
// it makes the ASCII test below fail.
body.trim_end_matches(['.', '!', '?'])
},
|(first_word, _)| first_word,
);
let first_word = trim_start_body
.find(char::is_whitespace)
.map_or(trim_start_body, |idx| &trim_start_body[..idx])
.trim_end_matches(['.', '!', '?']);

let mut first_word_chars = first_word.chars();
let Some(first_char) = first_word_chars.next() else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/ruff_linter/src/rules/pydocstyle/mod.rs
snapshot_kind: text
---
D403.py:2:5: D403 [*] First word of the docstring should be capitalized: `this` -> `This`
|
Expand Down Expand Up @@ -72,6 +73,8 @@ D403.py:36:5: D403 [*] First word of the docstring should be capitalized: `here`
42 | | What do you think?
43 | | """
| |_______^ D403
44 |
45 | def single_word_then_newline():
|
= help: Capitalize `here` to `Here`

Expand All @@ -84,3 +87,45 @@ D403.py:36:5: D403 [*] First word of the docstring should be capitalized: `here`
41 41 |
42 42 | What do you think?
43 43 | """
D403.py:46:5: D403 [*] First word of the docstring should be capitalized: `singleword` -> `Singleword`
|
45 | def single_word_then_newline():
46 | """singleword
| _____^
47 | |
48 | | """
| |_______^ D403
49 |
50 | def single_word_on_second_line():
|
= help: Capitalize `singleword` to `Singleword`

Safe fix
43 43 | """
44 44 |
45 45 | def single_word_then_newline():
46 |- """singleword
46 |+ """Singleword
47 47 |
48 48 | """
49 49 |

D403.py:51:5: D403 [*] First word of the docstring should be capitalized: `singleword` -> `Singleword`
|
50 | def single_word_on_second_line():
51 | """
| _____^
52 | | singleword
53 | | """
| |_______^ D403
|
= help: Capitalize `singleword` to `Singleword`

Safe fix
49 49 |
50 50 | def single_word_on_second_line():
51 51 | """
52 |- singleword
52 |+ Singleword
53 53 | """

0 comments on commit 61effe6

Please sign in to comment.