Skip to content

Commit 289d1e8

Browse files
authored
Manually parenthesize tuple expr in B014 autofix (#6415)
## Summary Manually add the parentheses around tuple expressions for the autofix in `B014`. This is also done in various other autofixes as well such as for [`RUF005`](https://github.com/astral-sh/ruff/blob/6df5ab40987cba7bb5681c2445caaf83ebcfbf7d/crates/ruff/src/rules/ruff/rules/collection_literal_concatenation.rs#L183-L184), [`UP024`](https://github.com/astral-sh/ruff/blob/6df5ab40987cba7bb5681c2445caaf83ebcfbf7d/crates/ruff/src/rules/pyupgrade/rules/os_error_alias.rs#L137-L137). ### Alternate Solution An alternate solution would be to fix this in the `Generator` itself by checking if the tuple expression needs to be generated at the top-level or not. If so, then always add the parentheses. ```rust } else if level == 0 { // Top-level tuples are always parenthesized. self.p("("); let mut first = true; for elt in elts { self.p_delim(&mut first, ", "); self.unparse_expr(elt, precedence::COMMA); } self.p_if(elts.len() == 1, ","); self.p(")"); ``` ## Test Plan Add a regression test for this case in `B014`. fixes: #6412
1 parent 6df5ab4 commit 289d1e8

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

crates/ruff/resources/test/fixtures/flake8_bugbear/B014.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,10 @@ class MyError(Exception):
7474
except (ValueError, binascii.Error):
7575
# binascii.Error is a subclass of ValueError.
7676
pass
77+
78+
79+
# https://github.com/astral-sh/ruff/issues/6412
80+
try:
81+
pass
82+
except (ValueError, ValueError, TypeError):
83+
pass

crates/ruff/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ fn duplicate_handler_exceptions<'a>(
150150
if unique_elts.len() == 1 {
151151
checker.generator().expr(unique_elts[0])
152152
} else {
153-
checker.generator().expr(&type_pattern(unique_elts))
153+
// Multiple exceptions must always be parenthesized. This is done
154+
// manually as the generator never parenthesizes lone tuples.
155+
format!("({})", checker.generator().expr(&type_pattern(unique_elts)))
154156
},
155157
expr.range(),
156158
)));

crates/ruff/src/rules/flake8_bugbear/snapshots/ruff__rules__flake8_bugbear__tests__B014_B014.py.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,22 @@ B014.py:49:8: B014 [*] Exception handler with duplicate exception: `re.error`
6464
51 51 | pass
6565
52 52 |
6666

67+
B014.py:82:8: B014 [*] Exception handler with duplicate exception: `ValueError`
68+
|
69+
80 | try:
70+
81 | pass
71+
82 | except (ValueError, ValueError, TypeError):
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ B014
73+
83 | pass
74+
|
75+
= help: De-duplicate exceptions
76+
77+
Fix
78+
79 79 | # https://github.com/astral-sh/ruff/issues/6412
79+
80 80 | try:
80+
81 81 | pass
81+
82 |-except (ValueError, ValueError, TypeError):
82+
82 |+except (ValueError, TypeError):
83+
83 83 | pass
84+
6785

0 commit comments

Comments
 (0)