Skip to content

Commit

Permalink
[flake8-bugbear] don't fix B009/B010 if identifier would be mangled
Browse files Browse the repository at this point in the history
fixes #2202
  • Loading branch information
sciyoshi committed Jan 26, 2023
1 parent adb5c5b commit 79f1860
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 37 deletions.
8 changes: 6 additions & 2 deletions resources/test/fixtures/flake8_bugbear/B009_B010.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Should emit:
B009 - Line 18, 19, 20, 21, 22
B010 - Line 33, 34, 35, 36
B009 - Line 18, 19, 20, 21, 22, 23, 24
B010 - Line 39, 40, 41, 42, 43, 44, 45
"""

# Valid getattr usage
Expand All @@ -17,6 +17,8 @@
# Invalid usage
getattr(foo, "bar")
getattr(foo, "_123abc")
getattr(foo, "__123abc")
getattr(foo, "__123abc__")
getattr(foo, "abc123")
getattr(foo, r"abc123")
_ = lambda x: getattr(x, "bar")
Expand All @@ -36,6 +38,8 @@
# Invalid usage
setattr(foo, "bar", None)
setattr(foo, "_123abc", None)
setattr(foo, "__123abc", None)
setattr(foo, "__123abc__", None)
setattr(foo, "abc123", None)
setattr(foo, r"abc123", None)
setattr(foo.bar, r"baz", None)
8 changes: 7 additions & 1 deletion src/rules/flake8_bugbear/rules/getattr_with_constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ fn attribute(value: &Expr, attr: &str) -> Expr {
)
}

fn is_mangled_private(id: &str) -> bool {
id.starts_with("__") && !id.ends_with("__")
}

/// B009
pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, args: &[Expr]) {
let ExprKind::Name { id, .. } = &func.node else {
Expand All @@ -47,7 +51,9 @@ pub fn getattr_with_constant(checker: &mut Checker, expr: &Expr, func: &Expr, ar

let mut diagnostic =
Diagnostic::new(violations::GetAttrWithConstant, Range::from_located(expr));
if checker.patch(diagnostic.kind.rule()) {

// Don't autofix if name would be mangled by Python's "private variables" handling
if checker.patch(diagnostic.kind.rule()) && !is_mangled_private(value.as_str()) {
diagnostic.amend(Fix::replacement(
unparse_expr(&attribute(obj, value), checker.stylist),
expr.location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,69 +45,97 @@ expression: diagnostics
column: 0
end_location:
row: 20
column: 24
fix: ~
parent: ~
- kind:
GetAttrWithConstant: ~
location:
row: 21
column: 0
end_location:
row: 21
column: 26
fix:
content:
- foo.__123abc__
location:
row: 21
column: 0
end_location:
row: 21
column: 26
parent: ~
- kind:
GetAttrWithConstant: ~
location:
row: 22
column: 0
end_location:
row: 22
column: 22
fix:
content:
- foo.abc123
location:
row: 20
row: 22
column: 0
end_location:
row: 20
row: 22
column: 22
parent: ~
- kind:
GetAttrWithConstant: ~
location:
row: 21
row: 23
column: 0
end_location:
row: 21
row: 23
column: 23
fix:
content:
- foo.abc123
location:
row: 21
row: 23
column: 0
end_location:
row: 21
row: 23
column: 23
parent: ~
- kind:
GetAttrWithConstant: ~
location:
row: 22
row: 24
column: 14
end_location:
row: 22
row: 24
column: 31
fix:
content:
- x.bar
location:
row: 22
row: 24
column: 14
end_location:
row: 22
row: 24
column: 31
parent: ~
- kind:
GetAttrWithConstant: ~
location:
row: 23
row: 25
column: 3
end_location:
row: 23
row: 25
column: 20
fix:
content:
- x.bar
location:
row: 23
row: 25
column: 3
end_location:
row: 23
row: 25
column: 20
parent: ~

Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,127 @@ expression: diagnostics
- kind:
SetAttrWithConstant: ~
location:
row: 37
row: 39
column: 0
end_location:
row: 37
row: 39
column: 25
fix:
content:
- foo.bar = None
location:
row: 37
row: 39
column: 0
end_location:
row: 37
row: 39
column: 25
parent: ~
- kind:
SetAttrWithConstant: ~
location:
row: 38
row: 40
column: 0
end_location:
row: 38
row: 40
column: 29
fix:
content:
- foo._123abc = None
location:
row: 38
row: 40
column: 0
end_location:
row: 38
row: 40
column: 29
parent: ~
- kind:
SetAttrWithConstant: ~
location:
row: 39
row: 41
column: 0
end_location:
row: 39
row: 41
column: 30
fix:
content:
- foo.__123abc = None
location:
row: 41
column: 0
end_location:
row: 41
column: 30
parent: ~
- kind:
SetAttrWithConstant: ~
location:
row: 42
column: 0
end_location:
row: 42
column: 32
fix:
content:
- foo.__123abc__ = None
location:
row: 42
column: 0
end_location:
row: 42
column: 32
parent: ~
- kind:
SetAttrWithConstant: ~
location:
row: 43
column: 0
end_location:
row: 43
column: 28
fix:
content:
- foo.abc123 = None
location:
row: 39
row: 43
column: 0
end_location:
row: 39
row: 43
column: 28
parent: ~
- kind:
SetAttrWithConstant: ~
location:
row: 40
row: 44
column: 0
end_location:
row: 40
row: 44
column: 29
fix:
content:
- foo.abc123 = None
location:
row: 40
row: 44
column: 0
end_location:
row: 40
row: 44
column: 29
parent: ~
- kind:
SetAttrWithConstant: ~
location:
row: 41
row: 45
column: 0
end_location:
row: 41
row: 45
column: 30
fix:
content:
- foo.bar.baz = None
location:
row: 41
row: 45
column: 0
end_location:
row: 41
row: 45
column: 30
parent: ~

0 comments on commit 79f1860

Please sign in to comment.