From d4fb95587f80a55a58c859720558bb4fbe08f446 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Fri, 18 Mar 2022 14:39:59 +0000 Subject: [PATCH 1/3] Remove unnecessary parentheses from Except clauses --- CHANGES.md | 2 ++ src/black/linegen.py | 7 +++++- src/black/mode.py | 5 ++-- tests/data/remove_except_parens.py | 40 ++++++++++++++++++++++++++++++ tests/test_format.py | 1 + 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 tests/data/remove_except_parens.py diff --git a/CHANGES.md b/CHANGES.md index bb3ccb9ed9f..f7fdd1db3d4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ +- Remove unnecessary parentheses from `except` statements (#2939) + ### _Blackd_ diff --git a/src/black/linegen.py b/src/black/linegen.py index 4dc242a1dfe..2a5105b0154 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -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=Ø) diff --git a/src/black/mode.py b/src/black/mode.py index 35a072c23e0..a091d432c92 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -127,6 +127,7 @@ class Preview(Enum): """Individual preview style features.""" string_processing = auto() + remove_except_parens = auto() class Deprecated(UserWarning): @@ -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: diff --git a/tests/data/remove_except_parens.py b/tests/data/remove_except_parens.py new file mode 100644 index 00000000000..f49c837330c --- /dev/null +++ b/tests/data/remove_except_parens.py @@ -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 diff --git a/tests/test_format.py b/tests/test_format.py index 269bbacd249..64f4ad135ea 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -79,6 +79,7 @@ "long_strings__edge_case", "long_strings__regression", "percent_precedence", + "remove_except_parens", ] SOURCES: List[str] = [ From ce394b60a5ddb134bda7cc67beeb5ebf64f1f310 Mon Sep 17 00:00:00 2001 From: jpy-git Date: Mon, 21 Mar 2022 23:54:54 +0000 Subject: [PATCH 2/3] Update for latest master --- src/black/linegen.py | 2 +- src/black/mode.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index f48bcafbe89..31e396f8db8 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -313,7 +313,7 @@ def __post_init__(self) -> None: self.visit_try_stmt = partial( v, keywords={"try", "except", "else", "finally"}, parens=Ø ) - if Preview.remove_except_parens in self.mode: + if self.mode.preview: self.visit_except_clause = partial( v, keywords={"except"}, parens={"except"} ) diff --git a/src/black/mode.py b/src/black/mode.py index a091d432c92..3c3d94de186 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -127,7 +127,7 @@ class Preview(Enum): """Individual preview style features.""" string_processing = auto() - remove_except_parens = auto() + remove_redundant_parens = auto() class Deprecated(UserWarning): From aed21dfbddc4050dbd7bcb2a054b2d3d73688d6b Mon Sep 17 00:00:00 2001 From: jpy-git Date: Wed, 23 Mar 2022 09:42:56 +0000 Subject: [PATCH 3/3] Add extra unit tests for long lines --- tests/data/remove_except_parens.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/data/remove_except_parens.py b/tests/data/remove_except_parens.py index f49c837330c..322c5b7a51b 100644 --- a/tests/data/remove_except_parens.py +++ b/tests/data/remove_except_parens.py @@ -18,6 +18,22 @@ 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: @@ -38,3 +54,26 @@ 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