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

[flake8-bugbear] Allow singleton tuples with starred expressions in B013 #12484

Merged
merged 4 commits into from
Jul 24, 2024
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
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]
Loading