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

Do not add an extra blank line to an import line that has fmt disabled (#3610)
authorYilei "Dolee" Yang <yileiyang@google.com>
Sat, 18 Mar 2023 04:39:21 +0000 (21:39 -0700)
committerGitHub <noreply@github.com>
Sat, 18 Mar 2023 04:39:21 +0000 (21:39 -0700)
CHANGES.md
src/black/comments.py
src/black/lines.py
src/blib2to3/pytree.py
tests/data/simple_cases/fmtonoff.py
tests/data/simple_cases/fmtpass_imports.py [new file with mode: 0644]

index eff2640a01e9f3191389b3f7d3ae5a99e7571451..06a0ab7e9eb5df143f2d24d350c66d64d0780697 100644 (file)
@@ -10,6 +10,9 @@
 
 <!-- Changes that affect Black's stable style -->
 
 
 <!-- Changes that affect Black's stable style -->
 
+- Import lines with `# fmt: skip` and `# fmt: off` no longer have an extra blank line
+  added when they are right after another import line (#3610)
+
 ### Preview style
 
 <!-- Changes that affect Black's preview style -->
 ### Preview style
 
 <!-- Changes that affect Black's preview style -->
index 7cf15bf67b3ff6ecb7df0e7a0221cbff48411ac8..619123ab4be883703d9792521458f1440979186c 100644 (file)
@@ -203,6 +203,7 @@ def convert_one_fmt_off_pair(node: Node) -> bool:
                     STANDALONE_COMMENT,
                     hidden_value,
                     prefix=standalone_comment_prefix,
                     STANDALONE_COMMENT,
                     hidden_value,
                     prefix=standalone_comment_prefix,
+                    fmt_pass_converted_first_leaf=first_leaf_of(first),
                 ),
             )
             return True
                 ),
             )
             return True
index 329dfc4f0d3d7de0dbe545656c68d020628a9a5a..66bba14b357166eb984dcba459689aaf3eb87142 100644 (file)
@@ -195,6 +195,26 @@ class Line:
             return False
         return self.leaves[-1].type == token.COLON
 
             return False
         return self.leaves[-1].type == token.COLON
 
+    def is_fmt_pass_converted(
+        self, *, first_leaf_matches: Optional[Callable[[Leaf], bool]] = None
+    ) -> bool:
+        """Is this line converted from fmt off/skip code?
+
+        If first_leaf_matches is not None, it only returns True if the first
+        leaf of converted code matches.
+        """
+        if len(self.leaves) != 1:
+            return False
+        leaf = self.leaves[0]
+        if (
+            leaf.type != STANDALONE_COMMENT
+            or leaf.fmt_pass_converted_first_leaf is None
+        ):
+            return False
+        return first_leaf_matches is None or first_leaf_matches(
+            leaf.fmt_pass_converted_first_leaf
+        )
+
     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
         """If so, needs to be split before emitting."""
         for leaf in self.leaves:
     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
         """If so, needs to be split before emitting."""
         for leaf in self.leaves:
@@ -597,6 +617,7 @@ class EmptyLineTracker:
             self.previous_line
             and self.previous_line.is_import
             and not current_line.is_import
             self.previous_line
             and self.previous_line.is_import
             and not current_line.is_import
+            and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import)
             and depth == self.previous_line.depth
         ):
             return (before or 1), 0
             and depth == self.previous_line.depth
         ):
             return (before or 1), 0
index 15a1420ef7d6f3c3c6054bfbbbf90040d9aa6f4b..ea60c894e20490fd37f028f6e38e08be767ce761 100644 (file)
@@ -392,6 +392,10 @@ class Leaf(Base):
     _prefix = ""  # Whitespace and comments preceding this token in the input
     lineno: int = 0  # Line where this token starts in the input
     column: int = 0  # Column where this token starts in the input
     _prefix = ""  # Whitespace and comments preceding this token in the input
     lineno: int = 0  # Line where this token starts in the input
     column: int = 0  # Column where this token starts in the input
+    # If not None, this Leaf is created by converting a block of fmt off/skip
+    # code, and `fmt_pass_converted_first_leaf` points to the first Leaf in the
+    # converted code.
+    fmt_pass_converted_first_leaf: Optional["Leaf"] = None
 
     def __init__(
         self,
 
     def __init__(
         self,
@@ -401,6 +405,7 @@ class Leaf(Base):
         prefix: Optional[Text] = None,
         fixers_applied: List[Any] = [],
         opening_bracket: Optional["Leaf"] = None,
         prefix: Optional[Text] = None,
         fixers_applied: List[Any] = [],
         opening_bracket: Optional["Leaf"] = None,
+        fmt_pass_converted_first_leaf: Optional["Leaf"] = None,
     ) -> None:
         """
         Initializer.
     ) -> None:
         """
         Initializer.
@@ -419,6 +424,7 @@ class Leaf(Base):
         self.fixers_applied: Optional[List[Any]] = fixers_applied[:]
         self.children = []
         self.opening_bracket = opening_bracket
         self.fixers_applied: Optional[List[Any]] = fixers_applied[:]
         self.children = []
         self.opening_bracket = opening_bracket
+        self.fmt_pass_converted_first_leaf = fmt_pass_converted_first_leaf
 
     def __repr__(self) -> str:
         """Return a canonical string representation."""
 
     def __repr__(self) -> str:
         """Return a canonical string representation."""
index e40ea2c8d210c76e0ae7f608309a0a850ecaffdd..d1f15cd5c8b682c72de85e0a2e0b8d877ffe8730 100644 (file)
@@ -195,7 +195,6 @@ import sys
 from third_party import X, Y, Z
 
 from library import some_connection, some_decorator
 from third_party import X, Y, Z
 
 from library import some_connection, some_decorator
-
 # fmt: off
 from third_party import (X,
                          Y, Z)
 # fmt: off
 from third_party import (X,
                          Y, Z)
diff --git a/tests/data/simple_cases/fmtpass_imports.py b/tests/data/simple_cases/fmtpass_imports.py
new file mode 100644 (file)
index 0000000..8b3c0bc
--- /dev/null
@@ -0,0 +1,19 @@
+# Regression test for https://github.com/psf/black/issues/3438
+
+import ast
+import collections  # fmt: skip
+import dataclasses
+# fmt: off
+import os
+# fmt: on
+import pathlib
+
+import re  # fmt: skip
+import secrets
+
+# fmt: off
+import sys
+# fmt: on
+
+import tempfile
+import zoneinfo