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

Remove unnecessary parentheses from except clauses #2939

Merged
merged 6 commits into from Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,8 @@

<!-- Changes that affect Black's preview style -->

- Remove unnecessary parentheses from `except` statements (#2939)

### _Blackd_

<!-- Changes to blackd -->
Expand Down
7 changes: 6 additions & 1 deletion src/black/linegen.py
Expand Up @@ -313,7 +313,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 Preview.remove_except_parens in self.mode:
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
5 changes: 2 additions & 3 deletions src/black/mode.py
Expand Up @@ -127,6 +127,7 @@ class Preview(Enum):
"""Individual preview style features."""

string_processing = auto()
remove_except_parens = auto()
jpy-git marked this conversation as resolved.
Show resolved Hide resolved


class Deprecated(UserWarning):
Expand Down Expand Up @@ -162,9 +163,7 @@ def __contains__(self, feature: Preview) -> bool:
"""
if feature is Preview.string_processing:
return self.preview or self.experimental_string_processing
# TODO: Remove type ignore comment once preview contains more features
# than just ESP
return self.preview # type: ignore
return self.preview

def get_cache_key(self) -> str:
if self.target_versions:
Expand Down
40 changes: 40 additions & 0 deletions tests/data/remove_except_parens.py
@@ -0,0 +1,40 @@
# These brackets are redundant, therefore remove.
try:
a.something
except (AttributeError) as err:
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
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:
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 1 addition & 0 deletions tests/test_format.py
Expand Up @@ -79,6 +79,7 @@
"long_strings__edge_case",
"long_strings__regression",
"percent_precedence",
"remove_except_parens",
]

SOURCES: List[str] = [
Expand Down