Skip to content

Commit 25160cd

Browse files
authored
bpo-38870: Don't put unnecessary parentheses on class declarations in ast.parse (GH-20134)
1 parent ce4a753 commit 25160cd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Lib/ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ def visit_ClassDef(self, node):
930930
self.fill("@")
931931
self.traverse(deco)
932932
self.fill("class " + node.name)
933-
with self.delimit("(", ")"):
933+
with self.delimit_if("(", ")", condition = node.bases or node.keywords):
934934
comma = False
935935
for e in node.bases:
936936
if comma:

Lib/test/test_unparse.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Foo: pass
110110

111111
docstring_prefixes = [
112112
"",
113-
"class foo():\n ",
113+
"class foo:\n ",
114114
"def foo():\n ",
115115
"async def foo():\n ",
116116
]
@@ -367,6 +367,19 @@ def test_simple_expressions_parens(self):
367367
self.check_src_roundtrip("call((yield x))")
368368
self.check_src_roundtrip("return x + (yield x)")
369369

370+
371+
def test_class_bases_and_keywords(self):
372+
self.check_src_roundtrip("class X:\n pass")
373+
self.check_src_roundtrip("class X(A):\n pass")
374+
self.check_src_roundtrip("class X(A, B, C, D):\n pass")
375+
self.check_src_roundtrip("class X(x=y):\n pass")
376+
self.check_src_roundtrip("class X(metaclass=z):\n pass")
377+
self.check_src_roundtrip("class X(x=y, z=d):\n pass")
378+
self.check_src_roundtrip("class X(A, x=y):\n pass")
379+
self.check_src_roundtrip("class X(A, **kw):\n pass")
380+
self.check_src_roundtrip("class X(*args):\n pass")
381+
self.check_src_roundtrip("class X(*args, **kwargs):\n pass")
382+
370383
def test_docstrings(self):
371384
docstrings = (
372385
'"""simple doc string"""',

0 commit comments

Comments
 (0)