From 7b2c2ca72c19184e05d1d3625875a0fd2dcd9c9b Mon Sep 17 00:00:00 2001 From: KindaOK <27974340+KindaOK@users.noreply.github.com> Date: Wed, 9 Nov 2022 01:40:37 -0500 Subject: [PATCH 1/4] Create failing test case Copied the examples from the bug report and added an additional test case with the failing behavior multiple times in the same expression. --- .../data/simple_cases/deep_trailing_commas.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/data/simple_cases/deep_trailing_commas.py diff --git a/tests/data/simple_cases/deep_trailing_commas.py b/tests/data/simple_cases/deep_trailing_commas.py new file mode 100644 index 00000000000..b664d829ee5 --- /dev/null +++ b/tests/data/simple_cases/deep_trailing_commas.py @@ -0,0 +1,33 @@ +assert foo(1, 2, 3,)[0] == {'bar': 'baz'} + +a = [1,][2]() +b = [1,][2](3) + +[1, 2,]()(3, 4,)()(5, 6,)(0)(0) + +# output + + +assert foo( + 1, + 2, + 3, +)[0] == {'bar': 'baz'} + +a = [ + 1, +][2]() +b = [ + 1, +][2](3) + +[ + 1, + 2, +]()( + 3, + 4, +)()( + 5, + 6, +)(0) From 8429ebc2eabdd3ec984505ccff265e692cb49f7d Mon Sep 17 00:00:00 2001 From: KindaOK <27974340+KindaOK@users.noreply.github.com> Date: Wed, 9 Nov 2022 02:20:28 -0500 Subject: [PATCH 2/4] Correct quote type in test case Black always uses double quotes. Test would also fail on this, but that's not the reason the test is failing to begin with. --- tests/data/simple_cases/deep_trailing_commas.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/data/simple_cases/deep_trailing_commas.py b/tests/data/simple_cases/deep_trailing_commas.py index b664d829ee5..cdf1e21a485 100644 --- a/tests/data/simple_cases/deep_trailing_commas.py +++ b/tests/data/simple_cases/deep_trailing_commas.py @@ -1,4 +1,4 @@ -assert foo(1, 2, 3,)[0] == {'bar': 'baz'} +assert foo(1, 2, 3,)[0] == {"bar": "baz"} a = [1,][2]() b = [1,][2](3) @@ -12,7 +12,7 @@ 1, 2, 3, -)[0] == {'bar': 'baz'} +)[0] == {"bar": "baz"} a = [ 1, From 6079db6e8fc45ec846fedc80d0993b6159781986 Mon Sep 17 00:00:00 2001 From: KindaOK <27974340+KindaOK@users.noreply.github.com> Date: Wed, 16 Nov 2022 02:18:04 -0500 Subject: [PATCH 3/4] Fix another test bug Correct output was missing a parentheses group in the original. --- tests/data/simple_cases/deep_trailing_commas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data/simple_cases/deep_trailing_commas.py b/tests/data/simple_cases/deep_trailing_commas.py index cdf1e21a485..1eeeb92179c 100644 --- a/tests/data/simple_cases/deep_trailing_commas.py +++ b/tests/data/simple_cases/deep_trailing_commas.py @@ -30,4 +30,4 @@ )()( 5, 6, -)(0) +)(0)(0) From 48042ee74472de912b66780f6d96844b6df48535 Mon Sep 17 00:00:00 2001 From: KindaOK <27974340+KindaOK@users.noreply.github.com> Date: Wed, 16 Nov 2022 02:54:22 -0500 Subject: [PATCH 4/4] Move `omit` yield to end of generator Fix `omit` generator exiting early after seeing two consecutive non-empty bracket pairs before seeing a magic comma. This does cause a regression with `trailing_comma_optional_parens2` and it still does not fix lines with multiple magic commas. --- 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 a2e41bf5912..fa6aba59a57 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1231,7 +1231,6 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf omit.add(id(closing_bracket)) omit.update(inner_brackets) inner_brackets.clear() - yield omit if ( prev @@ -1246,6 +1245,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf if leaf.value: opening_bracket = leaf.opening_bracket closing_bracket = leaf + yield omit def run_transformer(