Skip to content

Commit f78b157

Browse files
rdrllJelleZijlstra
andauthored
Fix formatting for if clauses in match-case blocks (#4269)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 8332a75 commit f78b157

File tree

7 files changed

+91
-2
lines changed

7 files changed

+91
-2
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
<!-- Changes that affect Black's preview style -->
1616

17+
- `if` guards in `case` blocks are now wrapped in parentheses when the line is too long.
18+
(#4269)
19+
1720
### Configuration
1821

1922
<!-- Changes to how Black can be configured -->

docs/the_black_code_style/future_style.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Currently, the following features are included in the preview style:
3434
quotes of a docstring
3535
- `remove_redundant_guard_parens`: Removes redundant parentheses in `if` guards for
3636
`case` blocks.
37+
- `parens_for_long_if_clauses_in_case_block`: Adds parentheses to `if` clauses in `case`
38+
blocks when the the line is too long
3739

3840
(labels/unstable-features)=
3941

src/black/linegen.py

+10
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,16 @@ def normalize_invisible_parens( # noqa: C901
13101310
child, parens_after={"case"}, mode=mode, features=features
13111311
)
13121312

1313+
# Add parentheses around if guards in case blocks
1314+
if (
1315+
isinstance(child, Node)
1316+
and child.type == syms.guard
1317+
and Preview.parens_for_long_if_clauses_in_case_block in mode
1318+
):
1319+
normalize_invisible_parens(
1320+
child, parens_after={"if"}, mode=mode, features=features
1321+
)
1322+
13131323
# Add parentheses around long tuple unpacking in assignments.
13141324
if (
13151325
index == 0

src/black/mode.py

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class Preview(Enum):
180180
is_simple_lookup_for_doublestar_expression = auto()
181181
docstring_check_for_newline = auto()
182182
remove_redundant_guard_parens = auto()
183+
parens_for_long_if_clauses_in_case_block = auto()
183184

184185

185186
UNSTABLE_FEATURES: Set[Preview] = {

src/black/resources/black.schema.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"typed_params_trailing_comma",
8989
"is_simple_lookup_for_doublestar_expression",
9090
"docstring_check_for_newline",
91-
"remove_redundant_guard_parens"
91+
"remove_redundant_guard_parens",
92+
"parens_for_long_if_clauses_in_case_block"
9293
]
9394
},
9495
"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."

tests/data/cases/pattern_matching_complex.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
match x:
8484
case [0]:
8585
y = 0
86-
case [1, 0] if (x := x[:0]):
86+
case [1, 0] if x := x[:0]:
8787
y = 1
8888
case [1, 0]:
8989
y = 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# flags: --preview --minimum-version=3.10
2+
match match:
3+
case "test" if case != "not very loooooooooooooog condition": # comment
4+
pass
5+
6+
match smth:
7+
case "test" if "any long condition" != "another long condition" and "this is a long condition":
8+
pass
9+
case test if "any long condition" != "another long condition" and "this is a looooong condition":
10+
pass
11+
case test if "any long condition" != "another long condition" and "this is a looooong condition": # some additional comments
12+
pass
13+
case test if (True): # some comment
14+
pass
15+
case test if (False
16+
): # some comment
17+
pass
18+
case test if (True # some comment
19+
):
20+
pass # some comment
21+
case cases if (True # some comment
22+
): # some other comment
23+
pass # some comment
24+
case match if (True # some comment
25+
):
26+
pass # some comment
27+
28+
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
29+
match x:
30+
case [1, 0] if x := x[:0]:
31+
y = 1
32+
case [1, 0] if (x := x[:0]):
33+
y = 1
34+
35+
# output
36+
37+
match match:
38+
case "test" if case != "not very loooooooooooooog condition": # comment
39+
pass
40+
41+
match smth:
42+
case "test" if (
43+
"any long condition" != "another long condition" and "this is a long condition"
44+
):
45+
pass
46+
case test if (
47+
"any long condition" != "another long condition"
48+
and "this is a looooong condition"
49+
):
50+
pass
51+
case test if (
52+
"any long condition" != "another long condition"
53+
and "this is a looooong condition"
54+
): # some additional comments
55+
pass
56+
case test if True: # some comment
57+
pass
58+
case test if False: # some comment
59+
pass
60+
case test if True: # some comment
61+
pass # some comment
62+
case cases if True: # some comment # some other comment
63+
pass # some comment
64+
case match if True: # some comment
65+
pass # some comment
66+
67+
# case black_test_patma_052 (originally in the pattern_matching_complex test case)
68+
match x:
69+
case [1, 0] if x := x[:0]:
70+
y = 1
71+
case [1, 0] if x := x[:0]:
72+
y = 1

0 commit comments

Comments
 (0)