Remove redundant parentheses in case statement if guards (#4214) · psf/black@dab37a6 (original) (raw)

File tree

6 files changed

lines changed

6 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
20 20 expression (#4154)
21 21 - Checking for newline before adding one on docstring that is almost at the line limit
22 22 (#4185)
23 +- Remove redundant parentheses in `case` statement `if` guards (#4214).
23 24
24 25 ### Configuration
25 26
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ Currently, the following features are included in the preview style:
32 32 expressions that involve the power operator
33 33 - `docstring_check_for_newline`: checks if there is a newline before the terminating
34 34 quotes of a docstring
35 +- `remove_redundant_guard_parens`: Removes redundant parentheses in `if` guards for
36 +`case` blocks.
35 37
36 38 (labels/unstable-features)=
37 39
Original file line number Diff line number Diff line change
@@ -529,6 +529,8 @@ def __post_init__(self) -> None:
529 529 # PEP 634
530 530 self.visit_match_stmt = self.visit_match_case
531 531 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"})
532 534
533 535
534 536 def _hugging_power_ops_line_to_string(
Original file line number Diff line number Diff line change
@@ -179,6 +179,7 @@ class Preview(Enum):
179 179 typed_params_trailing_comma = auto()
180 180 is_simple_lookup_for_doublestar_expression = auto()
181 181 docstring_check_for_newline = auto()
182 +remove_redundant_guard_parens = auto()
182 183
183 184
184 185 UNSTABLE_FEATURES: Set[Preview] = {
Original file line number Diff line number Diff line change
@@ -87,7 +87,8 @@
87 87 "multiline_string_handling",
88 88 "typed_params_trailing_comma",
89 89 "is_simple_lookup_for_doublestar_expression",
90 -"docstring_check_for_newline"
90 +"docstring_check_for_newline",
91 +"remove_redundant_guard_parens"
91 92 ]
92 93 },
93 94 "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."
Original file line number Diff line number Diff 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