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 required space when fixing B013 #7148

Merged
merged 1 commit into from
Sep 5, 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
2 changes: 2 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_bugbear/B013.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
pass
except (*retriable_exceptions,):
pass
except(ValueError,):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ruff_python_ast::{self as ast, ExceptHandler, Expr};
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::helpers::map_starred;
use ruff_text_size::Ranged;
use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
Expand Down Expand Up @@ -78,8 +78,26 @@ pub(crate) fn redundant_tuple_in_exception_handler(
type_.range(),
);
if checker.patch(diagnostic.kind.rule()) {
// If there's no space between the `except` and the tuple, we need to insert a space,
// as in:
// ```python
// except(ValueError,):
// ```
// Otherwise, the output will be invalid syntax, since we're removing a set of
// parentheses.
let requires_space = checker
.locator()
.slice(TextRange::up_to(type_.start()))
.chars()
.last()
.is_some_and(|char| char.is_ascii_alphabetic());
let content = checker.generator().expr(elt);
diagnostic.set_fix(Fix::automatic(Edit::range_replacement(
checker.generator().expr(elt),
if requires_space {
format!(" {content}")
} else {
content
},
type_.range(),
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ B013.py:11:8: B013 [*] A length-one tuple literal is redundant in exception hand
11 | except (*retriable_exceptions,):
| ^^^^^^^^^^^^^^^^^^^^^^^^ B013
12 | pass
13 | except(ValueError,):
|
= help: Replace with `except retriable_exceptions`

Expand All @@ -39,5 +40,25 @@ B013.py:11:8: B013 [*] A length-one tuple literal is redundant in exception hand
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,):
12 | pass
13 | except(ValueError,):
| ^^^^^^^^^^^^^ B013
14 | pass
|
= help: Replace with `except ValueError`

ℹ Fix
10 10 | pass
11 11 | except (*retriable_exceptions,):
12 12 | pass
13 |-except(ValueError,):
13 |+except ValueError:
14 14 | pass


Loading