this is invalid. This was a bug introduced in version 22.6.0. (#3166)
- `--skip-string-normalization` / `-S` now prevents docstring prefixes from being
normalized as expected (#3168)
+- When using `--skip-magic-trailing-comma` or `-C`, trailing commas are stripped from
+ subscript expressions with more than 1 element (#3209)
### _Blackd_
- 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
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 (
+ 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
remove_block_trailing_newline = auto()
remove_redundant_parens = auto()
string_processing = auto()
+ skip_magic_trailing_comma_in_subscript = auto()
class Deprecated(UserWarning):
--- /dev/null
+# 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,)
@pytest.mark.parametrize("filename", all_data_cases("preview"))
def test_preview_format(filename: str) -> None:
- check_file("preview", filename, black.Mode(preview=True))
+ magic_trailing_comma = filename != "skip_magic_trailing_comma"
+ check_file(
+ "preview",
+ filename,
+ black.Mode(preview=True, magic_trailing_comma=magic_trailing_comma),
+ )
@pytest.mark.parametrize("filename", all_data_cases("preview_39"))