-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PYI048 for
flake8-pyi
plugin (#4645)
- Loading branch information
Showing
11 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
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,19 @@ | ||
def bar(): # OK | ||
... | ||
|
||
|
||
def oof(): # OK, docstrings are handled by another rule | ||
"""oof""" | ||
print("foo") | ||
|
||
|
||
def foo(): # Ok not in Stub file | ||
"""foo""" | ||
print("foo") | ||
print("foo") | ||
|
||
|
||
def buzz(): # Ok not in Stub file | ||
print("fizz") | ||
print("buzz") | ||
print("test") |
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,20 @@ | ||
def bar(): | ||
... # OK | ||
|
||
|
||
def oof(): # OK, docstrings are handled by another rule | ||
"""oof""" | ||
print("foo") | ||
|
||
|
||
|
||
def foo(): # ERROR PYI048 | ||
"""foo""" | ||
print("foo") | ||
print("foo") | ||
|
||
|
||
def buzz(): # ERROR PYI048 | ||
print("fizz") | ||
print("buzz") | ||
print("test") |
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
37 changes: 37 additions & 0 deletions
37
crates/ruff/src/rules/flake8_pyi/rules/stub_body_multiple_statements.rs
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,37 @@ | ||
use rustpython_parser::ast::Stmt; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers; | ||
use ruff_python_ast::helpers::is_docstring_stmt; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
#[violation] | ||
pub struct StubBodyMultipleStatements; | ||
|
||
impl Violation for StubBodyMultipleStatements { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Function body must contain exactly one statement") | ||
} | ||
} | ||
|
||
/// PYI010 | ||
pub(crate) fn stub_body_multiple_statements(checker: &mut Checker, stmt: &Stmt, body: &[Stmt]) { | ||
// If the function body consists of exactly one statement, abort. | ||
if body.len() == 1 { | ||
return; | ||
} | ||
|
||
// If the function body consists of exactly two statements, and the first is a | ||
// docstring, abort (this is covered by PYI021). | ||
if body.len() == 2 && is_docstring_stmt(&body[0]) { | ||
return; | ||
} | ||
|
||
checker.diagnostics.push(Diagnostic::new( | ||
StubBodyMultipleStatements, | ||
helpers::identifier_range(stmt, checker.locator), | ||
)); | ||
} |
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI048_PYI048.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,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
|
20 changes: 20 additions & 0 deletions
20
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI048_PYI048.pyi.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,20 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI048.pyi:11:5: PYI048 Function body must contain exactly one statement | ||
| | ||
11 | def foo(): # ERROR PYI048 | ||
| ^^^ PYI048 | ||
12 | """foo""" | ||
13 | print("foo") | ||
| | ||
|
||
PYI048.pyi:17:5: PYI048 Function body must contain exactly one statement | ||
| | ||
17 | def buzz(): # ERROR PYI048 | ||
| ^^^^ PYI048 | ||
18 | print("fizz") | ||
19 | print("buzz") | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.