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

Improve error messages for except* (B025, B029, B030, B904) #14791 #14815

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,6 +1,6 @@
"""
Should emit:
B025 - on lines 15, 22, 31
B025 - on lines 15, 22, 31, 40, 47, 56
"""

import pickle
Expand Down Expand Up @@ -36,3 +36,28 @@
a = 2
except (OSError, TypeError):
a = 2

try:
a = 1
except* ValueError:
a = 2
except* ValueError:
a = 2

try:
a = 1
except* pickle.PickleError:
a = 2
except* ValueError:
a = 2
except* pickle.PickleError:
a = 2

try:
a = 1
except* (ValueError, TypeError):
a = 2
except* ValueError:
a = 2
except* (OSError, TypeError):
a = 2
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
handlers,
orelse,
finalbody,
is_star,
..
}) => {
if checker.enabled(Rule::TooManyNestedBlocks) {
Expand All @@ -1459,7 +1460,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
Rule::DuplicateHandlerException,
Rule::DuplicateTryBlockException,
]) {
flake8_bugbear::rules::duplicate_exceptions(checker, handlers);
flake8_bugbear::rules::duplicate_exceptions(checker, handlers, *is_star);
}
if checker.enabled(Rule::RedundantTupleInExceptionHandler) {
flake8_bugbear::rules::redundant_tuple_in_exception_handler(checker, handlers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ use crate::registry::Rule;
#[derive(ViolationMetadata)]
pub(crate) struct DuplicateTryBlockException {
name: String,
is_star: bool,
}

impl Violation for DuplicateTryBlockException {
#[derive_message_formats]
fn message(&self) -> String {
let DuplicateTryBlockException { name } = self;
format!("try-except block with duplicate exception `{name}`")
let DuplicateTryBlockException { name, is_star } = self;
let star = if *is_star {
"*".to_string()
} else {
String::new()
};
format!("try-except{star} block with duplicate exception `{name}`")
smokyabdulrahman marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -170,7 +176,11 @@ fn duplicate_handler_exceptions<'a>(
}

/// B025
pub(crate) fn duplicate_exceptions(checker: &mut Checker, handlers: &[ExceptHandler]) {
pub(crate) fn duplicate_exceptions(
checker: &mut Checker,
handlers: &[ExceptHandler],
is_star: bool,
) {
let mut seen: FxHashSet<UnqualifiedName> = FxHashSet::default();
let mut duplicates: FxHashMap<UnqualifiedName, Vec<&Expr>> = FxHashMap::default();
for handler in handlers {
Expand Down Expand Up @@ -210,6 +220,7 @@ pub(crate) fn duplicate_exceptions(checker: &mut Checker, handlers: &[ExceptHand
checker.diagnostics.push(Diagnostic::new(
DuplicateTryBlockException {
name: name.segments().join("."),
is_star,
},
expr.range(),
));
Expand Down
Loading