]> 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 multiline strings unnecessarily wrapped in optional parentheses
authorŁukasz Langa <lukasz@langa.pl>
Thu, 17 May 2018 02:19:48 +0000 (19:19 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Thu, 17 May 2018 02:19:48 +0000 (19:19 -0700)
Fixes #215

README.md
black.py
tests/comments3.py

index 8b7b2bd820331ee79c2ed5deaf87c88cbc253a30..3eb8ee4f131bb3bd234b457071895318932bf44c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -653,6 +653,9 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 * fixed non-deterministic formatting when multiple pairs of removable parentheses
   were used (#183)
 
+* fixed multiline strings being unnecessarily wrapped in optional
+  parentheses in long assignments (#215)
+
 * fixed not splitting long from-imports with only a single name
 
 * fixed Python 3.6+ file discovery by also looking at function calls with
index 4b5f19edced61f1ebbcf7bf06f12d172c377d68d..298597bcb30f8aad947056fc8583327a75814d5e 100644 (file)
--- a/black.py
+++ b/black.py
@@ -1315,7 +1315,7 @@ class LineGenerator(Visitor[Line]):
         The relevant Python language `keywords` for a given statement will be
         NAME leaves within it. This methods puts those on a separate line.
 
-        `parens` holds a set of string leaf values immeditely after which
+        `parens` holds a set of string leaf values immediately after which
         invisible parens should be put.
         """
         normalize_invisible_parens(node, parens_after=parens)
@@ -2361,7 +2361,7 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
                 rpar = Leaf(token.RPAR, ")")
                 index = child.remove() or 0
                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
-            else:
+            elif not (isinstance(child, Leaf) and is_multiline_string(child)):
                 # wrap child in invisible parentheses
                 lpar = Leaf(token.LPAR, "")
                 rpar = Leaf(token.RPAR, "")
@@ -2472,6 +2472,12 @@ def is_vararg(leaf: Leaf, within: Set[NodeType]) -> bool:
     return p.type in within
 
 
+def is_multiline_string(leaf: Leaf) -> bool:
+    """Return True if `leaf` is a multiline string that actually spans many lines."""
+    value = leaf.value.lstrip("furbFURB")
+    return value[:3] in {'"""', "'''"} and "\n" in value
+
+
 def is_stub_suite(node: Node) -> bool:
     """Return True if `node` is a suite with a stub body."""
     if (
index c1f0e7598aa84751a4a63c41aff99dfcb2f0e2ea..e95bd21ca9edaf6af7049c06ff967b54883c92ae 100644 (file)
@@ -1,4 +1,7 @@
 def func():
+    x = """
+    a really long string
+    """
     lcomp3 = [
         # This one is actually too long to fit in a single line.
         element.split("\n", 1)[0]