]> 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:

Strip redundant parentheses from assignment exprs (#1906)
authorRishav Kundu <rk@rishav.io>
Sun, 28 Feb 2021 01:20:23 +0000 (06:50 +0530)
committerGitHub <noreply@github.com>
Sun, 28 Feb 2021 01:20:23 +0000 (17:20 -0800)
Fixes #1656

src/black/__init__.py
tests/data/pep_572.py
tests/data/pep_572_remove_parens.py [new file with mode: 0644]
tests/data/remove_parens.py
tests/test_black.py

index 6919468609ce34c6079a27c0794120b54c57acf2..a1d16d9bdeb222f9d5d2a811c52f3671323cfe6e 100644 (file)
@@ -5370,10 +5370,7 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
             check_lpar = True
 
         if check_lpar:
             check_lpar = True
 
         if check_lpar:
-            if is_walrus_assignment(child):
-                pass
-
-            elif child.type == syms.atom:
+            if child.type == syms.atom:
                 if maybe_make_parens_invisible_in_atom(child, parent=node):
                     wrap_in_parentheses(node, child, visible=False)
             elif is_one_tuple(child):
                 if maybe_make_parens_invisible_in_atom(child, parent=node):
                     wrap_in_parentheses(node, child, visible=False)
             elif is_one_tuple(child):
@@ -5545,6 +5542,7 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
     Returns whether the node should itself be wrapped in invisible parentheses.
 
     """
     Returns whether the node should itself be wrapped in invisible parentheses.
 
     """
+
     if (
         node.type != syms.atom
         or is_empty_tuple(node)
     if (
         node.type != syms.atom
         or is_empty_tuple(node)
@@ -5554,6 +5552,10 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
     ):
         return False
 
     ):
         return False
 
+    if is_walrus_assignment(node):
+        if parent.type in [syms.annassign, syms.expr_stmt]:
+            return False
+
     first = node.children[0]
     last = node.children[-1]
     if first.type == token.LPAR and last.type == token.RPAR:
     first = node.children[0]
     last = node.children[-1]
     if first.type == token.LPAR and last.type == token.RPAR:
index 637b3bb38c6b218864ebff5877b0801e2f0bfb62..c6867f2625855693197e2d262120193788528b7c 100644 (file)
@@ -2,7 +2,7 @@
 (a := a)
 if (match := pattern.search(data)) is None:
     pass
 (a := a)
 if (match := pattern.search(data)) is None:
     pass
-if (match := pattern.search(data)):
+if match := pattern.search(data):
     pass
 [y := f(x), y ** 2, y ** 3]
 filtered_data = [y for x in data if (y := f(x)) is None]
     pass
 [y := f(x), y ** 2, y ** 3]
 filtered_data = [y for x in data if (y := f(x)) is None]
@@ -43,5 +43,5 @@ foo(c=(b := 2), a=1)
 
 while x := f(x):
     pass
 
 while x := f(x):
     pass
-while (x := f(x)):
+while x := f(x):
     pass
     pass
diff --git a/tests/data/pep_572_remove_parens.py b/tests/data/pep_572_remove_parens.py
new file mode 100644 (file)
index 0000000..04cc75b
--- /dev/null
@@ -0,0 +1,38 @@
+if (foo := 0):
+    pass
+
+if (foo := 1):
+    pass
+
+if (y := 5 + 5):
+    pass
+
+y = (x := 0)
+
+y += (x := 0)
+
+(y := 5 + 5)
+
+test: int = (test2 := 2)
+
+a, b = (test := (1, 2))
+
+# output
+if foo := 0:
+    pass
+
+if foo := 1:
+    pass
+
+if y := 5 + 5:
+    pass
+
+y = (x := 0)
+
+y += (x := 0)
+
+(y := 5 + 5)
+
+test: int = (test2 := 2)
+
+a, b = (test := (1, 2))
index afc34010c30f9bc1c4aca176a4faa6d27d70fe02..abd5f71fcd0f46f40bb70ddd2aafa81aef4a46fd 100644 (file)
@@ -54,7 +54,6 @@ def example7():
 def example8():
     return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
 
 def example8():
     return (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((None)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
 
-
 # output
 x = 1
 x = 1.2
 # output
 x = 1
 x = 1.2
@@ -141,4 +140,3 @@ def example7():
 
 def example8():
     return None
 
 def example8():
     return None
-
index 5d14ceda8f49f172c6a34c66747882a6eca070cd..9c3cc64387b1d91bdc3238024a7e6d94d67aaa5c 100644 (file)
@@ -263,6 +263,15 @@ class BlackTestCase(BlackBaseTestCase):
         if sys.version_info >= (3, 8):
             black.assert_equivalent(source, actual)
 
         if sys.version_info >= (3, 8):
             black.assert_equivalent(source, actual)
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_pep_572_remove_parens(self) -> None:
+        source, expected = read_data("pep_572_remove_parens")
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_stable(source, actual, DEFAULT_MODE)
+        if sys.version_info >= (3, 8):
+            black.assert_equivalent(source, actual)
+
     def test_pep_572_version_detection(self) -> None:
         source, _ = read_data("pep_572")
         root = black.lib2to3_parse(source)
     def test_pep_572_version_detection(self) -> None:
         source, _ = read_data("pep_572")
         root = black.lib2to3_parse(source)