Remove unnecessary parentheses from except
clauses (#2939)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
14d84ba2e9
commit
bd1e980349
@ -15,6 +15,7 @@
|
|||||||
<!-- Changes that affect Black's preview style -->
|
<!-- Changes that affect Black's preview style -->
|
||||||
|
|
||||||
- Code cell separators `#%%` are now standardised to `# %%` (#2919)
|
- 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)
|
- Remove unnecessary parentheses from tuple unpacking in `for` loops (#2945)
|
||||||
- Avoid magic-trailing-comma in single-element subscripts (#2942)
|
- Avoid magic-trailing-comma in single-element subscripts (#2942)
|
||||||
|
|
||||||
|
@ -318,6 +318,11 @@ def __post_init__(self) -> None:
|
|||||||
self.visit_try_stmt = partial(
|
self.visit_try_stmt = partial(
|
||||||
v, keywords={"try", "except", "else", "finally"}, parens=Ø
|
v, keywords={"try", "except", "else", "finally"}, 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_except_clause = partial(v, keywords={"except"}, parens=Ø)
|
||||||
self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
|
self.visit_with_stmt = partial(v, keywords={"with"}, parens=Ø)
|
||||||
self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
|
self.visit_funcdef = partial(v, keywords={"def"}, parens=Ø)
|
||||||
|
79
tests/data/remove_except_parens.py
Normal file
79
tests/data/remove_except_parens.py
Normal file
@ -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
|
@ -80,6 +80,7 @@
|
|||||||
"long_strings__edge_case",
|
"long_strings__edge_case",
|
||||||
"long_strings__regression",
|
"long_strings__regression",
|
||||||
"percent_precedence",
|
"percent_precedence",
|
||||||
|
"remove_except_parens",
|
||||||
"remove_for_brackets",
|
"remove_for_brackets",
|
||||||
"one_element_subscript",
|
"one_element_subscript",
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user