From 4e0185be2f33e0a3a2b37ff2983a13f1c1108a8b Mon Sep 17 00:00:00 2001 From: Kelvin Li <13176405+rdrll@users.noreply.github.com> Date: Fri, 8 Mar 2024 16:33:00 -0800 Subject: [PATCH 1/8] fix --- src/black/linegen.py | 7 +++ tests/data/cases/pattern_matching_complex.py | 2 +- .../cases/pattern_matching_with_if_stmt.py | 56 +++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/data/cases/pattern_matching_with_if_stmt.py diff --git a/src/black/linegen.py b/src/black/linegen.py index e34ff040c73..2918302e152 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -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: + 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/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..455c7b777bb --- /dev/null +++ b/tests/data/cases/pattern_matching_with_if_stmt.py @@ -0,0 +1,56 @@ +# flags: --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 From 66e2b1015e94c38bb42d741a82058650dc798632 Mon Sep 17 00:00:00 2001 From: Kelvin Li <13176405+rdrll@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:06:00 -0800 Subject: [PATCH 2/8] Update CHANGES.md --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index e28730a3b5f..a49985be1ad 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,8 @@ - Don't move comments along with delimiters, which could cause crashes (#4248) +- Fixed a bug where long `if` clauses in `match-case` blocks are not wrapped with + parenthesis (#4269) ### Preview style From 9b67dfb03b0e17970e30ae6448564404fedfc55f Mon Sep 17 00:00:00 2001 From: Kelvin Li <13176405+rdrll@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:21:57 -0800 Subject: [PATCH 3/8] Update CHANGES.md --- CHANGES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a49985be1ad..335bb7f6b5a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,8 +11,8 @@ - Don't move comments along with delimiters, which could cause crashes (#4248) -- Fixed a bug where long `if` clauses in `match-case` blocks are not wrapped with - parenthesis (#4269) +- Fixed a bug where `if` clauses in `match-case` blocks are not wrapped with parenthesis + when the line is too long (#4269) ### Preview style From 6042717a0ee809211cf04b5d406ac958a41906b8 Mon Sep 17 00:00:00 2001 From: Kelvin Li <13176405+rdrll@users.noreply.github.com> Date: Fri, 8 Mar 2024 19:28:54 -0800 Subject: [PATCH 4/8] Move to preview --- CHANGES.md | 5 +++-- docs/the_black_code_style/future_style.md | 2 ++ src/black/linegen.py | 8 ++++++-- src/black/mode.py | 1 + src/black/resources/black.schema.json | 3 ++- tests/data/cases/pattern_matching_with_if_stmt.py | 2 +- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 335bb7f6b5a..95ec195d3db 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,13 +11,14 @@ - 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 +- 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 2918302e152..599ce0ae8f6 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1311,8 +1311,12 @@ def normalize_invisible_parens( # noqa: C901 ) # Fixes a bug where invisible parens are not properly wrapped around - # if statement. - if isinstance(child, Node) and child.type == syms.guard: + # 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 ) 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_with_if_stmt.py b/tests/data/cases/pattern_matching_with_if_stmt.py index 455c7b777bb..3ec6b9d8f1b 100644 --- a/tests/data/cases/pattern_matching_with_if_stmt.py +++ b/tests/data/cases/pattern_matching_with_if_stmt.py @@ -1,4 +1,4 @@ -# flags: --minimum-version=3.10 +# flags: --preview --minimum-version=3.10 match "test": case "test" if "first long condition" != "some loooooooooooooooooooooooooooooooooooooog condition": print("Test") From 820c188111fff0f8876bd4f524369e1846e46243 Mon Sep 17 00:00:00 2001 From: Kelvin Li <13176405+rdrll@users.noreply.github.com> Date: Fri, 8 Mar 2024 19:49:15 -0800 Subject: [PATCH 5/8] trigger CI test again --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 95ec195d3db..de20f184325 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,7 +17,7 @@ - Fixed a bug where `if` clauses in `match-case` blocks are not wrapped with parenthesis - when the line is too long (#4269) + when the line is too long. (#4269) ### Configuration From 28798482f0469295e6ea905138557d48f2f5bc04 Mon Sep 17 00:00:00 2001 From: rdrll <13176405+rdrll@users.noreply.github.com> Date: Wed, 13 Mar 2024 00:47:00 -0700 Subject: [PATCH 6/8] Apply suggestions from code review Co-authored-by: Jelle Zijlstra --- CHANGES.md | 2 +- docs/the_black_code_style/future_style.md | 4 ++-- src/black/linegen.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index de20f184325..c50c8a67ea4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ -- Fixed a bug where `if` clauses in `match-case` blocks are not wrapped with parenthesis +- `if` guards in `case` blocks are now wrapped in parentheses 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 2d638bdf112..e7d487a3f44 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -34,8 +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 +- `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)= diff --git a/src/black/linegen.py b/src/black/linegen.py index 599ce0ae8f6..2d9c27a6141 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1310,8 +1310,7 @@ 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. + # Add parentheses around if guards in case blocks if ( isinstance(child, Node) and child.type == syms.guard From a12c5a1270e70dd6ee05555f25e07d9eb34d7acf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:47:22 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGES.md | 4 ++-- docs/the_black_code_style/future_style.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c50c8a67ea4..fe234380799 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,8 +16,8 @@ -- `if` guards in `case` blocks are now wrapped in parentheses - when the line is too long. (#4269) +- `if` guards in `case` blocks are now wrapped in parentheses 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 e7d487a3f44..e0f45b47106 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -34,8 +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 +- `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)= From c87416a736441c015121595f2d7ef9a41dc6ab4d Mon Sep 17 00:00:00 2001 From: Kelvin Li <13176405+rdrll@users.noreply.github.com> Date: Fri, 15 Mar 2024 15:11:50 -0700 Subject: [PATCH 8/8] New tests --- .../cases/pattern_matching_with_if_stmt.py | 70 ++++++++++++------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/tests/data/cases/pattern_matching_with_if_stmt.py b/tests/data/cases/pattern_matching_with_if_stmt.py index 3ec6b9d8f1b..ff54af91771 100644 --- a/tests/data/cases/pattern_matching_with_if_stmt.py +++ b/tests/data/cases/pattern_matching_with_if_stmt.py @@ -1,19 +1,29 @@ # 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") + case "test" if case != "not very loooooooooooooog condition": # comment + pass -match "test": +match smth: 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") + 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: @@ -24,29 +34,35 @@ # 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") + case "test" if case != "not very loooooooooooooog condition": # comment + pass -match "test": +match smth: case "test" if ( "any long condition" != "another long condition" and "this is a long condition" ): - print("Test") - -match "test": - case "test" if ( + pass + case test if ( "any long condition" != "another long condition" and "this is a looooong condition" ): - print("Test") + 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: