Allow starred arguments in B030 (#3871)

This commit is contained in:
Charlie Marsh 2023-04-03 19:20:34 -04:00 committed by GitHub
parent f4173b2a93
commit 60f6a8571a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 12 deletions

View File

@ -28,6 +28,11 @@ except (ValueError, *(RuntimeError, (KeyError, TypeError))): # error
pass
try:
pass
except (*a, *(RuntimeError, (KeyError, TypeError))): # error
pass
try:
pass
except (ValueError, *(RuntimeError, TypeError)): # ok
@ -38,10 +43,36 @@ try:
except (ValueError, *[RuntimeError, *(TypeError,)]): # ok
pass
try:
pass
except (*a, *b): # ok
pass
try:
pass
except (*a, *(RuntimeError, TypeError)): # ok
pass
try:
pass
except (*a, *(b, c)): # ok
pass
try:
pass
except (*a, *(*b, *c)): # ok
pass
def what_to_catch():
return ...
try:
pass
except what_to_catch(): # ok
pass
pass

View File

@ -13,17 +13,17 @@ pub struct ExceptWithEmptyTuple;
impl Violation for ExceptWithEmptyTuple {
#[derive_message_formats]
fn message(&self) -> String {
format!("Using except (): with an empty tuple does not handle/catch anything. Add exceptions to handle.")
format!("Using `except ():` with an empty tuple does not catch anything; add exceptions to handle")
}
}
/// B029
pub fn except_with_empty_tuple(checker: &mut Checker, excepthandler: &Excepthandler) {
let ExcepthandlerKind::ExceptHandler { type_, .. } = &excepthandler.node;
if type_.is_none() {
let Some(type_) = type_ else {
return;
}
let ExprKind::Tuple { elts, .. } = &type_.as_ref().unwrap().node else {
};
let ExprKind::Tuple { elts, .. } = &type_.node else {
return;
};
if elts.is_empty() {

View File

@ -33,7 +33,7 @@ fn flatten_starred_iterables(expr: &Expr) -> Vec<&Expr> {
ExprKind::Tuple { elts, .. } | ExprKind::List { elts, .. } => {
exprs_to_process.append(&mut elts.iter().collect());
}
_ => flattened_exprs.push(expr),
_ => flattened_exprs.push(value),
},
_ => flattened_exprs.push(expr),
}
@ -48,12 +48,17 @@ pub fn except_with_non_exception_classes(checker: &mut Checker, excepthandler: &
return;
};
for expr in flatten_starred_iterables(type_) {
match expr.node {
ExprKind::Attribute { .. } | ExprKind::Name { .. } | ExprKind::Call { .. } => (),
_ => checker.diagnostics.push(Diagnostic::new(
if !matches!(
&expr.node,
ExprKind::Subscript { .. }
| ExprKind::Attribute { .. }
| ExprKind::Name { .. }
| ExprKind::Call { .. },
) {
checker.diagnostics.push(Diagnostic::new(
ExceptWithNonExceptionClasses,
Range::from(expr),
)),
));
}
}
}

View File

@ -4,7 +4,7 @@ expression: diagnostics
---
- kind:
name: ExceptWithEmptyTuple
body: "Using except (): with an empty tuple does not handle/catch anything. Add exceptions to handle."
body: "Using `except ():` with an empty tuple does not catch anything; add exceptions to handle"
suggestion: ~
fixable: false
location:
@ -18,7 +18,7 @@ expression: diagnostics
parent: ~
- kind:
name: ExceptWithEmptyTuple
body: "Using except (): with an empty tuple does not handle/catch anything. Add exceptions to handle."
body: "Using `except ():` with an empty tuple does not catch anything; add exceptions to handle"
suggestion: ~
fixable: false
location:

View File

@ -58,4 +58,18 @@ expression: diagnostics
fix:
edits: []
parent: ~
- kind:
name: ExceptWithNonExceptionClasses
body: "`except` handlers should only be exception classes or tuples of exception classes"
suggestion: ~
fixable: false
location:
row: 33
column: 28
end_location:
row: 33
column: 49
fix:
edits: []
parent: ~