-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 tuples of exceptions (B030
)
#10437
Merged
charliermarsh
merged 11 commits into
astral-sh:main
from
ottaviohartman:thartman/B030-tuples
Mar 18, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e3f71c8
working but not with star packed tuples
ottaviohartman 1c76c3a
test passing
ottaviohartman e49bbd2
cleanup
ottaviohartman e4e795b
insta
ottaviohartman 95f0fdc
doc
ottaviohartman a2d1e76
add one more test
ottaviohartman 2d70e29
fix recursion
ottaviohartman b720717
add more tests
ottaviohartman b3aa93a
remove print
ottaviohartman e2bb506
Merge branch 'main' into thartman/B030-tuples
charliermarsh bd64fc0
Add a * test case
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use std::collections::VecDeque; | ||
|
||
use ruff_python_ast::{self as ast, ExceptHandler, Expr}; | ||
use ruff_python_ast::{self as ast, ExceptHandler, Expr, Operator}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
@@ -44,30 +44,6 @@ impl Violation for ExceptWithNonExceptionClasses { | |
} | ||
} | ||
|
||
/// Given an [`Expr`], flatten any [`Expr::Starred`] expressions. | ||
/// This should leave any unstarred iterables alone (subsequently raising a | ||
/// warning for B029). | ||
fn flatten_starred_iterables(expr: &Expr) -> Vec<&Expr> { | ||
let Expr::Tuple(ast::ExprTuple { elts, .. }) = expr else { | ||
return vec![expr]; | ||
}; | ||
let mut flattened_exprs: Vec<&Expr> = Vec::with_capacity(elts.len()); | ||
let mut exprs_to_process: VecDeque<&Expr> = elts.iter().collect(); | ||
while let Some(expr) = exprs_to_process.pop_front() { | ||
match expr { | ||
Expr::Starred(ast::ExprStarred { value, .. }) => match value.as_ref() { | ||
Expr::Tuple(ast::ExprTuple { elts, .. }) | ||
| Expr::List(ast::ExprList { elts, .. }) => { | ||
exprs_to_process.append(&mut elts.iter().collect()); | ||
} | ||
_ => flattened_exprs.push(value), | ||
}, | ||
_ => flattened_exprs.push(expr), | ||
} | ||
} | ||
flattened_exprs | ||
} | ||
|
||
/// B030 | ||
pub(crate) fn except_with_non_exception_classes( | ||
checker: &mut Checker, | ||
|
@@ -78,7 +54,7 @@ pub(crate) fn except_with_non_exception_classes( | |
let Some(type_) = type_ else { | ||
return; | ||
}; | ||
for expr in flatten_starred_iterables(type_) { | ||
for expr in flatten_iterables(type_) { | ||
if !matches!( | ||
expr, | ||
Expr::Subscript(_) | Expr::Attribute(_) | Expr::Name(_) | Expr::Call(_), | ||
|
@@ -89,3 +65,61 @@ pub(crate) fn except_with_non_exception_classes( | |
} | ||
} | ||
} | ||
|
||
/// Given an [`Expr`], flatten any [`Expr::Starred`] expressions and any | ||
/// [`Expr::BinOp`] expressions into a flat list of expressions. | ||
/// | ||
/// This should leave any unstarred iterables alone (subsequently raising a | ||
/// warning for B029). | ||
fn flatten_iterables(expr: &Expr) -> Vec<&Expr> { | ||
// Unpack the top-level Tuple into queue, otherwise add as-is. | ||
let mut exprs_to_process: VecDeque<&Expr> = match expr { | ||
Expr::Tuple(ast::ExprTuple { elts, .. }) => elts.iter().collect(), | ||
_ => vec![expr].into(), | ||
}; | ||
let mut flattened_exprs: Vec<&Expr> = Vec::with_capacity(exprs_to_process.len()); | ||
|
||
while let Some(expr) = exprs_to_process.pop_front() { | ||
match expr { | ||
Expr::Starred(ast::ExprStarred { value, .. }) => match value.as_ref() { | ||
Expr::Tuple(ast::ExprTuple { elts, .. }) | ||
| Expr::List(ast::ExprList { elts, .. }) => { | ||
exprs_to_process.append(&mut elts.iter().collect()); | ||
} | ||
Expr::BinOp(ast::ExprBinOp { | ||
op: Operator::Add, .. | ||
}) => { | ||
exprs_to_process.push_back(value); | ||
} | ||
_ => flattened_exprs.push(value), | ||
}, | ||
Expr::BinOp(ast::ExprBinOp { | ||
left, | ||
right, | ||
op: Operator::Add, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gated these to |
||
.. | ||
}) => { | ||
for expr in [left, right] { | ||
// If left or right are tuples, starred, or binary operators, flatten them. | ||
match expr.as_ref() { | ||
Expr::Tuple(ast::ExprTuple { elts, .. }) => { | ||
exprs_to_process.append(&mut elts.iter().collect()); | ||
} | ||
Expr::Starred(ast::ExprStarred { value, .. }) => { | ||
exprs_to_process.push_back(value); | ||
} | ||
Expr::BinOp(ast::ExprBinOp { | ||
op: Operator::Add, .. | ||
}) => { | ||
exprs_to_process.push_back(expr); | ||
} | ||
_ => flattened_exprs.push(expr), | ||
} | ||
} | ||
} | ||
_ => flattened_exprs.push(expr), | ||
} | ||
} | ||
|
||
flattened_exprs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@autinerd added this test case