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

Discover whether a file is Python 3.6+ also by stars in calls
authorŁukasz Langa <lukasz@langa.pl>
Tue, 8 May 2018 22:44:44 +0000 (15:44 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Wed, 9 May 2018 04:57:09 +0000 (21:57 -0700)
Fixes a pathological situation where if a function signature used a trailing
comma but was later reformatted to a single line (with the trailing comma
removed), Black would change its mind whether a file is Python
3.6-compatible between runs.

README.md
black.py
tests/function.py
tests/function2.py [new file with mode: 0644]
tests/test_black.py

index a9ec9eab86167fd7f4ce39cc4076bf456ec023ca..8c5bbd2928987873f9badc264dbb07293a294a22 100644 (file)
--- a/README.md
+++ b/README.md
@@ -549,6 +549,11 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 
 * fixed not splitting long from-imports with only a single name
 
 
 * fixed not splitting long from-imports with only a single name
 
+* fixed Python 3.6+ file discovery by also looking at function calls with
+  unpacking. This fixed non-deterministic formatting if trailing commas
+  where used both in function signatures with stars and function calls
+  with stars but the former would be reformatted to a single line.
+
 
 ### 18.4a4
 
 
 ### 18.4a4
 
index d2d23c879420d24e945e95fbccd1e578edf436a8..151dc8cccfe30efd38a1555e83aaaa9a90eadb48 100644 (file)
--- a/black.py
+++ b/black.py
@@ -2337,7 +2337,7 @@ def is_python36(node: Node) -> bool:
 
     Currently looking for:
     - f-strings; and
 
     Currently looking for:
     - f-strings; and
-    - trailing commas after * or ** in function signatures.
+    - trailing commas after * or ** in function signatures and calls.
     """
     for n in node.pre_order():
         if n.type == token.STRING:
     """
     for n in node.pre_order():
         if n.type == token.STRING:
@@ -2346,7 +2346,7 @@ def is_python36(node: Node) -> bool:
                 return True
 
         elif (
                 return True
 
         elif (
-            n.type == syms.typedargslist
+            n.type in {syms.typedargslist, syms.arglist}
             and n.children
             and n.children[-1].type == token.COMMA
         ):
             and n.children
             and n.children[-1].type == token.COMMA
         ):
@@ -2354,6 +2354,11 @@ def is_python36(node: Node) -> bool:
                 if ch.type in STARS:
                     return True
 
                 if ch.type in STARS:
                     return True
 
+                if ch.type == syms.argument:
+                    for argch in ch.children:
+                        if argch.type in STARS:
+                            return True
+
     return False
 
 
     return False
 
 
index 4ec90571298f60abc862df0ee68c57b52a3be7b7..a18121219f58a4da1dc5533e9693467378d83521 100644 (file)
@@ -81,6 +81,15 @@ def trailing_comma():
     C: 0.1 * (10.0 / 12),
     D: 0.1 * (10.0 / 12),
 }
     C: 0.1 * (10.0 / 12),
     D: 0.1 * (10.0 / 12),
 }
+def f(
+  a,
+  **kwargs,
+) -> A:
+    return A(
+        very_long_argument_name1=very_long_value_for_the_argument,
+        very_long_argument_name2=very_long_value_for_the_argument,
+        **kwargs,
+    )
 
 # output
 
 
 # output
 
@@ -212,3 +221,11 @@ def trailing_comma():
         C: 0.1 * (10.0 / 12),
         D: 0.1 * (10.0 / 12),
     }
         C: 0.1 * (10.0 / 12),
         D: 0.1 * (10.0 / 12),
     }
+
+
+def f(a, **kwargs) -> A:
+    return A(
+        very_long_argument_name1=very_long_value_for_the_argument,
+        very_long_argument_name2=very_long_value_for_the_argument,
+        **kwargs,
+    )
diff --git a/tests/function2.py b/tests/function2.py
new file mode 100644 (file)
index 0000000..1b9d7b6
--- /dev/null
@@ -0,0 +1,18 @@
+def f(
+  a,
+  **kwargs,
+) -> A:
+    return A(
+        very_long_argument_name1=very_long_value_for_the_argument,
+        very_long_argument_name2=very_long_value_for_the_argument,
+        **kwargs,
+    )
+
+# output
+
+def f(a, **kwargs) -> A:
+    return A(
+        very_long_argument_name1=very_long_value_for_the_argument,
+        very_long_argument_name2=very_long_value_for_the_argument,
+        **kwargs,
+    )
index 951b2988631979e1e8de4a13e0da92a29741ce5b..5b84c3ce0a143a1afe7d3ea077d063402d7462d6 100644 (file)
@@ -167,6 +167,14 @@ class BlackTestCase(unittest.TestCase):
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, line_length=ll)
 
         black.assert_equivalent(source, actual)
         black.assert_stable(source, actual, line_length=ll)
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_function2(self) -> None:
+        source, expected = read_data("function2")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, line_length=ll)
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_expression(self) -> None:
         source, expected = read_data("expression")
     @patch("black.dump_to_file", dump_to_stderr)
     def test_expression(self) -> None:
         source, expected = read_data("expression")