Don't remove the single trailing comma from square bracket indexing

Fixes #59
This commit is contained in:
Łukasz Langa 2018-03-22 16:33:50 -07:00
parent c9f8983936
commit a970a205bc
2 changed files with 16 additions and 2 deletions

View File

@ -510,10 +510,16 @@ def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
): ):
return False return False
if closing.type == token.RSQB or closing.type == token.RBRACE: if closing.type == token.RBRACE:
self.leaves.pop() self.leaves.pop()
return True return True
if closing.type == token.RSQB:
comma = self.leaves[-1]
if comma.parent and comma.parent.type == syms.listmaker:
self.leaves.pop()
return True
# For parens let's check if it's safe to remove the comma. If the # For parens let's check if it's safe to remove the comma. If the
# trailing one is the only one, we might mistakenly change a tuple # trailing one is the only one, we might mistakenly change a tuple
# into a different type by removing the comma. # into a different type by removing the comma.

View File

@ -53,6 +53,7 @@
(1, 2, 3) (1, 2, 3)
[] []
[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)] [1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]
[1, 2, 3,]
{i for i in (1, 2, 3)} {i for i in (1, 2, 3)}
{(i ** 2) for i in (1, 2, 3)} {(i ** 2) for i in (1, 2, 3)}
{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))} {(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}
@ -84,7 +85,10 @@
list[str] list[str]
dict[str, int] dict[str, int]
tuple[str, ...] tuple[str, ...]
tuple[str, int, float, dict[str, int]] tuple[str, int, float, dict[str, int],]
very_long_variable_name_filters: t.List[
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
]
slice[0] slice[0]
slice[0:1] slice[0:1]
slice[0:1:2] slice[0:1:2]
@ -207,6 +211,7 @@ async def f():
(1, 2, 3) (1, 2, 3)
[] []
[1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)] [1, 2, 3, 4, 5, 6, 7, 8, 9, (10 or A), (11 or B), (12 or C)]
[1, 2, 3]
{i for i in (1, 2, 3)} {i for i in (1, 2, 3)}
{(i ** 2) for i in (1, 2, 3)} {(i ** 2) for i in (1, 2, 3)}
{(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))} {(i ** 2) for i, _ in ((1, 'a'), (2, 'b'), (3, 'c'))}
@ -248,6 +253,9 @@ async def f():
dict[str, int] dict[str, int]
tuple[str, ...] tuple[str, ...]
tuple[str, int, float, dict[str, int]] tuple[str, int, float, dict[str, int]]
very_long_variable_name_filters: t.List[
t.Tuple[str, t.Union[str, t.List[t.Optional[str]]]],
]
slice[0] slice[0]
slice[0:1] slice[0:1]
slice[0:1:2] slice[0:1:2]