Skip to content

Commit

Permalink
Remove unnecessary parentheses from except clauses (#2939)
Browse files Browse the repository at this point in the history
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
jpy-git and JelleZijlstra committed Mar 26, 2022
1 parent 14d84ba commit bd1e980
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -15,6 +15,7 @@
<!-- Changes that affect Black's preview style -->

- Code cell separators `#%%` are now standardised to `# %%` (#2919)
- Remove unnecessary parentheses from `except` statements (#2939)
- Remove unnecessary parentheses from tuple unpacking in `for` loops (#2945)
- Avoid magic-trailing-comma in single-element subscripts (#2942)

Expand Down
7 changes: 6 additions & 1 deletion src/black/linegen.py
Expand Up @@ -318,7 +318,12 @@ def __post_init__(self) -> None:
self.visit_try_stmt = partial(
v, keywords={"try", "except", "else", "finally"}, parens=Ø
)
self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
if self.mode.preview:
self.visit_except_clause = partial(
v, keywords={"except"}, parens={"except"}
)
else:
self.visit_except_clause = partial(v, keywords={"except"}, parens=Ø)
self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
Expand Down
79 changes: 79 additions & 0 deletions tests/data/remove_except_parens.py
@@ -0,0 +1,79 @@
# These brackets are redundant, therefore remove.
try:
a.something
except (AttributeError) as err:
raise err

# This is tuple of exceptions.
# Although this could be replaced with just the exception,
# we do not remove brackets to preserve AST.
try:
a.something
except (AttributeError,) as err:
raise err

# This is a tuple of exceptions. Do not remove brackets.
try:
a.something
except (AttributeError, ValueError) as err:
raise err

# Test long variants.
try:
a.something
except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error) as err:
raise err

try:
a.something
except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,) as err:
raise err

try:
a.something
except (some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error, some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error) as err:
raise err

# output
# These brackets are redundant, therefore remove.
try:
a.something
except AttributeError as err:
raise err

# This is tuple of exceptions.
# Although this could be replaced with just the exception,
# we do not remove brackets to preserve AST.
try:
a.something
except (AttributeError,) as err:
raise err

# This is a tuple of exceptions. Do not remove brackets.
try:
a.something
except (AttributeError, ValueError) as err:
raise err

# Test long variants.
try:
a.something
except (
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error
) as err:
raise err

try:
a.something
except (
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
) as err:
raise err

try:
a.something
except (
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
some.really.really.really.looooooooooooooooooooooooooooooooong.module.over89.chars.Error,
) as err:
raise err
1 change: 1 addition & 0 deletions tests/test_format.py
Expand Up @@ -80,6 +80,7 @@
"long_strings__edge_case",
"long_strings__regression",
"percent_precedence",
"remove_except_parens",
"remove_for_brackets",
"one_element_subscript",
]
Expand Down

0 comments on commit bd1e980

Please sign in to comment.