]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

remove obviously unnecessary parentheses (#850)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Thu, 16 May 2019 04:11:04 +0000 (21:11 -0700)
committerGitHub <noreply@github.com>
Thu, 16 May 2019 04:11:04 +0000 (21:11 -0700)
Fixes #548

black.py
tests/data/expression.diff
tests/data/expression.py
tests/data/remove_parens.py [new file with mode: 0644]
tests/test_black.py

index 17aea7af264adafc4f5ee18cb9389cdfcdce8e17..694c1e961aa41bef489ea08145f1ee9faed7c69b 100644 (file)
--- 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.
index adca2c80d6b7dca2d8573ed9d19a618160f72147..9e01f3fad37aa9b62282b4315a07d64ca7fb5599 100644 (file)
 +{"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))
index b889bfc788c99f349a2377af8eff089445c87599..a2fc5897e6de80ce6b5a7e79cf2ff6279ee55865 100644 (file)
@@ -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 (file)
index 0000000..f08f91a
--- /dev/null
@@ -0,0 +1,10 @@
+print((1))
+x = (1)
+x = (1.2)
+(x) = (3)
+
+# output
+print(1)
+x = 1
+x = 1.2
+x = 3
index 0ea4ac55078393c93e74458d3c60ca98102262bb..88c03d05fb83e8e364b0454d30d9e48bb110517f 100644 (file)
@@ -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")