-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port pydocstyle code 401 (ImperativeMood) (#1999)
This adds support for pydocstyle code D401 using the `imperative` crate.
- Loading branch information
Showing
13 changed files
with
208 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Bad examples | ||
|
||
def bad_liouiwnlkjl(): | ||
"""Returns foo.""" | ||
|
||
|
||
def bad_sdgfsdg23245(): | ||
"""Constructor for a foo.""" | ||
|
||
|
||
def bad_sdgfsdg23245777(): | ||
""" | ||
Constructor for a boa. | ||
""" | ||
|
||
|
||
def bad_run_something(): | ||
"""Runs something""" | ||
pass | ||
|
||
|
||
def multi_line(): | ||
"""Writes a logical line that | ||
extends to two physical lines. | ||
""" | ||
|
||
|
||
# Good examples | ||
|
||
def good_run_something(): | ||
"""Run away.""" | ||
|
||
|
||
def good_construct(): | ||
"""Construct a beautiful house.""" | ||
|
||
|
||
def good_multi_line(): | ||
"""Write a logical line that | ||
extends to two physical lines. | ||
""" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1277,6 +1277,7 @@ | |
"D4", | ||
"D40", | ||
"D400", | ||
"D401", | ||
"D402", | ||
"D403", | ||
"D404", | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use imperative::Mood; | ||
use once_cell::sync::Lazy; | ||
use ruff_macros::derive_message_formats; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::define_violation; | ||
use crate::docstrings::definition::Docstring; | ||
use crate::registry::Diagnostic; | ||
use crate::rules::pydocstyle::helpers::normalize_word; | ||
use crate::violation::Violation; | ||
|
||
static MOOD: Lazy<Mood> = Lazy::new(Mood::new); | ||
|
||
/// D401 | ||
pub fn non_imperative_mood(checker: &mut Checker, docstring: &Docstring) { | ||
let body = docstring.body; | ||
|
||
// Find first line, disregarding whitespace. | ||
let line = match body.trim().lines().next() { | ||
Some(line) => line.trim(), | ||
None => return, | ||
}; | ||
// Find the first word on that line and normalize it to lower-case. | ||
let first_word_norm = match line.split_whitespace().next() { | ||
Some(word) => normalize_word(word), | ||
None => return, | ||
}; | ||
if first_word_norm.is_empty() { | ||
return; | ||
} | ||
if let Some(false) = MOOD.is_imperative(&first_word_norm) { | ||
let diagnostic = Diagnostic::new( | ||
NonImperativeMood(line.to_string()), | ||
Range::from_located(docstring.expr), | ||
); | ||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
|
||
define_violation!( | ||
pub struct NonImperativeMood(pub String); | ||
); | ||
impl Violation for NonImperativeMood { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let NonImperativeMood(first_line) = self; | ||
format!("First line of docstring should be in imperative mood: \"{first_line}\"") | ||
} | ||
} |
This file contains 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
55 changes: 55 additions & 0 deletions
55
src/rules/pydocstyle/snapshots/ruff__rules__pydocstyle__tests__D401_D401.py.snap
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
--- | ||
source: src/rules/pydocstyle/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
NonImperativeMood: Returns foo. | ||
location: | ||
row: 4 | ||
column: 4 | ||
end_location: | ||
row: 4 | ||
column: 22 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
NonImperativeMood: Constructor for a foo. | ||
location: | ||
row: 8 | ||
column: 4 | ||
end_location: | ||
row: 8 | ||
column: 32 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
NonImperativeMood: Constructor for a boa. | ||
location: | ||
row: 12 | ||
column: 4 | ||
end_location: | ||
row: 16 | ||
column: 7 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
NonImperativeMood: Runs something | ||
location: | ||
row: 20 | ||
column: 4 | ||
end_location: | ||
row: 20 | ||
column: 24 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
NonImperativeMood: Writes a logical line that | ||
location: | ||
row: 25 | ||
column: 4 | ||
end_location: | ||
row: 27 | ||
column: 7 | ||
fix: ~ | ||
parent: ~ | ||
|