From c712d57ca9e30ba0db61c2fd7e4a2bf67f58bcc2 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 27 Oct 2023 12:17:54 -0700 Subject: [PATCH] Add trailing comma test case for hugging parens (#3991) --- docs/the_black_code_style/future_style.md | 13 +++++++++++++ ...hug_parens_with_braces_and_square_brackets.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index f2534b0f0d0..c744902577d 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -139,6 +139,19 @@ foo([ ]) ``` +You can use a magic trailing comma to avoid this compacting behavior; by default, +_Black_ will not reformat the following code: + +```python +foo( + [ + 1, + 2, + 3, + ], +) +``` + ### Improved multiline string handling _Black_ is smarter when formatting multiline strings, especially in function arguments, 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 98ed342fcbc..6d10518133c 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 @@ -36,6 +36,14 @@ def foo_square_brackets(request): ] ) +func( + [ + 'a', + 'b', + 'c', + ], +) + func( # a [ # b "c", # c @@ -171,6 +179,14 @@ def foo_square_brackets(request): "c", ]) +func( + [ + "a", + "b", + "c", + ], +) + func([ # a # b "c", # c "d", # d