Skip to content

Commit 01c29bd

Browse files
rxjacobpre-commit-ci[bot]cobaltt7
authored
Fix #4653 If guard explosion in case statement (#4884)
* Updated changelog to resolve conflict * Gating change to preview style, fixed merge conflict * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * removed extra entries * added test case * Fixed preview style and unintentional formatting changes * removed extra line in mode.py * added unstable change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestion from @cobaltt7 * Move docs to preview style * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Reorder enum values in black.schema.json --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: cobalt <61329810+cobaltt7@users.noreply.github.com>
1 parent 88e7833 commit 01c29bd

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
<!-- Changes that affect Black's preview style -->
2222

23+
- Fix bug where `if` guards in `case` blocks were incorrectly split when the pattern had
24+
a trailing comma (#4884)
2325
- Fix `string_processing` crashing on unassigned long string literals with trailing
2426
commas (one-item tuples) (#4929)
2527
- Simplify implementation of the power operator "hugging" logic (#4918)

docs/the_black_code_style/future_style.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Currently, the following features are included in the preview style:
2222
([see below](labels/simplify-power-operator))
2323
- `wrap_long_dict_values_in_parens`: Add parentheses around long values in dictionaries.
2424
([see below](labels/wrap-long-dict-values))
25+
- `fix_if_guard_explosion_in_case_statement`: fixed exploding of the if guard in case
26+
patterns which have trailing commas in them, even if the guard expression fits in one
27+
line
2528

2629
(labels/wrap-comprehension-in)=
2730

src/black/linegen.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,18 @@ def normalize_invisible_parens(
15981598
break
15991599

16001600
elif not is_multiline_string(child):
1601-
wrap_in_parentheses(node, child, visible=False)
1601+
if (
1602+
Preview.fix_if_guard_explosion_in_case_statement in mode
1603+
and node.type == syms.guard
1604+
):
1605+
mock_line = Line(mode=mode)
1606+
for leaf in child.leaves():
1607+
mock_line.append(leaf)
1608+
# If it's a guard AND it's short, we DON'T wrap
1609+
if not is_line_short_enough(mock_line, mode=mode):
1610+
wrap_in_parentheses(node, child, visible=False)
1611+
else:
1612+
wrap_in_parentheses(node, child, visible=False)
16021613

16031614
comma_check = child.type == token.COMMA
16041615

src/black/mode.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ class Preview(Enum):
229229
wrap_comprehension_in = auto()
230230
simplify_power_operator_hugging = auto()
231231
wrap_long_dict_values_in_parens = auto()
232+
fix_if_guard_explosion_in_case_statement = auto()
232233

233234

234235
UNSTABLE_FEATURES: set[Preview] = {

src/black/resources/black.schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@
8484
"hug_parens_with_braces_and_square_brackets",
8585
"wrap_comprehension_in",
8686
"simplify_power_operator_hugging",
87-
"wrap_long_dict_values_in_parens"
87+
"wrap_long_dict_values_in_parens",
88+
"fix_if_guard_explosion_in_case_statement"
8889
]
8990
},
9091
"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."
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# flags: --preview
2+
3+
def f(x):
4+
match x:
5+
# good refactor
6+
case [
7+
y
8+
] if y == 123:
9+
pass
10+
11+
case [
12+
y
13+
] if True:
14+
pass
15+
16+
case [
17+
y,
18+
] if True:
19+
pass
20+
21+
# bad refactor
22+
case [
23+
y,
24+
] if y == 123:
25+
pass
26+
27+
28+
# output
29+
30+
31+
def f(x):
32+
match x:
33+
# good refactor
34+
case [y] if y == 123:
35+
pass
36+
37+
case [y] if True:
38+
pass
39+
40+
case [
41+
y,
42+
] if True:
43+
pass
44+
45+
# bad refactor
46+
case [
47+
y,
48+
] if y == 123:
49+
pass

0 commit comments

Comments
 (0)