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

Insert necessary padding in B014 fixes #7699

Merged
merged 1 commit into from
Sep 28, 2023
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 @@ -76,8 +76,15 @@ class MyError(Exception):
pass


# https://github.com/astral-sh/ruff/issues/6412
# Regression test for: https://github.com/astral-sh/ruff/issues/6412
try:
pass
except (ValueError, ValueError, TypeError):
pass


# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739801758
try:
pas
except(re.error, re.error):
p
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ruff_python_ast::call_path;
use ruff_python_ast::call_path::CallPath;

use crate::checkers::ast::Checker;
use crate::fix::edits::pad;
use crate::registry::{AsRule, Rule};

/// ## What it does
Expand Down Expand Up @@ -112,6 +113,7 @@ fn type_pattern(elts: Vec<&Expr>) -> Expr {
.into()
}

/// B014
fn duplicate_handler_exceptions<'a>(
checker: &mut Checker,
expr: &'a Expr,
Expand Down Expand Up @@ -146,8 +148,14 @@ fn duplicate_handler_exceptions<'a>(
);
if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
if unique_elts.len() == 1 {
checker.generator().expr(unique_elts[0])
// Single exceptions don't require parentheses, but since we're _removing_
// parentheses, insert whitespace as needed.
if let [elt] = unique_elts.as_slice() {
pad(
checker.generator().expr(elt),
expr.range(),
checker.locator(),
)
} else {
// Multiple exceptions must always be parenthesized. This is done
// manually as the generator never parenthesizes lone tuples.
Expand All @@ -163,6 +171,7 @@ fn duplicate_handler_exceptions<'a>(
seen
}

/// B025
pub(crate) fn duplicate_exceptions(checker: &mut Checker, handlers: &[ExceptHandler]) {
let mut seen: FxHashSet<CallPath> = FxHashSet::default();
let mut duplicates: FxHashMap<CallPath, Vec<&Expr>> = FxHashMap::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,31 @@ B014.py:82:8: B014 [*] Exception handler with duplicate exception: `ValueError`
= help: De-duplicate exceptions

ℹ Fix
79 79 | # https://github.com/astral-sh/ruff/issues/6412
79 79 | # Regression test for: https://github.com/astral-sh/ruff/issues/6412
80 80 | try:
81 81 | pass
82 |-except (ValueError, ValueError, TypeError):
82 |+except (ValueError, TypeError):
83 83 | pass
84 84 |
85 85 |

B014.py:89:7: B014 [*] Exception handler with duplicate exception: `re.error`
|
87 | try:
88 | pas
89 | except(re.error, re.error):
| ^^^^^^^^^^^^^^^^^^^^ B014
90 | p
|
= help: De-duplicate exceptions

ℹ Fix
86 86 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1739801758
87 87 | try:
88 88 | pas
89 |-except(re.error, re.error):
89 |+except re.error:
90 90 | p


Loading