Skip to content

Commit

Permalink
Format one-tuples and one-subscripts as multi-line if already multi-line
Browse files Browse the repository at this point in the history
  • Loading branch information
bluetech committed Nov 10, 2023
1 parent d80e905 commit 93578f1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

<!-- Changes that affect Black's stable style -->

- Magic trailing commas are now respected in one-tuples and single-element
subscripts when they are already split to multiple lines. So a multiline
one-tuple is no longer collapsed to a single line when magic trailing
commas are enabled. (#4038)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
26 changes: 20 additions & 6 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,18 @@ def contains_multiline_strings(self) -> bool:
def has_magic_trailing_comma(
self, closing: Leaf, ensure_removable: bool = False
) -> bool:
"""Return True if we have a magic trailing comma, that is when:
- there's a trailing comma here
- it's not a one-tuple
- it's not a single-element subscript
"""Return whether we have a magic trailing comma.
This is when:
* there's a trailing comma here
* it's not a one-tuple
* it's not a single-element subscript
Additionally, if ``ensure_removable`` is false:
* it's a one-tuple or single-element subscript that is already split to
multiple lines
"""
if not (
closing.type in CLOSING_BRACKETS
Expand All @@ -360,7 +368,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

Expand All @@ -374,7 +385,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

Expand Down
36 changes: 36 additions & 0 deletions tests/data/cases/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,)
Expand Down Expand Up @@ -127,6 +142,11 @@
y = {
"oneple": (1,),
}
z = {
"oneple": (
1,
)
}
assert False, (
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa wraps %s"
% bar
Expand All @@ -138,6 +158,22 @@
for (x,) in (1,), (2,), (3,):
pass

(1,)
(
1,
)
[
1,
]
[
1,
]
{
1,
}
{
1,
}
[
1,
2,
Expand Down
22 changes: 20 additions & 2 deletions tests/data/cases/one_element_subscript.py
Original file line number Diff line number Diff line change
@@ -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,]
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/data/cases/skip_magic_trailing_comma.py
Original file line number Diff line number Diff line change
@@ -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,]
Expand All @@ -15,6 +21,9 @@

# Except single element tuples
small_tuple = (1,)
small_multiline_tuple = (
1,
)

# Trailing commas in multiple chained non-nested parens.
zero(
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down

0 comments on commit 93578f1

Please sign in to comment.