diff --git a/CHANGES.md b/CHANGES.md index e28730a3b5f..95ec195d3db 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,9 @@ +- Fixed a bug where `if` clauses in `match-case` blocks are not wrapped with parenthesis + when the line is too long (#4269) + ### Configuration diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index 4ae46cecded..2d638bdf112 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -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`: Fixed a bug where `if` clauses in + `match-case` blocks are not wrapped with parenthesis when the line is too long (labels/unstable-features)= diff --git a/src/black/linegen.py b/src/black/linegen.py index e34ff040c73..599ce0ae8f6 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1310,6 +1310,17 @@ 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 when line is too long. + 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 diff --git a/src/black/mode.py b/src/black/mode.py index 90c10c324a5..b54f355e20a 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -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] = { diff --git a/src/black/resources/black.schema.json b/src/black/resources/black.schema.json index 8252a6c4bd8..5c800775d57 100644 --- a/src/black/resources/black.schema.json +++ b/src/black/resources/black.schema.json @@ -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." diff --git a/tests/data/cases/pattern_matching_complex.py b/tests/data/cases/pattern_matching_complex.py index ba64f2639a0..028832d772a 100644 --- a/tests/data/cases/pattern_matching_complex.py +++ b/tests/data/cases/pattern_matching_complex.py @@ -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 diff --git a/tests/data/cases/pattern_matching_with_if_stmt.py b/tests/data/cases/pattern_matching_with_if_stmt.py new file mode 100644 index 00000000000..3ec6b9d8f1b --- /dev/null +++ b/tests/data/cases/pattern_matching_with_if_stmt.py @@ -0,0 +1,56 @@ +# flags: --preview --minimum-version=3.10 +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 + +# 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