[Python-checkins] bpo-38870: Don't put unnecessary parentheses on class declarations in ast.parse (GH-20134)

Batuhan Taskaya webhook-mailer at python.org
Sat May 16 17:53:30 EDT 2020


https://github.com/python/cpython/commit/25160cdc4775a1ddb4e37c8bf5a6e31ad9c146ed
commit: 25160cdc4775a1ddb4e37c8bf5a6e31ad9c146ed
branch: master
author: Batuhan Taskaya <batuhanosmantaskaya at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-05-16T22:53:25+01:00
summary:

bpo-38870: Don't put unnecessary parentheses on class declarations in ast.parse (GH-20134)

files:
M Lib/ast.py
M Lib/test/test_unparse.py

diff --git a/Lib/ast.py b/Lib/ast.py
index 1de37b9567ece..d6cb334432c9c 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -930,7 +930,7 @@ def visit_ClassDef(self, node):
             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:
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 1393bcce741c9..410df7dbb7581 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -110,7 +110,7 @@ class Foo: pass
 
 docstring_prefixes = [
     "",
-    "class foo():\n    ",
+    "class foo:\n    ",
     "def foo():\n    ",
     "async def foo():\n    ",
 ]
@@ -367,6 +367,19 @@ def test_simple_expressions_parens(self):
         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"""',



More information about the Python-checkins mailing list