Skip to content

Commit

Permalink
Supported starred exceptions in length-one tuple detection (#7080)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Sep 3, 2023
1 parent b70dde4 commit b0d171a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
4 changes: 4 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B013.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
retriable_exceptions = (FileExistsError, FileNotFoundError)

try:
pass
except (ValueError,):
Expand All @@ -6,3 +8,5 @@
pass
except (ImportError, TypeError):
pass
except (*retriable_exceptions,):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ruff_python_ast::{self as ast, ExceptHandler, Expr};

use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_starred;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -41,11 +42,7 @@ pub struct RedundantTupleInExceptionHandler {
impl AlwaysAutofixableViolation for RedundantTupleInExceptionHandler {
#[derive_message_formats]
fn message(&self) -> String {
let RedundantTupleInExceptionHandler { name } = self;
format!(
"A length-one tuple literal is redundant. Write `except {name}` instead of `except \
({name},)`."
)
format!("A length-one tuple literal is redundant in exception handlers")
}

fn autofix_title(&self) -> String {
Expand All @@ -70,9 +67,10 @@ pub(crate) fn redundant_tuple_in_exception_handler(
let Expr::Tuple(ast::ExprTuple { elts, .. }) = type_.as_ref() else {
continue;
};
let [elt] = &elts[..] else {
let [elt] = elts.as_slice() else {
continue;
};
let elt = map_starred(elt);
let mut diagnostic = Diagnostic::new(
RedundantTupleInExceptionHandler {
name: checker.generator().expr(elt),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
---
source: crates/ruff/src/rules/flake8_bugbear/mod.rs
---
B013.py:3:8: B013 [*] A length-one tuple literal is redundant. Write `except ValueError` instead of `except (ValueError,)`.
B013.py:5:8: B013 [*] A length-one tuple literal is redundant in exception handlers
|
1 | try:
2 | pass
3 | except (ValueError,):
| ^^^^^^^^^^^^^ B013
3 | try:
4 | pass
5 | except AttributeError:
5 | except (ValueError,):
| ^^^^^^^^^^^^^ B013
6 | pass
7 | except AttributeError:
|
= help: Replace with `except ValueError`

Fix
1 1 | try:
2 2 | pass
3 |-except (ValueError,):
3 |+except ValueError:
2 2 |
3 3 | try:
4 4 | pass
5 5 | except AttributeError:
5 |-except (ValueError,):
5 |+except ValueError:
6 6 | pass
7 7 | except AttributeError:
8 8 | pass

B013.py:11:8: B013 [*] A length-one tuple literal is redundant in exception handlers
|
9 | except (ImportError, TypeError):
10 | pass
11 | except (*retriable_exceptions,):
| ^^^^^^^^^^^^^^^^^^^^^^^^ B013
12 | pass
|
= help: Replace with `except retriable_exceptions`

Fix
8 8 | pass
9 9 | except (ImportError, TypeError):
10 10 | pass
11 |-except (*retriable_exceptions,):
11 |+except retriable_exceptions:
12 12 | pass


11 changes: 11 additions & 0 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,17 @@ pub fn map_subscript(expr: &Expr) -> &Expr {
}
}

/// Given an [`Expr`] that can be starred, return the underlying starred expression.
pub fn map_starred(expr: &Expr) -> &Expr {
if let Expr::Starred(ast::ExprStarred { value, .. }) = expr {
// Ex) `*args`
value
} else {
// Ex) `args`
expr
}
}

/// Return `true` if the body uses `locals()`, `globals()`, `vars()`, `eval()`.
///
/// Accepts a closure that determines whether a given name (e.g., `"list"`) is a Python builtin.
Expand Down

0 comments on commit b0d171a

Please sign in to comment.