Unverified Commit 14b28c89 authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by GitHub
Browse files

Back out #850 (#1079)

Fixes #1042 (and probably #1044 which looks like the same thing).

The issue with the "obviously unnecessary" parentheses that #850 removed is that sometimes they're necessary to help Black fit something in one line. I didn't see an obvious solution that still removes the parens #850 was intended to remove, so let's back out this change for now in the interest of unblocking a release.

This PR also adds a test adapted from the failing example in #1042, so that if we try to reapply the #850 change we don't break the same case again.
parent a73d2588
Loading
Loading
Loading
Loading
+0 −20
Changes for black.py: 0 added lines, 20 removed lines.
Original line number Diff line number Diff line
@@ -1714,26 +1714,6 @@ def visit_default(self, node: LN) -> Iterator[Line]:
                self.current_line.append(node)
        yield from super().visit_default(node)

    def visit_atom(self, node: Node) -> Iterator[Line]:
        # Always make parentheses invisible around a single node, because it should
        # not be needed (except in the case of yield, where removing the parentheses
        # produces a SyntaxError).
        if (
            len(node.children) == 3
            and isinstance(node.children[0], Leaf)
            and node.children[0].type == token.LPAR
            and isinstance(node.children[2], Leaf)
            and node.children[2].type == token.RPAR
            and isinstance(node.children[1], Leaf)
            and not (
                node.children[1].type == token.NAME
                and node.children[1].value == "yield"
            )
        ):
            node.children[0].value = ""
            node.children[2].value = ""
        yield from super().visit_default(node)

    def visit_factor(self, node: Node) -> Iterator[Line]:
        """Force parentheses between a unary op and a binary power:

+1 −2
Changes for tests/data/expression.diff: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -171,8 +171,7 @@
+{"2.7": dead, "3.7": long_live or die_hard}
+{"2.7", "3.6", "3.7", "3.8", "3.9", "4.0" if gilectomy else "3.10"}
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]
-(SomeName)
+SomeName
 (SomeName)
 SomeName
 (Good, Bad, Ugly)
 (i for i in (1, 2, 3))
+1 −1
Changes for tests/data/expression.py: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -424,7 +424,7 @@ async def f():
{"2.7": dead, "3.7": long_live or die_hard}
{"2.7", "3.6", "3.7", "3.8", "3.9", "4.0" if gilectomy else "3.10"}
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or A, 11 or B, 12 or C]
SomeName
(SomeName)
SomeName
(Good, Bad, Ugly)
(i for i in (1, 2, 3))
+28 −4
Changes for tests/data/remove_parens.py: 28 added lines, 4 removed lines.
Original line number Diff line number Diff line
print((1))
x = (1)
x = (1.2)
(x) = (3)

data = (
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
).encode()

async def show_status():
    while True:
        try:
            if report_host:
                data = (
                   f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
               ).encode()
        except Exception as e:
            pass

def example():
    return (("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
@@ -45,10 +56,23 @@ def example8():


# output
print(1)
x = 1
x = 1.2
x = 3

data = (
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
).encode()


async def show_status():
    while True:
        try:
            if report_host:
                data = (
                    f"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                ).encode()
        except Exception as e:
            pass


def example():