From: Ɓukasz Langa Date: Thu, 17 May 2018 02:19:48 +0000 (-0700) Subject: Fix multiline strings unnecessarily wrapped in optional parentheses X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/3ad0f5855c46410652b27b5e09c6f22314241757?ds=sidebyside;pf=etc Fix multiline strings unnecessarily wrapped in optional parentheses Fixes #215 --- diff --git a/README.md b/README.md index 8b7b2bd..3eb8ee4 100644 --- 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 diff --git a/black.py b/black.py index 4b5f19e..298597b 100644 --- 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 ( diff --git a/tests/comments3.py b/tests/comments3.py index c1f0e75..e95bd21 100644 --- a/tests/comments3.py +++ b/tests/comments3.py @@ -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]