From: Jelle Zijlstra Date: Thu, 16 May 2019 04:11:04 +0000 (-0700) Subject: remove obviously unnecessary parentheses (#850) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/957ba24bb6cdd32e4dd14ff2808dcf4d58851844 remove obviously unnecessary parentheses (#850) Fixes #548 --- diff --git a/black.py b/black.py index 17aea7a..694c1e9 100644 --- a/black.py +++ b/black.py @@ -1602,6 +1602,26 @@ class LineGenerator(Visitor[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. diff --git a/tests/data/expression.diff b/tests/data/expression.diff index adca2c8..9e01f3f 100644 --- a/tests/data/expression.diff +++ b/tests/data/expression.diff @@ -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)) diff --git a/tests/data/expression.py b/tests/data/expression.py index b889bfc..a2fc589 100644 --- a/tests/data/expression.py +++ b/tests/data/expression.py @@ -410,7 +410,7 @@ numpy[np.newaxis, :] {"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)) diff --git a/tests/data/remove_parens.py b/tests/data/remove_parens.py new file mode 100644 index 0000000..f08f91a --- /dev/null +++ b/tests/data/remove_parens.py @@ -0,0 +1,10 @@ +print((1)) +x = (1) +x = (1.2) +(x) = (3) + +# output +print(1) +x = 1 +x = 1.2 +x = 3 diff --git a/tests/test_black.py b/tests/test_black.py index 0ea4ac5..88c03d0 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -436,6 +436,14 @@ class BlackTestCase(unittest.TestCase): 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")