Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix long case blocks not split into multiple lines #4024

Merged
merged 13 commits into from Nov 7, 2023
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -12,6 +12,8 @@
- Fix crash when whitespace followed a backslash before newline in a docstring (#4008)

- Fix crash on formatting code like `await (a ** b)` (#3994)
- Fix a bug that long `case` blocks will not be split into multiple lines, also enabled
rdrll marked this conversation as resolved.
Show resolved Hide resolved
general trailing comma rules on `case` blocks (#4024)

### Preview style

Expand Down
7 changes: 7 additions & 0 deletions src/black/linegen.py
Expand Up @@ -1260,6 +1260,13 @@ def normalize_invisible_parens(
child, parens_after=parens_after, mode=mode, features=features
)

# Fixes a bug where invisible parens are not properly wrapped around
# case blocks.
if isinstance(child, Node) and child.type == syms.case_block:
rdrll marked this conversation as resolved.
Show resolved Hide resolved
normalize_invisible_parens(
child, parens_after={"case"}, mode=mode, features=features
)

# Add parentheses around long tuple unpacking in assignments.
if (
index == 0
Expand Down
20 changes: 2 additions & 18 deletions tests/data/cases/pattern_matching_extras.py
Expand Up @@ -30,22 +30,6 @@ def func(match: case, case: match) -> case:
...


match maybe, multiple:
case perhaps, 5:
pass
case perhaps, 6,:
pass


match more := (than, one), indeed,:
case _, (5, 6):
pass
case [[5], (6)], [7],:
pass
case _:
pass


match a, *b, c:
case [*_]:
assert "seq" == _
Expand All @@ -71,12 +55,12 @@ def func(match: case, case: match) -> case:
case [a as match]:
pass

case case:
case anything:
pass


match match:
case case:
case something:
pass


Expand Down
4 changes: 2 additions & 2 deletions tests/data/cases/pattern_matching_generic.py
Expand Up @@ -25,9 +25,9 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
]

match match:
case case:
case something:
match match:
case case:
case something:
pass

if all(version.is_python2() for version in target_versions):
Expand Down
39 changes: 39 additions & 0 deletions tests/data/cases/pattern_matching_long.py
@@ -0,0 +1,39 @@
# flags: --minimum-version=3.10
match x:
case "abcd" | "abcd" | "abcd" :
pass
case "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd" | "abcd":
pass
case xxxxxxxxxxxxxxxxxxxxxxx:
pass
case case:
pass

# output

match x:
case "abcd" | "abcd" | "abcd":
pass
case (
"abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
| "abcd"
):
pass
case xxxxxxxxxxxxxxxxxxxxxxx:
pass
case case :
pass

39 changes: 39 additions & 0 deletions tests/data/cases/pattern_matching_trailing_comma.py
@@ -0,0 +1,39 @@
# flags: --minimum-version=3.10
match maybe, multiple:
case perhaps, 5:
pass
case perhaps, 6,:
pass


match more := (than, one), indeed,:
case _, (5, 6):
pass
case [[5], (6)], [7],:
pass
case _:
pass


# output

match maybe, multiple:
case perhaps, 5:
pass
case (
perhaps,
6,
):
pass


match more := (than, one), indeed,:
case _, (5, 6):
pass
case (
[[5], (6)],
[7],
):
pass
case _:
pass