]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Strip trailing commas in subscripts with -C (#3209)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Sat, 13 Aug 2022 13:41:34 +0000 (06:41 -0700)
committerGitHub <noreply@github.com>
Sat, 13 Aug 2022 13:41:34 +0000 (06:41 -0700)
Fixes #2296, #3204

CHANGES.md
src/black/lines.py
src/black/mode.py
tests/data/preview/skip_magic_trailing_comma.py [new file with mode: 0644]
tests/test_format.py

index 79f4ce591872ff2802302a56f775efb4afdd6b3f..fb7a2723b67f569fe6801b5d030fb1d3b4ebb613 100644 (file)
@@ -24,6 +24,8 @@
   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)
   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_
 
 
 ### _Blackd_
 
index 1ebc7808901d03335b48bf8d5e8fe0bb12d729a3..30622650d53cd631cdcb800ce8e2a546b0de6840 100644 (file)
@@ -273,6 +273,8 @@ class Line:
         - it's not a single-element subscript
         Additionally, if ensure_removable:
         - it's not from square bracket indexing
         - 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 (
             closing.type in CLOSING_BRACKETS
@@ -301,8 +303,22 @@ class Line:
 
             if not ensure_removable:
                 return True
 
             if not ensure_removable:
                 return True
+
             comma = self.leaves[-1]
             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
 
         if self.is_import:
             return True
index f6d0cbf62bd46b579fec3520618e6c1c8b117ceb..6c0847e8bccc50f9305c2c12083d616a45d4434a 100644 (file)
@@ -156,6 +156,7 @@ class Preview(Enum):
     remove_block_trailing_newline = auto()
     remove_redundant_parens = auto()
     string_processing = auto()
     remove_block_trailing_newline = auto()
     remove_redundant_parens = auto()
     string_processing = auto()
+    skip_magic_trailing_comma_in_subscript = auto()
 
 
 class Deprecated(UserWarning):
 
 
 class Deprecated(UserWarning):
diff --git a/tests/data/preview/skip_magic_trailing_comma.py b/tests/data/preview/skip_magic_trailing_comma.py
new file mode 100644 (file)
index 0000000..e98174a
--- /dev/null
@@ -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,)
index 3645934721f0248278d08e14c6386cbd2bcde86d..01cd61eef63dee3272e156d5e5700ad9f4152eda 100644 (file)
@@ -36,7 +36,12 @@ def test_simple_format(filename: str) -> None:
 
 @pytest.mark.parametrize("filename", all_data_cases("preview"))
 def test_preview_format(filename: str) -> None:
 
 @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"))
 
 
 @pytest.mark.parametrize("filename", all_data_cases("preview_39"))