Skip to content

Commit

Permalink
Strip trailing commas in subscripts with -C
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Aug 5, 2022
1 parent 507234c commit 9de9cd2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ def has_magic_trailing_comma(
- it's not a single-element subscript
Additionally, if ensure_removable:
- it's not from square bracket indexing
- specifically, single-element square bracket indexing with
Preview.skip_magic_trailing_comma_in_subscript
"""
if not (
closing.type in CLOSING_BRACKETS
Expand Down Expand Up @@ -301,8 +303,22 @@ def has_magic_trailing_comma(

if not ensure_removable:
return True

comma = self.leaves[-1]
return bool(comma.parent and comma.parent.type == syms.listmaker)
if comma.parent is None:
return False
if Preview.skip_magic_trailing_comma_in_subscript in self.mode:
return bool(
comma.parent.type != syms.subscriptlist
or closing.opening_bracket is None
or not is_one_sequence_between(
closing.opening_bracket,
closing,
self.leaves,
brackets=(token.LSQB, token.RSQB),
)
)
return comma.parent.type == syms.listmaker

if self.is_import:
return True
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Preview(Enum):
remove_block_trailing_newline = auto()
remove_redundant_parens = auto()
string_processing = auto()
skip_magic_trailing_comma_in_subscript = auto()


class Deprecated(UserWarning):
Expand Down
34 changes: 34 additions & 0 deletions tests/data/preview/skip_magic_trailing_comma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# We should not remove the trailing comma in a single-element subscript.
a: tuple[int,]
b = tuple[int,]

# But commas in multiple element subscripts should be removed.
c: tuple[int, int,]
d = tuple[int, int,]

# Remove commas for non-subscripts.
small_list = [1,]
list_of_types = [tuple[int,],]
small_set = {1,}
set_of_types = {tuple[int,],}

# Except single element tuples
small_tuple = (1,)

# output
# We should not remove the trailing comma in a single-element subscript.
a: tuple[int,]
b = tuple[int,]

# But commas in multiple element subscripts should be removed.
c: tuple[int, int]
d = tuple[int, int]

# Remove commas for non-subscripts.
small_list = [1]
list_of_types = [tuple[int,]]
small_set = {1}
set_of_types = {tuple[int,]}

# Except single element tuples
small_tuple = (1,)
5 changes: 5 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def test_preview_docstring_no_string_normalization() -> None:
assert_format(source, expected, mode)


def test_skip_magic_trailing_comma_in_subscript() -> None:
source, expected = read_data("preview", "skip_magic_trailing_comma")
assert_format(source, expected, mode=black.Mode(magic_trailing_comma=False, preview=True))


def test_long_strings_flag_disabled() -> None:
"""Tests for turning off the string processing logic."""
source, expected = read_data("miscellaneous", "long_strings_flag_disabled")
Expand Down

0 comments on commit 9de9cd2

Please sign in to comment.