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 formatting for if clauses in match-case blocks #4269

Merged
merged 8 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<!-- Changes that affect Black's stable style -->

- Don't move comments along with delimiters, which could cause crashes (#4248)
- Fixed a bug where `if` clauses in `match-case` blocks are not wrapped with parenthesis
when the line is too long (#4269)

### Preview style

Expand Down
7 changes: 7 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,13 @@ def normalize_invisible_parens( # noqa: C901
child, parens_after={"case"}, mode=mode, features=features
)

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

# Add parentheses around long tuple unpacking in assignments.
if (
index == 0
Expand Down
2 changes: 1 addition & 1 deletion tests/data/cases/pattern_matching_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
match x:
case [0]:
y = 0
case [1, 0] if (x := x[:0]):
case [1, 0] if x := x[:0]:
y = 1
case [1, 0]:
y = 2
Expand Down
56 changes: 56 additions & 0 deletions tests/data/cases/pattern_matching_with_if_stmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# flags: --minimum-version=3.10
match "test":
rdrll marked this conversation as resolved.
Show resolved Hide resolved
case "test" if "first long condition" != "some loooooooooooooooooooooooooooooooooooooog condition":
print("Test")

match match:
case "test" if case != "not very loooooooooooooog condition":
print("No format change")

match "test":
case "test" if "any long condition" != "another long condition" and "this is a long condition":
print("Test")

match "test":
case "test" if "any long condition" != "another long condition" and "this is a looooong condition":
print("Test")

# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if (x := x[:0]):
y = 1

# output

match "test":
case "test" if (
"first long condition"
!= "some loooooooooooooooooooooooooooooooooooooog condition"
):
print("Test")

match match:
case "test" if case != "not very loooooooooooooog condition":
print("No format change")

match "test":
case "test" if (
"any long condition" != "another long condition" and "this is a long condition"
):
print("Test")

match "test":
case "test" if (
"any long condition" != "another long condition"
and "this is a looooong condition"
):
print("Test")

# case black_test_patma_052 (originally in the pattern_matching_complex test case)
match x:
case [1, 0] if x := x[:0]:
y = 1
case [1, 0] if x := x[:0]:
y = 1
Loading