Commit 3ad0f585 authored by Łukasz Langa's avatar Łukasz Langa
Browse files

Fix multiline strings unnecessarily wrapped in optional parentheses

Fixes #215
parent 665ed8a2
Loading
Loading
Loading
Loading
+3 −0
Changes for README.md: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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
+8 −2
Changes for black.py: 8 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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 (
+3 −0
Changes for tests/comments3.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
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]