mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 18:11:08 +00:00
D403: Require capitalizing single word sentence (#10776)
This commit is contained in:
parent
d050d6da2e
commit
2e7a1a4cb1
3 changed files with 59 additions and 10 deletions
|
@ -25,3 +25,9 @@ def non_ascii():
|
||||||
|
|
||||||
def all_caps():
|
def all_caps():
|
||||||
"""th•s is not capitalized."""
|
"""th•s is not capitalized."""
|
||||||
|
|
||||||
|
def single_word():
|
||||||
|
"""singleword."""
|
||||||
|
|
||||||
|
def single_word_no_dot():
|
||||||
|
"""singleword"""
|
||||||
|
|
|
@ -59,26 +59,36 @@ pub(crate) fn capitalized(checker: &mut Checker, docstring: &Docstring) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = docstring.body();
|
let body = docstring.body();
|
||||||
let Some(first_word) = body.split(' ').next() else {
|
let first_word = body.split_once(' ').map_or_else(
|
||||||
return;
|
|| {
|
||||||
};
|
// If the docstring is a single word, trim the punctuation marks because
|
||||||
|
// it makes the ASCII test below fail.
|
||||||
// Like pydocstyle, we only support ASCII for now.
|
body.trim_end_matches(['.', '!', '?'])
|
||||||
for char in first_word.chars() {
|
},
|
||||||
if !char.is_ascii_alphabetic() && char != '\'' {
|
|(first_word, _)| first_word,
|
||||||
return;
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut first_word_chars = first_word.chars();
|
let mut first_word_chars = first_word.chars();
|
||||||
let Some(first_char) = first_word_chars.next() else {
|
let Some(first_char) = first_word_chars.next() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if !first_char.is_ascii() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let uppercase_first_char = first_char.to_ascii_uppercase();
|
let uppercase_first_char = first_char.to_ascii_uppercase();
|
||||||
if first_char == uppercase_first_char {
|
if first_char == uppercase_first_char {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Like pydocstyle, we only support ASCII for now.
|
||||||
|
for char in first_word.chars().skip(1) {
|
||||||
|
if !char.is_ascii_alphabetic() && char != '\'' {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let capitalized_word = uppercase_first_char.to_string() + first_word_chars.as_str();
|
let capitalized_word = uppercase_first_char.to_string() + first_word_chars.as_str();
|
||||||
|
|
||||||
let mut diagnostic = Diagnostic::new(
|
let mut diagnostic = Diagnostic::new(
|
||||||
|
|
|
@ -19,4 +19,37 @@ D403.py:2:5: D403 [*] First word of the first line should be capitalized: `this`
|
||||||
4 4 | def good_function():
|
4 4 | def good_function():
|
||||||
5 5 | """This docstring is capitalized."""
|
5 5 | """This docstring is capitalized."""
|
||||||
|
|
||||||
|
D403.py:30:5: D403 [*] First word of the first line should be capitalized: `singleword` -> `Singleword`
|
||||||
|
|
|
||||||
|
29 | def single_word():
|
||||||
|
30 | """singleword."""
|
||||||
|
| ^^^^^^^^^^^^^^^^^ D403
|
||||||
|
31 |
|
||||||
|
32 | def single_word_no_dot():
|
||||||
|
|
|
||||||
|
= help: Capitalize `singleword` to `Singleword`
|
||||||
|
|
||||||
|
ℹ Safe fix
|
||||||
|
27 27 | """th•s is not capitalized."""
|
||||||
|
28 28 |
|
||||||
|
29 29 | def single_word():
|
||||||
|
30 |- """singleword."""
|
||||||
|
30 |+ """Singleword."""
|
||||||
|
31 31 |
|
||||||
|
32 32 | def single_word_no_dot():
|
||||||
|
33 33 | """singleword"""
|
||||||
|
|
||||||
|
D403.py:33:5: D403 [*] First word of the first line should be capitalized: `singleword` -> `Singleword`
|
||||||
|
|
|
||||||
|
32 | def single_word_no_dot():
|
||||||
|
33 | """singleword"""
|
||||||
|
| ^^^^^^^^^^^^^^^^ D403
|
||||||
|
|
|
||||||
|
= help: Capitalize `singleword` to `Singleword`
|
||||||
|
|
||||||
|
ℹ Safe fix
|
||||||
|
30 30 | """singleword."""
|
||||||
|
31 31 |
|
||||||
|
32 32 | def single_word_no_dot():
|
||||||
|
33 |- """singleword"""
|
||||||
|
33 |+ """Singleword"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue