diff --git a/CHANGES.md b/CHANGES.md index 5d4858da811..3583348f377 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -20,6 +20,7 @@ expression (#4154) - Checking for newline before adding one on docstring that is almost at the line limit (#4185) +- Remove redundant parentheses in `case` statement `if` guards (#4214). ### Configuration diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index f4534680645..4ae46cecded 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -32,6 +32,8 @@ Currently, the following features are included in the preview style: expressions that involve the power operator - `docstring_check_for_newline`: checks if there is a newline before the terminating quotes of a docstring +- `remove_redundant_guard_parens`: Removes redundant parentheses in `if` guards for + `case` blocks. (labels/unstable-features)= diff --git a/src/black/linegen.py b/src/black/linegen.py index c45a1308013..2b7fbc492cf 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -529,6 +529,8 @@ def __post_init__(self) -> None: # PEP 634 self.visit_match_stmt = self.visit_match_case self.visit_case_block = self.visit_match_case + if Preview.remove_redundant_guard_parens in self.mode: + self.visit_guard = partial(v, keywords=Ø, parens={"if"}) def _hugging_power_ops_line_to_string( diff --git a/src/black/mode.py b/src/black/mode.py index f9fad082e03..90c10c324a5 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -179,6 +179,7 @@ class Preview(Enum): typed_params_trailing_comma = auto() is_simple_lookup_for_doublestar_expression = auto() docstring_check_for_newline = auto() + remove_redundant_guard_parens = auto() UNSTABLE_FEATURES: Set[Preview] = { diff --git a/tests/data/cases/remove_redundant_parens_in_case_guard.py b/tests/data/cases/remove_redundant_parens_in_case_guard.py new file mode 100644 index 00000000000..fea76fa5d44 --- /dev/null +++ b/tests/data/cases/remove_redundant_parens_in_case_guard.py @@ -0,0 +1,24 @@ +# flags: --minimum-version=3.10 --preview + +match 1: + case _ if (True): + pass + + +match 1: + case _ if ( + True + ): + pass + + +# output + +match 1: + case _ if True: + pass + + +match 1: + case _ if True: + pass