]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38870: Don't put unnecessary parentheses on class declarations in ast.parse ...
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>
Sat, 16 May 2020 21:53:25 +0000 (00:53 +0300)
committerGitHub <noreply@github.com>
Sat, 16 May 2020 21:53:25 +0000 (22:53 +0100)
Lib/ast.py
Lib/test/test_unparse.py

index 1de37b9567ece2f737be5ae0302efc9b3c437773..d6cb334432c9c6e39ebf037b601be19c8887626c 100644 (file)
@@ -930,7 +930,7 @@ class _Unparser(NodeVisitor):
             self.fill("@")
             self.traverse(deco)
         self.fill("class " + node.name)
-        with self.delimit("(", ")"):
+        with self.delimit_if("(", ")", condition = node.bases or node.keywords):
             comma = False
             for e in node.bases:
                 if comma:
index 1393bcce741c9430a25e15b959ddcd4eca362908..410df7dbb75818d4cd7e8750f25ccc595d2a6ad5 100644 (file)
@@ -110,7 +110,7 @@ with f() as x, g() as y:
 
 docstring_prefixes = [
     "",
-    "class foo():\n    ",
+    "class foo:\n    ",
     "def foo():\n    ",
     "async def foo():\n    ",
 ]
@@ -367,6 +367,19 @@ class CosmeticTestCase(ASTTestCase):
         self.check_src_roundtrip("call((yield x))")
         self.check_src_roundtrip("return x + (yield x)")
 
+
+    def test_class_bases_and_keywords(self):
+        self.check_src_roundtrip("class X:\n    pass")
+        self.check_src_roundtrip("class X(A):\n    pass")
+        self.check_src_roundtrip("class X(A, B, C, D):\n    pass")
+        self.check_src_roundtrip("class X(x=y):\n    pass")
+        self.check_src_roundtrip("class X(metaclass=z):\n    pass")
+        self.check_src_roundtrip("class X(x=y, z=d):\n    pass")
+        self.check_src_roundtrip("class X(A, x=y):\n    pass")
+        self.check_src_roundtrip("class X(A, **kw):\n    pass")
+        self.check_src_roundtrip("class X(*args):\n    pass")
+        self.check_src_roundtrip("class X(*args, **kwargs):\n    pass")
+
     def test_docstrings(self):
         docstrings = (
             '"""simple doc string"""',