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

Fix trailing comma for function with one arg (#880) (#891)
authordylanjblack <38996120+dylanjblack@users.noreply.github.com>
Sat, 15 Jun 2019 04:49:49 +0000 (14:49 +1000)
committerJelle Zijlstra <jelle.zijlstra@gmail.com>
Sat, 15 Jun 2019 04:49:49 +0000 (21:49 -0700)
Modified maybe_remove_trailing_comma to remove trailing commas for
typedarglists (in addition to arglists), and updated line split logic
to ensure that all lines in a function definition that contain only one
arg have a trailing comma.

black.py
tests/data/function_trailing_comma.py [new file with mode: 0644]
tests/test_black.py

index 635eba207cd0c2a695cab27d68326216a90325f5..8318674a722f41fa70e4dfe95febe839dd93c87e 100644 (file)
--- a/black.py
+++ b/black.py
@@ -1352,7 +1352,10 @@ class Line:
             bracket_depth = leaf.bracket_depth
             if bracket_depth == depth and leaf.type == token.COMMA:
                 commas += 1
-                if leaf.parent and leaf.parent.type == syms.arglist:
+                if leaf.parent and leaf.parent.type in {
+                    syms.arglist,
+                    syms.typedargslist,
+                }:
                     commas += 1
                     break
 
@@ -2488,9 +2491,13 @@ def bracket_split_build_line(
         if leaves:
             # Since body is a new indent level, remove spurious leading whitespace.
             normalize_prefix(leaves[0], inside_brackets=True)
-            # Ensure a trailing comma for imports, but be careful not to add one after
-            # any comments.
-            if original.is_import:
+            # Ensure a trailing comma for imports and standalone function arguments, but
+            # be careful not to add one after any comments.
+            no_commas = original.is_def and not any(
+                l.type == token.COMMA for l in leaves
+            )
+
+            if original.is_import or no_commas:
                 for i in range(len(leaves) - 1, -1, -1):
                     if leaves[i].type == STANDALONE_COMMENT:
                         continue
diff --git a/tests/data/function_trailing_comma.py b/tests/data/function_trailing_comma.py
new file mode 100644 (file)
index 0000000..29fd99b
--- /dev/null
@@ -0,0 +1,14 @@
+def f(a,):
+    ...
+
+def f(a:int=1,):
+    ...
+
+# output
+
+def f(a):
+    ...
+
+
+def f(a: int = 1):
+    ...
index 88c03d05fb83e8e364b0454d30d9e48bb110517f..828b3e45bf9a4b73af5ce065f51cf158b79be6a2 100644 (file)
@@ -264,6 +264,14 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, black.FileMode())
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_function_trailing_comma(self) -> None:
+        source, expected = read_data("function_trailing_comma")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, black.FileMode())
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_expression(self) -> None:
         source, expected = read_data("expression")