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

Stop stripping parens in even more illegal spots (#2148)
authorRichard Si <63936253+ichard26@users.noreply.github.com>
Mon, 26 Apr 2021 20:26:43 +0000 (16:26 -0400)
committerGitHub <noreply@github.com>
Mon, 26 Apr 2021 20:26:43 +0000 (22:26 +0200)
We're only fixing them so fuzzers don't yell at us when we break "valid"
code. I mean "valid" because some of the examples aren't even accepted by
Python.

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

index 051de5d13df58bfe7c0b6b300cfe629da27018d9..015ca415f1cd6a45bba83dc5f0e406d49e239de4 100644 (file)
@@ -5624,6 +5624,9 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
             syms.expr_stmt,
             syms.assert_stmt,
             syms.return_stmt,
             syms.expr_stmt,
             syms.assert_stmt,
             syms.return_stmt,
+            # these ones aren't useful to end users, but they do please fuzzers
+            syms.for_stmt,
+            syms.del_stmt,
         ]:
             return False
 
         ]:
             return False
 
diff --git a/tests/data/pep_572_do_not_remove_parens.py b/tests/data/pep_572_do_not_remove_parens.py
new file mode 100644 (file)
index 0000000..20e80a6
--- /dev/null
@@ -0,0 +1,21 @@
+# Most of the following examples are really dumb, some of them aren't even accepted by Python,
+# we're fixing them only so fuzzers (which follow the grammar which actually allows these
+# examples matter of fact!) don't yell at us :p
+
+del (a := [1])
+
+try:
+    pass
+except (a := 1) as (b := why_does_this_exist):
+    pass
+
+for (z := 124) in (x := -124):
+    pass
+
+with (y := [3, 2, 1]) as (funfunfun := indeed):
+    pass
+
+
+@(please := stop)
+def sigh():
+    pass
index 9a804859a09b92f321603004d79634044afb859b..9718d95b499de4e2c966de31d10433deb471e681 100644 (file)
@@ -33,6 +33,9 @@ def foo2(answer: (p := 42) = 5):
 
 lambda: (x := 1)
 
 
 lambda: (x := 1)
 
+a[(x := 12)]
+a[:(x := 13)]
+
 # we don't touch expressions in f-strings but if we do one day, don't break 'em
 f'{(x:=10)}'
 
 # we don't touch expressions in f-strings but if we do one day, don't break 'em
 f'{(x:=10)}'
 
@@ -43,6 +46,10 @@ def a():
     yield (a := 2)
     raise (c := 3)
 
     yield (a := 2)
     raise (c := 3)
 
+def this_is_so_dumb() -> (please := no):
+    pass
+
+
 # output
 if foo := 0:
     pass
 # output
 if foo := 0:
     pass
@@ -79,6 +86,9 @@ def foo2(answer: (p := 42) = 5):
 
 lambda: (x := 1)
 
 
 lambda: (x := 1)
 
+a[(x := 12)]
+a[:(x := 13)]
+
 # we don't touch expressions in f-strings but if we do one day, don't break 'em
 f"{(x:=10)}"
 
 # we don't touch expressions in f-strings but if we do one day, don't break 'em
 f"{(x:=10)}"
 
@@ -88,3 +98,8 @@ def a():
     await (b := 1)
     yield (a := 2)
     raise (c := 3)
     await (b := 1)
     yield (a := 2)
     raise (c := 3)
+
+
+def this_is_so_dumb() -> (please := no):
+    pass
+
index 88ea31777fbb4142753f916d97bae74e2c6a26b6..c643f27cacfa9428ae41dc164359512e8912e932 100644 (file)
@@ -290,6 +290,14 @@ 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_do_not_remove_parens(self) -> None:
+        source, expected = read_data("pep_572_do_not_remove_parens")
+        # the AST safety checks will fail, but that's expected, just make sure no
+        # parentheses are touched
+        actual = black.format_str(source, mode=DEFAULT_MODE)
+        self.assertFormatEqual(expected, 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)