Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow starred arguments in B030 #3871

Merged
merged 1 commit into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ~