Unverified Commit 957ba24b authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by GitHub
Browse files

remove obviously unnecessary parentheses (#850)

Fixes #548
parent 188c31db
Loading
Loading
Loading
Loading
+20 −0
Changes for black.py: 20 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1602,6 +1602,26 @@ 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_INDENT(self, node: Node) -> Iterator[Line]:
        """Increase indentation level, maybe yield a line."""
        # In blib2to3 INDENT never holds comments.
+2 −1
Changes for tests/data/expression.diff: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -158,7 +158,8 @@
+{"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
@@ -410,7 +410,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))
+10 −0
Changes for tests/data/remove_parens.py: 10 added lines, 0 removed lines.
Original line number Diff line number Diff line
print((1))
x = (1)
x = (1.2)
(x) = (3)

# output
print(1)
x = 1
x = 1.2
x = 3
+8 −0
Changes for tests/test_black.py: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -436,6 +436,14 @@ def test_empty_lines(self) -> None:
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode())

    @patch("black.dump_to_file", dump_to_stderr)
    def test_remove_parens(self) -> None:
        source, expected = read_data("remove_parens")
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode())

    @patch("black.dump_to_file", dump_to_stderr)
    def test_string_prefixes(self) -> None:
        source, expected = read_data("string_prefixes")