]> 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:

Avoid magic-trailing-comma in single-element subscripts (#2942)
authorJoe Young <80432516+jpy-git@users.noreply.github.com>
Thu, 24 Mar 2022 02:16:09 +0000 (02:16 +0000)
committerGitHub <noreply@github.com>
Thu, 24 Mar 2022 02:16:09 +0000 (19:16 -0700)
Closes #2918.

CHANGES.md
src/black/linegen.py
src/black/lines.py
src/black/mode.py
src/black/nodes.py
tests/data/one_element_subscript.py [new file with mode: 0644]
tests/test_format.py

index d0faf7cecb9af5455e5910253ca888b3f747f299..d753a24ff772e2dad8f97e44b9a1249b1f01f562 100644 (file)
@@ -15,6 +15,7 @@
 <!-- Changes that affect Black's preview style -->
 
 - Code cell separators `#%%` are now standardised to `# %%` (#2919)
 <!-- Changes that affect Black's preview style -->
 
 - Code cell separators `#%%` are now standardised to `# %%` (#2919)
+- Avoid magic-trailing-comma in single-element subscripts (#2942)
 
 ### _Blackd_
 
 
 ### _Blackd_
 
index 79475a83f0e363c5f9f223278c5a3ebdf5f7fad6..5d92011da9ab1c5de432cc3a1f68421b1dd68cf1 100644 (file)
@@ -8,7 +8,12 @@ from typing import Collection, Iterator, List, Optional, Set, Union
 from black.nodes import WHITESPACE, RARROW, STATEMENT, STANDALONE_COMMENT
 from black.nodes import ASSIGNMENTS, OPENING_BRACKETS, CLOSING_BRACKETS
 from black.nodes import Visitor, syms, is_arith_like, ensure_visible
 from black.nodes import WHITESPACE, RARROW, STATEMENT, STANDALONE_COMMENT
 from black.nodes import ASSIGNMENTS, OPENING_BRACKETS, CLOSING_BRACKETS
 from black.nodes import Visitor, syms, is_arith_like, ensure_visible
-from black.nodes import is_docstring, is_empty_tuple, is_one_tuple, is_one_tuple_between
+from black.nodes import (
+    is_docstring,
+    is_empty_tuple,
+    is_one_tuple,
+    is_one_sequence_between,
+)
 from black.nodes import is_name_token, is_lpar_token, is_rpar_token
 from black.nodes import is_walrus_assignment, is_yield, is_vararg, is_multiline_string
 from black.nodes import is_stub_suite, is_stub_body, is_atom_with_invisible_parens
 from black.nodes import is_name_token, is_lpar_token, is_rpar_token
 from black.nodes import is_walrus_assignment, is_yield, is_vararg, is_multiline_string
 from black.nodes import is_stub_suite, is_stub_body, is_atom_with_invisible_parens
@@ -973,7 +978,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
                     prev
                     and prev.type == token.COMMA
                     and leaf.opening_bracket is not None
                     prev
                     and prev.type == token.COMMA
                     and leaf.opening_bracket is not None
-                    and not is_one_tuple_between(
+                    and not is_one_sequence_between(
                         leaf.opening_bracket, leaf, line.leaves
                     )
                 ):
                         leaf.opening_bracket, leaf, line.leaves
                     )
                 ):
@@ -1001,7 +1006,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
                 prev
                 and prev.type == token.COMMA
                 and leaf.opening_bracket is not None
                 prev
                 and prev.type == token.COMMA
                 and leaf.opening_bracket is not None
-                and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves)
+                and not is_one_sequence_between(leaf.opening_bracket, leaf, line.leaves)
             ):
                 # Never omit bracket pairs with trailing commas.
                 # We need to explode on those.
             ):
                 # Never omit bracket pairs with trailing commas.
                 # We need to explode on those.
index f35665c8e0c5afa0eba3d0385c4153609e13c8f6..e455a5075392e6405b6c2472c371bbce36a7d1d5 100644 (file)
@@ -17,12 +17,12 @@ from blib2to3.pytree import Node, Leaf
 from blib2to3.pgen2 import token
 
 from black.brackets import BracketTracker, DOT_PRIORITY
 from blib2to3.pgen2 import token
 
 from black.brackets import BracketTracker, DOT_PRIORITY
-from black.mode import Mode
+from black.mode import Mode, Preview
 from black.nodes import STANDALONE_COMMENT, TEST_DESCENDANTS
 from black.nodes import BRACKETS, OPENING_BRACKETS, CLOSING_BRACKETS
 from black.nodes import syms, whitespace, replace_child, child_towards
 from black.nodes import is_multiline_string, is_import, is_type_comment
 from black.nodes import STANDALONE_COMMENT, TEST_DESCENDANTS
 from black.nodes import BRACKETS, OPENING_BRACKETS, CLOSING_BRACKETS
 from black.nodes import syms, whitespace, replace_child, child_towards
 from black.nodes import is_multiline_string, is_import, is_type_comment
-from black.nodes import is_one_tuple_between
+from black.nodes import is_one_sequence_between
 
 # types
 T = TypeVar("T")
 
 # types
 T = TypeVar("T")
@@ -254,6 +254,7 @@ class Line:
         """Return True if we have a magic trailing comma, that is when:
         - there's a trailing comma here
         - it's not a one-tuple
         """Return True if we have a magic trailing comma, that is when:
         - there's a trailing comma here
         - it's not a one-tuple
+        - it's not a single-element subscript
         Additionally, if ensure_removable:
         - it's not from square bracket indexing
         """
         Additionally, if ensure_removable:
         - it's not from square bracket indexing
         """
@@ -268,6 +269,20 @@ class Line:
             return True
 
         if closing.type == token.RSQB:
             return True
 
         if closing.type == token.RSQB:
+            if (
+                Preview.one_element_subscript in self.mode
+                and closing.parent
+                and closing.parent.type == syms.trailer
+                and closing.opening_bracket
+                and is_one_sequence_between(
+                    closing.opening_bracket,
+                    closing,
+                    self.leaves,
+                    brackets=(token.LSQB, token.RSQB),
+                )
+            ):
+                return False
+
             if not ensure_removable:
                 return True
             comma = self.leaves[-1]
             if not ensure_removable:
                 return True
             comma = self.leaves[-1]
@@ -276,7 +291,7 @@ class Line:
         if self.is_import:
             return True
 
         if self.is_import:
             return True
 
-        if closing.opening_bracket is not None and not is_one_tuple_between(
+        if closing.opening_bracket is not None and not is_one_sequence_between(
             closing.opening_bracket, closing, self.leaves
         ):
             return True
             closing.opening_bracket, closing, self.leaves
         ):
             return True
index 35a072c23e07bf9a493b77591a7a986d5e1e7bb8..77b1cabfcbcd87895427f186fbecd453eb768ece 100644 (file)
@@ -127,6 +127,7 @@ class Preview(Enum):
     """Individual preview style features."""
 
     string_processing = auto()
     """Individual preview style features."""
 
     string_processing = auto()
+    one_element_subscript = auto()
 
 
 class Deprecated(UserWarning):
 
 
 class Deprecated(UserWarning):
@@ -162,9 +163,7 @@ class Mode:
         """
         if feature is Preview.string_processing:
             return self.preview or self.experimental_string_processing
         """
         if feature is Preview.string_processing:
             return self.preview or self.experimental_string_processing
-        # TODO: Remove type ignore comment once preview contains more features
-        #  than just ESP
-        return self.preview  # type: ignore
+        return self.preview
 
     def get_cache_key(self) -> str:
         if self.target_versions:
 
     def get_cache_key(self) -> str:
         if self.target_versions:
index f130bff990e1622c3555f65262f12c0e7d65ff2d..d18d4bde8724ab669a27d54e9cef6cdc64699fa0 100644 (file)
@@ -9,6 +9,7 @@ from typing import (
     List,
     Optional,
     Set,
     List,
     Optional,
     Set,
+    Tuple,
     TypeVar,
     Union,
 )
     TypeVar,
     Union,
 )
@@ -559,9 +560,14 @@ def is_one_tuple(node: LN) -> bool:
     )
 
 
     )
 
 
-def is_one_tuple_between(opening: Leaf, closing: Leaf, leaves: List[Leaf]) -> bool:
-    """Return True if content between `opening` and `closing` looks like a one-tuple."""
-    if opening.type != token.LPAR and closing.type != token.RPAR:
+def is_one_sequence_between(
+    opening: Leaf,
+    closing: Leaf,
+    leaves: List[Leaf],
+    brackets: Tuple[int, int] = (token.LPAR, token.RPAR),
+) -> bool:
+    """Return True if content between `opening` and `closing` is a one-sequence."""
+    if (opening.type, closing.type) != brackets:
         return False
 
     depth = closing.bracket_depth + 1
         return False
 
     depth = closing.bracket_depth + 1
diff --git a/tests/data/one_element_subscript.py b/tests/data/one_element_subscript.py
new file mode 100644 (file)
index 0000000..39205ba
--- /dev/null
@@ -0,0 +1,36 @@
+# We should not treat the trailing comma
+# in a single-element subscript.
+a: tuple[int,]
+b = tuple[int,]
+
+# The magic comma still applies to multi-element subscripts.
+c: tuple[int, int,]
+d = tuple[int, int,]
+
+# Magic commas still work as expected for non-subscripts.
+small_list = [1,]
+list_of_types = [tuple[int,],]
+
+# output
+# We should not treat the trailing comma
+# in a single-element subscript.
+a: tuple[int,]
+b = tuple[int,]
+
+# The magic comma still applies to multi-element subscripts.
+c: tuple[
+    int,
+    int,
+]
+d = tuple[
+    int,
+    int,
+]
+
+# Magic commas still work as expected for non-subscripts.
+small_list = [
+    1,
+]
+list_of_types = [
+    tuple[int,],
+]
index 667d5c110fad03ba775fcd0f29736440da62f71d..4de3170026835b181958f217518ac6116514ee46 100644 (file)
@@ -80,6 +80,7 @@ PREVIEW_CASES: List[str] = [
     "long_strings__edge_case",
     "long_strings__regression",
     "percent_precedence",
     "long_strings__edge_case",
     "long_strings__regression",
     "percent_precedence",
+    "one_element_subscript",
 ]
 
 SOURCES: List[str] = [
 ]
 
 SOURCES: List[str] = [