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

Class new line between docstrings / vars / methods (#219)
authorLuka Sterbic <luka.sterbic@gmail.com>
Tue, 29 May 2018 06:48:59 +0000 (08:48 +0200)
committerŁukasz Langa <lukasz@langa.pl>
Tue, 29 May 2018 06:48:59 +0000 (23:48 -0700)
Partially addresses #144

black.py
tests/class_blank_parentheses.py
tests/class_methods_new_line.py [new file with mode: 0644]
tests/comments4.py
tests/composition.py
tests/test_black.py

index 05ec00c8f25944756f8f2f21e66df0a8d2568d8b..7dc6ef86c1d4b12a4a46bc2c1cd6fe080a12af01 100644 (file)
--- a/black.py
+++ b/black.py
@@ -987,6 +987,18 @@ class Line:
             and self.leaves[3].value == ")"
         )
 
+    @property
+    def is_triple_quoted_string(self) -> bool:
+        """Is the line a triple quoted docstring?"""
+        return (
+            bool(self)
+            and self.leaves[0].type == token.STRING
+            and (
+                self.leaves[0].value.startswith('"""')
+                or self.leaves[0].value.startswith("'''")
+            )
+        )
+
     def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
         """If so, needs to be split before emitting."""
         for leaf in self.leaves:
@@ -1194,6 +1206,7 @@ class EmptyLineTracker:
     the prefix of the first leaf consists of optional newlines.  Those newlines
     are consumed by `maybe_empty_lines()` and included in the computation.
     """
+
     is_pyi: bool = False
     previous_line: Optional[Line] = None
     previous_after: int = 0
@@ -1244,6 +1257,12 @@ class EmptyLineTracker:
             if self.previous_line.is_decorator:
                 return 0, 0
 
+            if (
+                self.previous_line.is_class
+                and self.previous_line.depth != current_line.depth
+            ):
+                return 0, 0
+
             if (
                 self.previous_line.is_comment
                 and self.previous_line.depth == current_line.depth
@@ -1275,6 +1294,13 @@ class EmptyLineTracker:
         ):
             return (before or 1), 0
 
+        if (
+            self.previous_line
+            and self.previous_line.is_class
+            and current_line.is_triple_quoted_string
+        ):
+            return before, 1
+
         return before, 0
 
 
@@ -1285,6 +1311,7 @@ class LineGenerator(Visitor[Line]):
     Note: destroys the tree it's visiting by mutating prefixes of its leaves
     in ways that will no longer stringify to valid Python code on the tree.
     """
+
     is_pyi: bool = False
     current_line: Line = Factory(Line)
     remove_u_prefix: bool = False
@@ -2768,6 +2795,7 @@ def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
 @dataclass
 class Report:
     """Provides a reformatting counter. Can be rendered with `str(report)`."""
+
     check: bool = False
     quiet: bool = False
     change_count: int = 0
index bf19a269667f7a125e9d7a568a61f58c7bdb06d3..d586cacdf31cb93fb862b93fc25fc5a08fbc5ef6 100644 (file)
@@ -39,7 +39,6 @@ class ClassWithSpaceParentheses:
 
 
 class ClassWithEmptyFunc(object):
-
     def func_with_blank_parentheses():
         return 5
 
@@ -55,7 +54,6 @@ def class_under_the_func_with_blank_parentheses():
 
 
 class NormalClass:
-
     def func_for_testing(self, first, second):
         sum = first + second
         return sum
diff --git a/tests/class_methods_new_line.py b/tests/class_methods_new_line.py
new file mode 100644 (file)
index 0000000..b098271
--- /dev/null
@@ -0,0 +1,42 @@
+class ClassSimplest:
+    pass
+class ClassWithInit:
+    def __init__(self):
+        pass
+class ClassWithInitAndVars:
+    cls_var = 100
+    def __init__(self):
+        pass
+class ClassWithInitAndVarsAndDocstring:
+    """Test class"""
+    cls_var = 100
+    def __init__(self):
+        pass
+
+
+# output
+
+
+class ClassSimplest:
+    pass
+
+
+class ClassWithInit:
+    def __init__(self):
+        pass
+
+
+class ClassWithInitAndVars:
+    cls_var = 100
+
+    def __init__(self):
+        pass
+
+
+class ClassWithInitAndVarsAndDocstring:
+    """Test class"""
+
+    cls_var = 100
+
+    def __init__(self):
+        pass
index 944714df7bfc97da12028b72e10703dd42b6bdf2..013684cac43238b5e307f6b5392a9f068e977684 100644 (file)
@@ -1,5 +1,4 @@
 class C:
-
     @pytest.mark.parametrize(
         ("post_data", "message"),
         [
index f8cd5edb10677a8aa92b0c70ca3d4ff65a5ade8a..ac63e46751a466260ce819ec0f1c05817fa88d34 100644 (file)
@@ -1,5 +1,4 @@
 class C:
-
     def test(self) -> None:
         with patch("black.out", print):
             self.assertEqual(
index a7d04cc548f310583793b5656913dd71604ac332..f5114a7c0c3c54d1b5259b5d349514fd5bceee17 100644 (file)
@@ -363,6 +363,14 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, line_length=ll)
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_new_line_between_class_and_code(self) -> None:
+        source, expected = read_data("class_methods_new_line")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, line_length=ll)
+
     def test_report(self) -> None:
         report = black.Report()
         out_lines = []