Skip to content

Commit

Permalink
Allow starred arguments in B030
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 3, 2023
1 parent f4173b2 commit 2f1aa4b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
33 changes: 32 additions & 1 deletion crates/ruff/resources/test/fixtures/flake8_bugbear/B030.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
pass


try:
pass
except (*a, *(RuntimeError, (KeyError, TypeError))): # error
pass

try:
pass
except (ValueError, *(RuntimeError, TypeError)): # ok
Expand All @@ -38,10 +43,36 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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),
)),
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~

0 comments on commit 2f1aa4b

Please sign in to comment.