From 53f6b8349a56e43ee2f84115efac10ceae625406 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Thu, 9 Nov 2023 23:20:28 +0200 Subject: [PATCH] Format one-tuples and one-subscripts as multi-line if already multi-line Fix #3918. --- src/black/lines.py | 13 +++++-- tests/data/cases/collections.py | 36 +++++++++++++++++++ tests/data/cases/one_element_subscript.py | 22 ++++++++++-- tests/data/cases/skip_magic_trailing_comma.py | 12 +++++++ 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index 20f22acf93a..995c3da82f6 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -337,6 +337,9 @@ def has_magic_trailing_comma( - there's a trailing comma here - it's not a one-tuple - it's not a single-element subscript + Additionally, if not ensure_removable: + - it's a one-tuple or single-element subscript that is already + split to multiple lines """ if not ( closing.type in CLOSING_BRACKETS @@ -360,7 +363,10 @@ def has_magic_trailing_comma( brackets=(token.LSQB, token.RSQB), ) ): - return False + return ( + closing.opening_bracket.lineno != closing.lineno + and not ensure_removable + ) return True @@ -374,7 +380,10 @@ def has_magic_trailing_comma( self.leaves, brackets=(token.LPAR, token.RPAR), ): - return False + return ( + closing.opening_bracket.lineno != closing.lineno + and not ensure_removable + ) return True diff --git a/tests/data/cases/collections.py b/tests/data/cases/collections.py index 68431665211..e8dc8b09225 100644 --- a/tests/data/cases/collections.py +++ b/tests/data/cases/collections.py @@ -37,6 +37,9 @@ ['ls', 'lsoneple/%s' % (foo,)] x = {"oneple": (1,)} y = {"oneple": (1,),} +z = {"oneple": ( + 1, +)} assert False, ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s" % bar) # looping over a 1-tuple should also not get wrapped @@ -45,6 +48,18 @@ for (x,) in (1,), (2,), (3,): pass +(1,) +( + 1, +) +[1,] +[ + 1, +] +{1,} +{ + 1, +} [1, 2, 3,] division_result_tuple = (6/2,) @@ -127,6 +142,11 @@ y = { "oneple": (1,), } +z = { + "oneple": ( + 1, + ) +} assert False, ( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s" % bar @@ -138,6 +158,22 @@ for (x,) in (1,), (2,), (3,): pass +(1,) +( + 1, +) +[ + 1, +] +[ + 1, +] +{ + 1, +} +{ + 1, +} [ 1, 2, diff --git a/tests/data/cases/one_element_subscript.py b/tests/data/cases/one_element_subscript.py index 39205ba9f7a..4fe8a38018e 100644 --- a/tests/data/cases/one_element_subscript.py +++ b/tests/data/cases/one_element_subscript.py @@ -1,8 +1,17 @@ # We should not treat the trailing comma -# in a single-element subscript. +# in a single-line single-element subscript. a: tuple[int,] b = tuple[int,] +# Trailing comma should be preserved on multi-line +# single-element subscript. +a: tuple[ + int, +] +b = tuple[ + int, +] + # The magic comma still applies to multi-element subscripts. c: tuple[int, int,] d = tuple[int, int,] @@ -13,10 +22,19 @@ # output # We should not treat the trailing comma -# in a single-element subscript. +# in a single-line single-element subscript. a: tuple[int,] b = tuple[int,] +# Trailing comma should be preserved on multi-line +# single-element subscript. +a: tuple[ + int, +] +b = tuple[ + int, +] + # The magic comma still applies to multi-element subscripts. c: tuple[ int, diff --git a/tests/data/cases/skip_magic_trailing_comma.py b/tests/data/cases/skip_magic_trailing_comma.py index 4dda5df40f0..c9cfde9512a 100644 --- a/tests/data/cases/skip_magic_trailing_comma.py +++ b/tests/data/cases/skip_magic_trailing_comma.py @@ -1,7 +1,13 @@ # flags: --skip-magic-trailing-comma # We should not remove the trailing comma in a single-element subscript. a: tuple[int,] +aa: tuple[ + int, +] b = tuple[int,] +bb = tuple[ + int, +] # But commas in multiple element subscripts should be removed. c: tuple[int, int,] @@ -15,6 +21,9 @@ # Except single element tuples small_tuple = (1,) +small_multiline_tuple = ( + 1, +) # Trailing commas in multiple chained non-nested parens. zero( @@ -50,7 +59,9 @@ # output # We should not remove the trailing comma in a single-element subscript. a: tuple[int,] +aa: tuple[int,] b = tuple[int,] +bb = tuple[int,] # But commas in multiple element subscripts should be removed. c: tuple[int, int] @@ -64,6 +75,7 @@ # Except single element tuples small_tuple = (1,) +small_multiline_tuple = (1,) # Trailing commas in multiple chained non-nested parens. zero(one).two(three).four(five)