Fix multiline strings unnecessarily wrapped in optional parentheses

Fixes #215
This commit is contained in:
Łukasz Langa 2018-05-16 19:19:48 -07:00
parent 665ed8a240
commit 3ad0f5855c
3 changed files with 14 additions and 2 deletions

View File

@ -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

View File

@ -1315,7 +1315,7 @@ def visit_stmt(
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 (

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