From bd1e98034907463f5d86f4d87e89202dc6c34dd4 Mon Sep 17 00:00:00 2001 From: Joe Young <80432516+jpy-git@users.noreply.github.com> Date: Sat, 26 Mar 2022 16:56:50 +0000 Subject: [PATCH] Remove unnecessary parentheses from `except` clauses (#2939) Co-authored-by: Jelle Zijlstra --- CHANGES.md | 1 + src/black/linegen.py | 7 ++- tests/data/remove_except_parens.py | 79 ++++++++++++++++++++++++++++++ tests/test_format.py | 1 + 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/data/remove_except_parens.py diff --git a/CHANGES.md b/CHANGES.md index b2325b6..d34bd4e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,6 +15,7 @@ - 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) diff --git a/src/black/linegen.py b/src/black/linegen.py index 9c85e76..8a28c39 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -318,7 +318,12 @@ class LineGenerator(Visitor[Line]): 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=Ø) diff --git a/tests/data/remove_except_parens.py b/tests/data/remove_except_parens.py new file mode 100644 index 0000000..322c5b7 --- /dev/null +++ b/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 diff --git a/tests/test_format.py b/tests/test_format.py index b744685..a995bd3 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -80,6 +80,7 @@ PREVIEW_CASES: List[str] = [ "long_strings__edge_case", "long_strings__regression", "percent_precedence", + "remove_except_parens", "remove_for_brackets", "one_element_subscript", ] -- 2.39.2