-
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.
feat(B032): add b032 flake8_bugbear (#3085)
- Loading branch information
1 parent
d9fd78d
commit 6eb014b
Showing
9 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
crates/ruff/resources/test/fixtures/flake8_bugbear/B032.py
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,29 @@ | ||
""" | ||
Should emit: | ||
B032 - on lines 9, 10, 12, 13, 16-19 | ||
""" | ||
|
||
# Flag these | ||
dct = {"a": 1} | ||
|
||
dct["b"]: 2 | ||
dct.b: 2 | ||
|
||
dct["b"]: "test" | ||
dct.b: "test" | ||
|
||
test = "test" | ||
dct["b"]: test | ||
dct["b"]: test.lower() | ||
dct.b: test | ||
dct.b: test.lower() | ||
|
||
# Do not flag below | ||
typed_dct: dict[str, int] = {"a": 1} | ||
typed_dct["b"] = 2 | ||
typed_dct.b = 2 | ||
|
||
|
||
class TestClass: | ||
def test_self(self): | ||
self.test: int |
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
69 changes: 69 additions & 0 deletions
69
crates/ruff/src/rules/flake8_bugbear/rules/unintentional_type_annotation.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,69 @@ | ||
use rustpython_parser::ast::{Expr, ExprKind, Stmt}; | ||
|
||
use ruff_macros::{define_violation, derive_message_formats}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::Diagnostic; | ||
use crate::violation::Violation; | ||
|
||
define_violation!( | ||
/// ## What it does | ||
/// Checks for the unintentional use of type annotations. | ||
/// | ||
/// ## Why is this bad? | ||
/// The use of a colon (`:`) in lieu of an assignment (`=`) can be syntactically valid, but | ||
/// is almost certainly a mistake when used in a subscript or attribute assignment. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// a["b"]: 1 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// a["b"] = 1 | ||
/// ``` | ||
pub struct UnintentionalTypeAnnotation; | ||
); | ||
impl Violation for UnintentionalTypeAnnotation { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!( | ||
"Possible unintentional type annotation (using `:`). Did you mean to assign (using `=`)?" | ||
) | ||
} | ||
} | ||
|
||
/// B032 | ||
pub fn unintentional_type_annotation( | ||
checker: &mut Checker, | ||
target: &Expr, | ||
value: Option<&Expr>, | ||
stmt: &Stmt, | ||
) { | ||
if value.is_some() { | ||
return; | ||
} | ||
match &target.node { | ||
ExprKind::Subscript { value, .. } => { | ||
if matches!(&value.node, ExprKind::Name { .. }) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnintentionalTypeAnnotation, | ||
Range::from_located(stmt), | ||
)); | ||
} | ||
} | ||
ExprKind::Attribute { value, .. } => { | ||
if let ExprKind::Name { id, .. } = &value.node { | ||
if id != "self" { | ||
checker.diagnostics.push(Diagnostic::new( | ||
UnintentionalTypeAnnotation, | ||
Range::from_located(stmt), | ||
)); | ||
} | ||
} | ||
} | ||
_ => {} | ||
}; | ||
} |
85 changes: 85 additions & 0 deletions
85
.../src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B032_B032.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,85 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_bugbear/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 9 | ||
column: 0 | ||
end_location: | ||
row: 9 | ||
column: 11 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 10 | ||
column: 0 | ||
end_location: | ||
row: 10 | ||
column: 8 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 12 | ||
column: 0 | ||
end_location: | ||
row: 12 | ||
column: 16 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 13 | ||
column: 0 | ||
end_location: | ||
row: 13 | ||
column: 13 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 16 | ||
column: 0 | ||
end_location: | ||
row: 16 | ||
column: 14 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 17 | ||
column: 0 | ||
end_location: | ||
row: 17 | ||
column: 22 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 18 | ||
column: 0 | ||
end_location: | ||
row: 18 | ||
column: 11 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
UnintentionalTypeAnnotation: ~ | ||
location: | ||
row: 19 | ||
column: 0 | ||
end_location: | ||
row: 19 | ||
column: 19 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.