Add function name to B008 message

This commit is contained in:
Charlie Marsh 2022-11-12 16:53:13 -05:00
parent 695b06ba60
commit 9ec7e6bcd6
5 changed files with 43 additions and 23 deletions

View file

@ -515,7 +515,7 @@ For more, see [flake8-bugbear](https://pypi.org/project/flake8-bugbear/22.10.27/
| B005 | StripWithMultiCharacters | Using `.strip()` with multi-character strings is misleading the reader. | | | B005 | StripWithMultiCharacters | Using `.strip()` with multi-character strings is misleading the reader. | |
| B006 | MutableArgumentDefault | Do not use mutable data structures for argument defaults. | | | B006 | MutableArgumentDefault | Do not use mutable data structures for argument defaults. | |
| B007 | UnusedLoopControlVariable | Loop control variable `i` not used within the loop body. | 🛠 | | B007 | UnusedLoopControlVariable | Loop control variable `i` not used within the loop body. | 🛠 |
| B008 | FunctionCallArgumentDefault | Do not perform function calls in argument defaults. | | | B008 | FunctionCallArgumentDefault | Do not perform function call in argument defaults | |
| B009 | GetAttrWithConstant | Do not call `getattr` with a constant attribute value, it is not any safer than normal property access. | 🛠 | | B009 | GetAttrWithConstant | Do not call `getattr` with a constant attribute value, it is not any safer than normal property access. | 🛠 |
| B010 | SetAttrWithConstant | Do not call `setattr` with a constant attribute value, it is not any safer than normal property access. | | | B010 | SetAttrWithConstant | Do not call `setattr` with a constant attribute value, it is not any safer than normal property access. | |
| B011 | DoNotAssertFalse | Do not `assert False` (`python -O` removes these calls), raise `AssertionError()` | 🛠 | | B011 | DoNotAssertFalse | Do not `assert False` (`python -O` removes these calls), raise `AssertionError()` | 🛠 |

View file

@ -382,7 +382,7 @@ pub enum CheckKind {
StripWithMultiCharacters, StripWithMultiCharacters,
MutableArgumentDefault, MutableArgumentDefault,
UnusedLoopControlVariable(String), UnusedLoopControlVariable(String),
FunctionCallArgumentDefault, FunctionCallArgumentDefault(Option<String>),
GetAttrWithConstant, GetAttrWithConstant,
SetAttrWithConstant, SetAttrWithConstant,
DoNotAssertFalse, DoNotAssertFalse,
@ -619,7 +619,7 @@ impl CheckCode {
CheckCode::B005 => CheckKind::StripWithMultiCharacters, CheckCode::B005 => CheckKind::StripWithMultiCharacters,
CheckCode::B006 => CheckKind::MutableArgumentDefault, CheckCode::B006 => CheckKind::MutableArgumentDefault,
CheckCode::B007 => CheckKind::UnusedLoopControlVariable("i".to_string()), CheckCode::B007 => CheckKind::UnusedLoopControlVariable("i".to_string()),
CheckCode::B008 => CheckKind::FunctionCallArgumentDefault, CheckCode::B008 => CheckKind::FunctionCallArgumentDefault(None),
CheckCode::B009 => CheckKind::GetAttrWithConstant, CheckCode::B009 => CheckKind::GetAttrWithConstant,
CheckCode::B010 => CheckKind::SetAttrWithConstant, CheckCode::B010 => CheckKind::SetAttrWithConstant,
CheckCode::B011 => CheckKind::DoNotAssertFalse, CheckCode::B011 => CheckKind::DoNotAssertFalse,
@ -1064,7 +1064,7 @@ impl CheckKind {
CheckKind::StripWithMultiCharacters => &CheckCode::B005, CheckKind::StripWithMultiCharacters => &CheckCode::B005,
CheckKind::MutableArgumentDefault => &CheckCode::B006, CheckKind::MutableArgumentDefault => &CheckCode::B006,
CheckKind::UnusedLoopControlVariable(_) => &CheckCode::B007, CheckKind::UnusedLoopControlVariable(_) => &CheckCode::B007,
CheckKind::FunctionCallArgumentDefault => &CheckCode::B008, CheckKind::FunctionCallArgumentDefault(_) => &CheckCode::B008,
CheckKind::GetAttrWithConstant => &CheckCode::B009, CheckKind::GetAttrWithConstant => &CheckCode::B009,
CheckKind::SetAttrWithConstant => &CheckCode::B010, CheckKind::SetAttrWithConstant => &CheckCode::B010,
CheckKind::DoNotAssertFalse => &CheckCode::B011, CheckKind::DoNotAssertFalse => &CheckCode::B011,
@ -1389,8 +1389,12 @@ impl CheckKind {
"Loop control variable `{name}` not used within the loop body. If this is \ "Loop control variable `{name}` not used within the loop body. If this is \
intended, start the name with an underscore." intended, start the name with an underscore."
), ),
CheckKind::FunctionCallArgumentDefault => { CheckKind::FunctionCallArgumentDefault(name) => {
"Do not perform function calls in argument defaults.".to_string() if let Some(name) = name {
format!("Do not perform function call `{name}` in argument defaults")
} else {
"Do not perform function call in argument defaults".to_string()
}
} }
CheckKind::GetAttrWithConstant => "Do not call `getattr` with a constant attribute \ CheckKind::GetAttrWithConstant => "Do not call `getattr` with a constant attribute \
value, it is not any safer than normal property \ value, it is not any safer than normal property \

View file

@ -75,7 +75,7 @@ where
&& !is_nan_or_infinity(func, args) && !is_nan_or_infinity(func, args)
{ {
self.checks.push(( self.checks.push((
CheckKind::FunctionCallArgumentDefault, CheckKind::FunctionCallArgumentDefault(compose_call_path(expr)),
Range::from_located(expr), Range::from_located(expr),
)) ))
} }

View file

@ -2,7 +2,8 @@
source: src/flake8_bugbear/mod.rs source: src/flake8_bugbear/mod.rs
expression: checks expression: checks
--- ---
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: Depends
location: location:
row: 19 row: 19
column: 50 column: 50

View file

@ -2,7 +2,8 @@
source: src/linter.rs source: src/linter.rs
expression: checks expression: checks
--- ---
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: range
location: location:
row: 85 row: 85
column: 60 column: 60
@ -10,7 +11,8 @@ expression: checks
row: 85 row: 85
column: 68 column: 68
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: range
location: location:
row: 89 row: 89
column: 63 column: 63
@ -18,7 +20,8 @@ expression: checks
row: 89 row: 89
column: 71 column: 71
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: range
location: location:
row: 93 row: 93
column: 59 column: 59
@ -26,7 +29,8 @@ expression: checks
row: 93 row: 93
column: 67 column: 67
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: time.time
location: location:
row: 109 row: 109
column: 38 column: 38
@ -34,7 +38,8 @@ expression: checks
row: 109 row: 109
column: 49 column: 49
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: dt.datetime.now
location: location:
row: 113 row: 113
column: 11 column: 11
@ -42,7 +47,8 @@ expression: checks
row: 113 row: 113
column: 28 column: 28
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: dt.timedelta
location: location:
row: 113 row: 113
column: 31 column: 31
@ -50,7 +56,8 @@ expression: checks
row: 113 row: 113
column: 51 column: 51
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: ~
location: location:
row: 117 row: 117
column: 29 column: 29
@ -58,7 +65,8 @@ expression: checks
row: 117 row: 117
column: 44 column: 44
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: float
location: location:
row: 155 row: 155
column: 33 column: 33
@ -66,7 +74,8 @@ expression: checks
row: 155 row: 155
column: 47 column: 47
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: float
location: location:
row: 160 row: 160
column: 29 column: 29
@ -74,7 +83,8 @@ expression: checks
row: 160 row: 160
column: 37 column: 37
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: float
location: location:
row: 164 row: 164
column: 44 column: 44
@ -82,7 +92,8 @@ expression: checks
row: 164 row: 164
column: 57 column: 57
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: float
location: location:
row: 170 row: 170
column: 20 column: 20
@ -90,7 +101,8 @@ expression: checks
row: 170 row: 170
column: 28 column: 28
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: dt.datetime.now
location: location:
row: 170 row: 170
column: 30 column: 30
@ -98,7 +110,8 @@ expression: checks
row: 170 row: 170
column: 47 column: 47
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: map
location: location:
row: 176 row: 176
column: 21 column: 21
@ -106,7 +119,8 @@ expression: checks
row: 176 row: 176
column: 62 column: 62
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: random.randint
location: location:
row: 181 row: 181
column: 18 column: 18
@ -114,7 +128,8 @@ expression: checks
row: 181 row: 181
column: 59 column: 59
fix: ~ fix: ~
- kind: FunctionCallArgumentDefault - kind:
FunctionCallArgumentDefault: dt.datetime.now
location: location:
row: 181 row: 181
column: 36 column: 36