forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
343 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,75 @@ | ||
# Violations of PYI012 | ||
|
||
|
||
class OneAttributeClass: | ||
value: int | ||
pass # PYI012 Class body must not contain `pass` | ||
|
||
|
||
class OneAttributeClassRev: | ||
pass # PYI012 Class body must not contain `pass` | ||
value: int | ||
|
||
|
||
class DocstringClass: | ||
""" | ||
My body only contains pass. | ||
""" | ||
|
||
pass # PYI012 Class body must not contain `pass` | ||
|
||
|
||
class NonEmptyChild(Exception): | ||
value: int | ||
pass # PYI012 Class body must not contain `pass` | ||
|
||
|
||
class NonEmptyChild2(Exception): | ||
pass # PYI012 Class body must not contain `pass` | ||
value: int | ||
|
||
|
||
class NonEmptyWithInit: | ||
value: int | ||
pass # PYI012 Class body must not contain `pass` | ||
|
||
def __init__(): | ||
pass | ||
|
||
|
||
# Not violations (of PYI012) | ||
|
||
|
||
class EmptyClass: | ||
pass # Y009 Empty body should contain `...`, not `pass` | ||
|
||
|
||
class EmptyOneLine: | ||
pass # Y009 Empty body should contain `...`, not `pass` | ||
|
||
|
||
class Dog: | ||
eyes: int = 2 | ||
|
||
|
||
class EmptyEllipsis: | ||
... | ||
|
||
|
||
class NonEmptyEllipsis: | ||
value: int | ||
... # Y013 Non-empty class body must not contain `...` | ||
|
||
|
||
class WithInit: | ||
value: int = 0 | ||
|
||
def __init__(): | ||
pass | ||
|
||
|
||
def function(): | ||
pass | ||
|
||
|
||
pass |
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,59 @@ | ||
# Violations of PYI012 | ||
|
||
class OneAttributeClass: | ||
value: int | ||
pass # PYI012 Class body must not contain `pass` | ||
|
||
class OneAttributeClassRev: | ||
pass # PYI012 Class body must not contain `pass` | ||
value: int | ||
|
||
class DocstringClass: | ||
""" | ||
My body only contains pass. | ||
""" | ||
|
||
pass # PYI012 Class body must not contain `pass` | ||
|
||
class NonEmptyChild(Exception): | ||
value: int | ||
pass # PYI012 Class body must not contain `pass` | ||
|
||
class NonEmptyChild2(Exception): | ||
pass # PYI012 Class body must not contain `pass` | ||
value: int | ||
|
||
class NonEmptyWithInit: | ||
value: int | ||
pass # PYI012 Class body must not contain `pass` | ||
|
||
def __init__(): | ||
pass | ||
|
||
# Not violations (of PYI012) | ||
|
||
class EmptyClass: | ||
pass # Y009 Empty body should contain `...`, not `pass` | ||
|
||
class EmptyOneLine: | ||
pass # Y009 Empty body should contain `...`, not `pass` | ||
|
||
class Dog: | ||
eyes: int = 2 | ||
|
||
class EmptyEllipsis: ... | ||
|
||
class NonEmptyEllipsis: | ||
value: int | ||
... # Y013 Non-empty class body must not contain `...` | ||
|
||
class WithInit: | ||
value: int = 0 | ||
|
||
def __init__(): | ||
pass | ||
|
||
def function(): | ||
pass | ||
|
||
pass |
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
62 changes: 62 additions & 0 deletions
62
crates/ruff/src/rules/flake8_pyi/rules/pass_in_class_body.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,62 @@ | ||
use crate::autofix::helpers::delete_stmt; | ||
use log::error; | ||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::{Range, RefEquality}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
use crate::registry::AsRule; | ||
use rustpython_parser::ast::{Stmt, StmtKind}; | ||
|
||
#[violation] | ||
pub struct PassInClassBody; | ||
|
||
impl AlwaysAutofixableViolation for PassInClassBody { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Class body must not contain `pass`") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
format!("Remove unnecessary `pass`") | ||
} | ||
} | ||
|
||
/// PYI012 | ||
pub fn pass_in_class_body<'a>(checker: &mut Checker<'a>, parent: &'a Stmt, body: &'a [Stmt]) { | ||
// `pass` is required in these situations (or handled by `pass_statement_stub_body`). | ||
if body.len() < 2 { | ||
return; | ||
} | ||
|
||
for stmt in body { | ||
if matches!(stmt.node, StmtKind::Pass) { | ||
let mut diagnostic = Diagnostic::new(PassInClassBody, Range::from(stmt)); | ||
|
||
if checker.patch(diagnostic.kind.rule()) { | ||
let deleted: Vec<&Stmt> = checker.deletions.iter().map(Into::into).collect(); | ||
match delete_stmt( | ||
stmt, | ||
Some(parent), | ||
&deleted, | ||
checker.locator, | ||
checker.indexer, | ||
checker.stylist, | ||
) { | ||
Ok(fix) => { | ||
if fix.content.is_empty() || fix.content == "pass" { | ||
checker.deletions.insert(RefEquality(stmt)); | ||
} | ||
diagnostic.set_fix(fix); | ||
} | ||
Err(e) => { | ||
error!("Failed to delete `pass` statement: {}", e); | ||
} | ||
}; | ||
}; | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI012_PYI012.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,6 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
expression: diagnostics | ||
--- | ||
[] | ||
|
131 changes: 131 additions & 0 deletions
131
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI012_PYI012.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,131 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: PassInClassBody | ||
body: "Class body must not contain `pass`" | ||
suggestion: "Remove unnecessary `pass`" | ||
fixable: true | ||
location: | ||
row: 5 | ||
column: 4 | ||
end_location: | ||
row: 5 | ||
column: 8 | ||
fix: | ||
edits: | ||
- content: "" | ||
location: | ||
row: 5 | ||
column: 0 | ||
end_location: | ||
row: 6 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: PassInClassBody | ||
body: "Class body must not contain `pass`" | ||
suggestion: "Remove unnecessary `pass`" | ||
fixable: true | ||
location: | ||
row: 8 | ||
column: 4 | ||
end_location: | ||
row: 8 | ||
column: 8 | ||
fix: | ||
edits: | ||
- content: "" | ||
location: | ||
row: 8 | ||
column: 0 | ||
end_location: | ||
row: 9 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: PassInClassBody | ||
body: "Class body must not contain `pass`" | ||
suggestion: "Remove unnecessary `pass`" | ||
fixable: true | ||
location: | ||
row: 16 | ||
column: 4 | ||
end_location: | ||
row: 16 | ||
column: 8 | ||
fix: | ||
edits: | ||
- content: "" | ||
location: | ||
row: 16 | ||
column: 0 | ||
end_location: | ||
row: 17 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: PassInClassBody | ||
body: "Class body must not contain `pass`" | ||
suggestion: "Remove unnecessary `pass`" | ||
fixable: true | ||
location: | ||
row: 20 | ||
column: 4 | ||
end_location: | ||
row: 20 | ||
column: 8 | ||
fix: | ||
edits: | ||
- content: "" | ||
location: | ||
row: 20 | ||
column: 0 | ||
end_location: | ||
row: 21 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: PassInClassBody | ||
body: "Class body must not contain `pass`" | ||
suggestion: "Remove unnecessary `pass`" | ||
fixable: true | ||
location: | ||
row: 23 | ||
column: 4 | ||
end_location: | ||
row: 23 | ||
column: 8 | ||
fix: | ||
edits: | ||
- content: "" | ||
location: | ||
row: 23 | ||
column: 0 | ||
end_location: | ||
row: 24 | ||
column: 0 | ||
parent: ~ | ||
- kind: | ||
name: PassInClassBody | ||
body: "Class body must not contain `pass`" | ||
suggestion: "Remove unnecessary `pass`" | ||
fixable: true | ||
location: | ||
row: 28 | ||
column: 4 | ||
end_location: | ||
row: 28 | ||
column: 8 | ||
fix: | ||
edits: | ||
- content: "" | ||
location: | ||
row: 28 | ||
column: 0 | ||
end_location: | ||
row: 29 | ||
column: 0 | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.