<!-- 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)
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=Ø)
--- /dev/null
+# 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