Skip to content

Commit dab37a6

Browse files
authored
Remove redundant parentheses in case statement if guards (#4214)
A follow up to #4024 but for `if` guards in `case` statements. I noticed this when #4024 was made stable, and noticed I had some code that had extra parens around the `if` guard.
1 parent 32230e6 commit dab37a6

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed

Diff for: CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
expression (#4154)
2121
- Checking for newline before adding one on docstring that is almost at the line limit
2222
(#4185)
23+
- Remove redundant parentheses in `case` statement `if` guards (#4214).
2324

2425
### Configuration
2526

Diff for: docs/the_black_code_style/future_style.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Currently, the following features are included in the preview style:
3232
expressions that involve the power operator
3333
- `docstring_check_for_newline`: checks if there is a newline before the terminating
3434
quotes of a docstring
35+
- `remove_redundant_guard_parens`: Removes redundant parentheses in `if` guards for
36+
`case` blocks.
3537

3638
(labels/unstable-features)=
3739

Diff for: src/black/linegen.py

+2
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ def __post_init__(self) -> None:
529529
# PEP 634
530530
self.visit_match_stmt = self.visit_match_case
531531
self.visit_case_block = self.visit_match_case
532+
if Preview.remove_redundant_guard_parens in self.mode:
533+
self.visit_guard = partial(v, keywords=Ø, parens={"if"})
532534

533535

534536
def _hugging_power_ops_line_to_string(

Diff for: src/black/mode.py

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class Preview(Enum):
179179
typed_params_trailing_comma = auto()
180180
is_simple_lookup_for_doublestar_expression = auto()
181181
docstring_check_for_newline = auto()
182+
remove_redundant_guard_parens = auto()
182183

183184

184185
UNSTABLE_FEATURES: Set[Preview] = {

Diff for: src/black/resources/black.schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
"multiline_string_handling",
8888
"typed_params_trailing_comma",
8989
"is_simple_lookup_for_doublestar_expression",
90-
"docstring_check_for_newline"
90+
"docstring_check_for_newline",
91+
"remove_redundant_guard_parens"
9192
]
9293
},
9394
"description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features."
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# flags: --minimum-version=3.10 --preview --line-length=79
2+
3+
match 1:
4+
case _ if (True):
5+
pass
6+
7+
8+
match 1:
9+
case _ if (
10+
True
11+
):
12+
pass
13+
14+
15+
match 1:
16+
case _ if (
17+
# this is a comment
18+
True
19+
):
20+
pass
21+
22+
23+
match 1:
24+
case _ if (
25+
True
26+
# this is a comment
27+
):
28+
pass
29+
30+
31+
match 1:
32+
case _ if (
33+
True # this is a comment
34+
):
35+
pass
36+
37+
38+
match 1:
39+
case _ if ( # this is a comment
40+
True
41+
):
42+
pass
43+
44+
45+
match 1:
46+
case _ if (
47+
True
48+
): # this is a comment
49+
pass
50+
51+
52+
match 1:
53+
case _ if (True): # comment over the line limit unless parens are removed x
54+
pass
55+
56+
57+
match 1:
58+
case _ if (True): # comment over the line limit and parens should go to next line
59+
pass
60+
61+
62+
# output
63+
64+
match 1:
65+
case _ if True:
66+
pass
67+
68+
69+
match 1:
70+
case _ if True:
71+
pass
72+
73+
74+
match 1:
75+
case _ if (
76+
# this is a comment
77+
True
78+
):
79+
pass
80+
81+
82+
match 1:
83+
case _ if (
84+
True
85+
# this is a comment
86+
):
87+
pass
88+
89+
90+
match 1:
91+
case _ if True: # this is a comment
92+
pass
93+
94+
95+
match 1:
96+
case _ if True: # this is a comment
97+
pass
98+
99+
100+
match 1:
101+
case _ if True: # this is a comment
102+
pass
103+
104+
105+
match 1:
106+
case _ if True: # comment over the line limit unless parens are removed x
107+
pass
108+
109+
110+
match 1:
111+
case (
112+
_
113+
) if True: # comment over the line limit and parens should go to next line
114+
pass

0 commit comments

Comments
 (0)