From e19aa766b9bd8e0c42708865b4d7e18b146ba688 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:18:52 +0300 Subject: [PATCH 01/12] Add test case --- .../preview_hug_parens_with_braces_and_square_brackets.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py index 6d10518133c..8b23beab034 100644 --- a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py +++ b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py @@ -137,6 +137,8 @@ def foo_square_brackets(request): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ) +foo(*["long long long long long line", "long long long long long line", "long long long long long line"]) + # output def foo_brackets(request): return JsonResponse({ @@ -287,3 +289,9 @@ def foo_square_brackets(request): baaaaaaaaaaaaar( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], {x}, "a string", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ) + +foo(*[ + "long long long long long line", + "long long long long long line", + "long long long long long line", +]) From a80be1a7f5236c1c587a1cf9fc70289fa9a86a51 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:19:45 +0300 Subject: [PATCH 02/12] Hug also multiline list unpacking as sole argument --- src/black/linegen.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 5f5a69152d5..fd40414cc4a 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -817,16 +817,17 @@ def _first_right_hand_split( head_leaves.reverse() if Preview.hug_parens_with_braces_and_square_brackets in line.mode: + is_unpacking = 1 if body_leaves[0].type == token.STAR else 0 if ( tail_leaves[0].type == token.RPAR and tail_leaves[0].value and tail_leaves[0].opening_bracket is head_leaves[-1] and body_leaves[-1].type in [token.RBRACE, token.RSQB] - and body_leaves[-1].opening_bracket is body_leaves[0] + and body_leaves[-1].opening_bracket is body_leaves[is_unpacking] ): - head_leaves = head_leaves + body_leaves[:1] + head_leaves = head_leaves + body_leaves[: 1 + is_unpacking] tail_leaves = body_leaves[-1:] + tail_leaves - body_leaves = body_leaves[1:-1] + body_leaves = body_leaves[1 + is_unpacking : -1] head = bracket_split_build_line( head_leaves, line, opening_bracket, component=_BracketSplitComponent.head From e0c10f9eb83f6d11a946d347eeb513c8e743aba2 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:20:22 +0300 Subject: [PATCH 03/12] Add changelog entry --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 84d9061135a..d8a367bdde1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ - Multiline dictionaries and lists that are the sole argument to a function are now indented less (#3964) +- Multiline list unpacking as the sole argument to a function is now also indented less + (#XXXX) ### Configuration From 72af34ce81107dfe0981174fbc62b9c06668f650 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:24:08 +0300 Subject: [PATCH 04/12] Update docs --- docs/the_black_code_style/future_style.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index c744902577d..9b4e6bb7a6c 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -139,6 +139,28 @@ foo([ ]) ``` +This also applies to list unpacking: + +```python +foo( + *[ + 1, + 2, + 3, + ] +) +``` + +will become: + +```python +foo(*[ + 1, + 2, + 3, +]) +``` + You can use a magic trailing comma to avoid this compacting behavior; by default, _Black_ will not reformat the following code: From f6412caa84c76675fa1afaa9857ea2850608dd87 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:37:58 +0300 Subject: [PATCH 05/12] Update PR number --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index d8a367bdde1..64c055e2b0c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -15,7 +15,7 @@ - Multiline dictionaries and lists that are the sole argument to a function are now indented less (#3964) - Multiline list unpacking as the sole argument to a function is now also indented less - (#XXXX) + (#3992) ### Configuration From aa07c0a832aa933a4125fbb5cccbe2d3f83ebbc3 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:42:23 +0300 Subject: [PATCH 06/12] Add one more test case --- .../preview_hug_parens_with_braces_and_square_brackets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py index 8b23beab034..3f13865817f 100644 --- a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py +++ b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py @@ -139,6 +139,8 @@ def foo_square_brackets(request): foo(*["long long long long long line", "long long long long long line", "long long long long long line"]) +foo(*[str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)]) + # output def foo_brackets(request): return JsonResponse({ @@ -295,3 +297,7 @@ def foo_square_brackets(request): "long long long long long line", "long long long long long line", ]) + +foo(*[ + str(i) for i in range(100000000000000000000000000000000000000000000000000000000000) +]) From a6ada8bedfcd0bfe2c48fc1b2264060d8ff31fb2 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 00:47:14 +0300 Subject: [PATCH 07/12] Better example for the docs --- docs/the_black_code_style/future_style.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index 9b4e6bb7a6c..88b8f26a00c 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -144,9 +144,8 @@ This also applies to list unpacking: ```python foo( *[ - 1, - 2, - 3, + a_long_function_name(a_long_variable_name) + for a_long_variable_name in some_generator ] ) ``` @@ -155,9 +154,8 @@ will become: ```python foo(*[ - 1, - 2, - 3, + a_long_function_name(a_long_variable_name) + for a_long_variable_name in some_generator ]) ``` From 57ca43d282e848c894c199a8a114add11d7883d3 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 10:24:27 +0300 Subject: [PATCH 08/12] Add case for dict unpacking --- ...hug_parens_with_braces_and_square_brackets.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py index 3f13865817f..40343bf18f8 100644 --- a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py +++ b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py @@ -141,6 +141,15 @@ def foo_square_brackets(request): foo(*[str(i) for i in range(100000000000000000000000000000000000000000000000000000000000)]) +foo( + **{ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1, + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 2, + "ccccccccccccccccccccccccccccccccc": 3, + **other, + } +) + # output def foo_brackets(request): return JsonResponse({ @@ -301,3 +310,10 @@ def foo_square_brackets(request): foo(*[ str(i) for i in range(100000000000000000000000000000000000000000000000000000000000) ]) + +foo(**{ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa": 1, + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb": 2, + "ccccccccccccccccccccccccccccccccc": 3, + **other, +}) From a1f159645655e0c3385e528d3e2e4044038dc021 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 10:24:59 +0300 Subject: [PATCH 09/12] Handle also dict unpacking --- src/black/linegen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index fd40414cc4a..43bc08efbbd 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -817,7 +817,7 @@ def _first_right_hand_split( head_leaves.reverse() if Preview.hug_parens_with_braces_and_square_brackets in line.mode: - is_unpacking = 1 if body_leaves[0].type == token.STAR else 0 + is_unpacking = 1 if body_leaves[0].type in [token.STAR, token.DOUBLESTAR] else 0 if ( tail_leaves[0].type == token.RPAR and tail_leaves[0].value From 43e72bd93f376ec18d9184cd6aba9e974acae0bd Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 10:27:05 +0300 Subject: [PATCH 10/12] Add notion of dict unpacking to changelog and docs --- CHANGES.md | 4 ++-- docs/the_black_code_style/future_style.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 64c055e2b0c..4e5bd9ca771 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,8 +14,8 @@ - Multiline dictionaries and lists that are the sole argument to a function are now indented less (#3964) -- Multiline list unpacking as the sole argument to a function is now also indented less - (#3992) +- Multiline list and dict unpacking as the sole argument to a function is now also + indented less (#3992) ### Configuration diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index 88b8f26a00c..944ffad033e 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -139,7 +139,7 @@ foo([ ]) ``` -This also applies to list unpacking: +This also applies to list and dictionary unpacking: ```python foo( From f995744bced78e151a3d182b4c3a5df92d955ad8 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sat, 28 Oct 2023 16:40:05 +0300 Subject: [PATCH 11/12] Run black on cache.py --- src/black/cache.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/black/cache.py b/src/black/cache.py index 6baa096baca..6a332304981 100644 --- a/src/black/cache.py +++ b/src/black/cache.py @@ -124,9 +124,9 @@ def filtered_cached(self, sources: Iterable[Path]) -> Tuple[Set[Path], Set[Path] def write(self, sources: Iterable[Path]) -> None: """Update the cache file data and write a new cache file.""" - self.file_data.update( - **{str(src.resolve()): Cache.get_file_data(src) for src in sources} - ) + self.file_data.update(**{ + str(src.resolve()): Cache.get_file_data(src) for src in sources + }) try: CACHE_DIR.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile( From 79b8eca846eef440a0b4fec72ca082c566761526 Mon Sep 17 00:00:00 2001 From: Henri Holopainen Date: Sun, 29 Oct 2023 09:42:07 +0200 Subject: [PATCH 12/12] Add test case --- .../preview_hug_parens_with_braces_and_square_brackets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py index 40343bf18f8..51fe516add5 100644 --- a/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py +++ b/tests/data/cases/preview_hug_parens_with_braces_and_square_brackets.py @@ -150,6 +150,8 @@ def foo_square_brackets(request): } ) +foo(**{x: y for x, y in enumerate(["long long long long line","long long long long line"])}) + # output def foo_brackets(request): return JsonResponse({ @@ -317,3 +319,7 @@ def foo_square_brackets(request): "ccccccccccccccccccccccccccccccccc": 3, **other, }) + +foo(**{ + x: y for x, y in enumerate(["long long long long line", "long long long long line"]) +})