Minor fixes for SIM105 (#1633)

This commit is contained in:
Harutaka Kawamura 2023-01-05 01:01:06 +09:00 committed by GitHub
parent 9ede902328
commit 12166584c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View file

@ -16,6 +16,11 @@ try:
except: # SIM105
pass
try:
foo()
except (a.Error, b.Error): # SIM105
pass
try:
foo()
except ValueError:
@ -29,3 +34,10 @@ except ValueError:
pass
else:
print('bar')
try:
foo()
except ValueError:
pass
finally:
print('bar')

View file

@ -1248,7 +1248,10 @@ where
}
}
StmtKind::Try {
handlers, orelse, ..
handlers,
orelse,
finalbody,
..
} => {
if self.settings.enabled.contains(&CheckCode::F707) {
if let Some(check) =
@ -1275,7 +1278,9 @@ where
);
}
if self.settings.enabled.contains(&CheckCode::SIM105) {
flake8_simplify::plugins::use_contextlib_suppress(self, stmt, handlers, orelse);
flake8_simplify::plugins::use_contextlib_suppress(
self, stmt, handlers, orelse, finalbody,
);
}
}
StmtKind::Assign { targets, value, .. } => {

View file

@ -11,8 +11,9 @@ pub fn use_contextlib_suppress(
stmt: &Stmt,
handlers: &[Excepthandler],
orelse: &[Stmt],
finalbody: &[Stmt],
) {
if handlers.len() != 1 || !orelse.is_empty() {
if handlers.len() != 1 || !orelse.is_empty() || !finalbody.is_empty() {
return;
}
let handler = &handlers[0];
@ -21,7 +22,7 @@ pub fn use_contextlib_suppress(
if matches!(body[0].node, StmtKind::Pass) {
let handler_names: Vec<_> = helpers::extract_handler_names(handlers)
.into_iter()
.flatten()
.map(|v| v.join("."))
.collect();
let exception = if handler_names.is_empty() {
"Exception".to_string()

View file

@ -32,4 +32,14 @@ expression: checks
column: 8
fix: ~
parent: ~
- kind:
UseContextlibSuppress: "a.Error, b.Error"
location:
row: 19
column: 0
end_location:
row: 22
column: 8
fix: ~
parent: ~