Skip to content

Commit

Permalink
[flake8-bugbear] Allow singleton tuples with starred expressions in B…
Browse files Browse the repository at this point in the history
…013 (#12484)
  • Loading branch information
dylwil3 authored Jul 24, 2024
1 parent eac965e commit 8890735
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@
pass
except(ValueError,):
pass

list_exceptions = [FileExistsError, FileNotFoundError]

try:
pass
except (*list_exceptions,):
pass
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_starred;
use ruff_python_ast::{self as ast, ExceptHandler, Expr};
use ruff_text_size::Ranged;

Expand All @@ -11,6 +10,9 @@ use crate::fix::edits::pad;
/// Checks for single-element tuples in exception handlers (e.g.,
/// `except (ValueError,):`).
///
/// Note: Single-element tuples consisting of a starred expression
/// are allowed.
///
/// ## Why is this bad?
/// A tuple with a single element can be more concisely and idiomatically
/// expressed as a single value.
Expand Down Expand Up @@ -69,7 +71,17 @@ pub(crate) fn redundant_tuple_in_exception_handler(
let [elt] = elts.as_slice() else {
continue;
};
let elt = map_starred(elt);
// It is not safe to replace a single-element
// tuple consisting of a starred expression
// by the unstarred expression because the unstarred
// expression can be any iterable whereas `except` must
// be followed by a literal or a tuple. For example:
// ```python
// except (*[ValueError,FileNotFoundError],)
// ```
if elt.is_starred_expr() {
continue;
}
let mut diagnostic = Diagnostic::new(
RedundantTupleInExceptionHandler {
name: checker.generator().expr(elt),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,6 @@ B013.py:5:8: B013 [*] A length-one tuple literal is redundant in exception handl
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
13 | except(ValueError,):
|
= help: Replace with `except retriable_exceptions`

Safe fix
8 8 | pass
9 9 | except (ImportError, TypeError):
10 10 | pass
11 |-except (*retriable_exceptions,):
11 |+except retriable_exceptions:
12 12 | pass
13 13 | except(ValueError,):
14 14 | pass

B013.py:13:7: B013 [*] A length-one tuple literal is redundant in exception handlers
|
11 | except (*retriable_exceptions,):
Expand All @@ -60,5 +39,5 @@ B013.py:13:7: B013 [*] A length-one tuple literal is redundant in exception hand
13 |-except(ValueError,):
13 |+except ValueError:
14 14 | pass


15 15 |
16 16 | list_exceptions = [FileExistsError, FileNotFoundError]

0 comments on commit 8890735

Please # to comment.