Skip to content

Commit

Permalink
Merge d4fb955 into fa7f015
Browse files Browse the repository at this point in the history
  • Loading branch information
jpy-git committed Mar 18, 2022
2 parents fa7f015 + d4fb955 commit 87556f3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class Preview(Enum):
"""Individual preview style features."""

string_processing = auto()
remove_except_parens = auto()


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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"long_strings__edge_case",
"long_strings__regression",
"percent_precedence",
"remove_except_parens",
]

SOURCES: List[str] = [
Expand Down

0 comments on commit 87556f3

Please sign in to comment.