Fix formatting for if
clauses in match-case
blocks (#4269)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
8332a75e82
commit
f78b15712a
@ -14,6 +14,9 @@
|
||||
|
||||
<!-- Changes that affect Black's preview style -->
|
||||
|
||||
- `if` guards in `case` blocks are now wrapped in parentheses when the line is too long.
|
||||
(#4269)
|
||||
|
||||
### Configuration
|
||||
|
||||
<!-- Changes to how Black can be configured -->
|
||||
|
@ -34,6 +34,8 @@ Currently, the following features are included in the preview style:
|
||||
quotes of a docstring
|
||||
- `remove_redundant_guard_parens`: Removes redundant parentheses in `if` guards for
|
||||
`case` blocks.
|
||||
- `parens_for_long_if_clauses_in_case_block`: Adds parentheses to `if` clauses in `case`
|
||||
blocks when the the line is too long
|
||||
|
||||
(labels/unstable-features)=
|
||||
|
||||
|
@ -1310,6 +1310,16 @@ def normalize_invisible_parens( # noqa: C901
|
||||
child, parens_after={"case"}, mode=mode, features=features
|
||||
)
|
||||
|
||||
# Add parentheses around if guards in case blocks
|
||||
if (
|
||||
isinstance(child, Node)
|
||||
and child.type == syms.guard
|
||||
and Preview.parens_for_long_if_clauses_in_case_block in mode
|
||||
):
|
||||
normalize_invisible_parens(
|
||||
child, parens_after={"if"}, mode=mode, features=features
|
||||
)
|
||||
|
||||
# Add parentheses around long tuple unpacking in assignments.
|
||||
if (
|
||||
index == 0
|
||||
|
@ -180,6 +180,7 @@ class Preview(Enum):
|
||||
is_simple_lookup_for_doublestar_expression = auto()
|
||||
docstring_check_for_newline = auto()
|
||||
remove_redundant_guard_parens = auto()
|
||||
parens_for_long_if_clauses_in_case_block = auto()
|
||||
|
||||
|
||||
UNSTABLE_FEATURES: Set[Preview] = {
|
||||
|
@ -88,7 +88,8 @@
|
||||
"typed_params_trailing_comma",
|
||||
"is_simple_lookup_for_doublestar_expression",
|
||||
"docstring_check_for_newline",
|
||||
"remove_redundant_guard_parens"
|
||||
"remove_redundant_guard_parens",
|
||||
"parens_for_long_if_clauses_in_case_block"
|
||||
]
|
||||
},
|
||||
"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."
|
||||
|
@ -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
|
||||
|
72
tests/data/cases/pattern_matching_with_if_stmt.py
Normal file
72
tests/data/cases/pattern_matching_with_if_stmt.py
Normal file
@ -0,0 +1,72 @@
|
||||
# flags: --preview --minimum-version=3.10
|
||||
match match:
|
||||
case "test" if case != "not very loooooooooooooog condition": # comment
|
||||
pass
|
||||
|
||||
match smth:
|
||||
case "test" if "any long condition" != "another long condition" and "this is a long condition":
|
||||
pass
|
||||
case test if "any long condition" != "another long condition" and "this is a looooong condition":
|
||||
pass
|
||||
case test if "any long condition" != "another long condition" and "this is a looooong condition": # some additional comments
|
||||
pass
|
||||
case test if (True): # some comment
|
||||
pass
|
||||
case test if (False
|
||||
): # some comment
|
||||
pass
|
||||
case test if (True # some comment
|
||||
):
|
||||
pass # some comment
|
||||
case cases if (True # some comment
|
||||
): # some other comment
|
||||
pass # some comment
|
||||
case match if (True # some comment
|
||||
):
|
||||
pass # some comment
|
||||
|
||||
# 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 match:
|
||||
case "test" if case != "not very loooooooooooooog condition": # comment
|
||||
pass
|
||||
|
||||
match smth:
|
||||
case "test" if (
|
||||
"any long condition" != "another long condition" and "this is a long condition"
|
||||
):
|
||||
pass
|
||||
case test if (
|
||||
"any long condition" != "another long condition"
|
||||
and "this is a looooong condition"
|
||||
):
|
||||
pass
|
||||
case test if (
|
||||
"any long condition" != "another long condition"
|
||||
and "this is a looooong condition"
|
||||
): # some additional comments
|
||||
pass
|
||||
case test if True: # some comment
|
||||
pass
|
||||
case test if False: # some comment
|
||||
pass
|
||||
case test if True: # some comment
|
||||
pass # some comment
|
||||
case cases if True: # some comment # some other comment
|
||||
pass # some comment
|
||||
case match if True: # some comment
|
||||
pass # some comment
|
||||
|
||||
# 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…
Reference in New Issue
Block a user