Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Commit a970a20

Browse files
committed
Don't remove the single trailing comma from square bracket indexing
Fixes pytest-dev#59
1 parent c9f8983 commit a970a20

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

black.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,16 @@ def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
510510
):
511511
return False
512512

513-
if closing.type == token.RSQB or closing.type == token.RBRACE:
513+
if closing.type == token.RBRACE:
514514
self.leaves.pop()
515515
return True
516516

517+
if closing.type == token.RSQB:
518+
comma = self.leaves[-1]
519+
if comma.parent and comma.parent.type == syms.listmaker:
520+
self.leaves.pop()
521+
return True
522+
517523
# For parens let's check if it's safe to remove the comma. If the
518524
# trailing one is the only one, we might mistakenly change a tuple
519525
# into a different type by removing the comma.

tests/expression.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
(1, 2, 3)
5454
[]
5555
[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]
56+
[1, 2, 3,]
5657
{i for i in (1, 2, 3)}
5758
{(i ** 2) for i in (1, 2, 3)}
5859
{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}
@@ -84,7 +85,10 @@
8485
list[str]
8586
dict[str, int]
8687
tuple[str, ...]
87-
tuple[str, int, float, dict[str, int]]
88+
tuple[str, int, float, dict[str, int],]
89+
very_long_variable_name_filters: t.List[
90+
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
91+
]
8892
slice[0]
8993
slice[0:1]
9094
slice[0:1:2]
@@ -207,6 +211,7 @@ async def f():
207211
(1, 2, 3)
208212
[]
209213
[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]
214+
[1, 2, 3]
210215
{i for i in (1, 2, 3)}
211216
{(i ** 2) for i in (1, 2, 3)}
212217
{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}
@@ -248,6 +253,9 @@ async def f():
248253
dict[str, int]
249254
tuple[str, ...]
250255
tuple[str, int, float, dict[str, int]]
256+
very_long_variable_name_filters: t.List[
257+
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
258+
]
251259
slice[0]
252260
slice[0:1]
253261
slice[0:1:2]

0 commit comments

Comments
 (0)