[Python-checkins] (no subject)

Batuhan Taşkaya webhook-mailer at python.org
Mon Mar 9 16:27:12 EDT 2020




To: python-checkins at python.org
Subject: bpo-38870: Simplify sequence interleaves in ast.unparse (GH-17892)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/e7cab7f780ac253999512ee86374fc345434=
2811
commit: e7cab7f780ac253999512ee86374fc3454342811
branch: master
author: Batuhan Ta=C5=9Fkaya <47358913+isidentical at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-03-09T20:27:03Z
summary:

bpo-38870: Simplify sequence interleaves in ast.unparse (GH-17892)

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

diff --git a/Lib/ast.py b/Lib/ast.py
index 2719f6ff7ac59..9a3d3806eb8ca 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -613,6 +613,16 @@ def interleave(self, inter, f, seq):
                 inter()
                 f(x)
=20
+    def items_view(self, traverser, items):
+        """Traverse and separate the given *items* with a comma and append i=
t to
+        the buffer. If *items* is a single item sequence, a trailing comma
+        will be added."""
+        if len(items) =3D=3D 1:
+            traverser(items[0])
+            self.write(",")
+        else:
+            self.interleave(lambda: self.write(", "), traverser, items)
+
     def fill(self, text=3D""):
         """Indent a piece of text and append it, according to the current
         indentation level"""
@@ -1020,11 +1030,7 @@ def visit_Constant(self, node):
         value =3D node.value
         if isinstance(value, tuple):
             with self.delimit("(", ")"):
-                if len(value) =3D=3D 1:
-                    self._write_constant(value[0])
-                    self.write(",")
-                else:
-                    self.interleave(lambda: self.write(", "), self._write_co=
nstant, value)
+                self.items_view(self._write_constant, value)
         elif value is ...:
             self.write("...")
         else:
@@ -1116,12 +1122,7 @@ def write_item(item):
=20
     def visit_Tuple(self, node):
         with self.delimit("(", ")"):
-            if len(node.elts) =3D=3D 1:
-                elt =3D node.elts[0]
-                self.traverse(elt)
-                self.write(",")
-            else:
-                self.interleave(lambda: self.write(", "), self.traverse, nod=
e.elts)
+            self.items_view(self.traverse, node.elts)
=20
     unop =3D {"Invert": "~", "Not": "not", "UAdd": "+", "USub": "-"}
     unop_precedence =3D {
@@ -1264,12 +1265,7 @@ def visit_Subscript(self, node):
             if (isinstance(node.slice, Index)
                     and isinstance(node.slice.value, Tuple)
                     and node.slice.value.elts):
-                if len(node.slice.value.elts) =3D=3D 1:
-                    elt =3D node.slice.value.elts[0]
-                    self.traverse(elt)
-                    self.write(",")
-                else:
-                    self.interleave(lambda: self.write(", "), self.traverse,=
 node.slice.value.elts)
+                self.items_view(self.traverse, node.slice.value.elts)
             else:
                 self.traverse(node.slice)
=20
@@ -1296,12 +1292,7 @@ def visit_Slice(self, node):
             self.traverse(node.step)
=20
     def visit_ExtSlice(self, node):
-        if len(node.dims) =3D=3D 1:
-            elt =3D node.dims[0]
-            self.traverse(elt)
-            self.write(",")
-        else:
-            self.interleave(lambda: self.write(", "), self.traverse, node.di=
ms)
+        self.items_view(self.traverse, node.dims)
=20
     def visit_arg(self, node):
         self.write(node.arg)
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index d33f32e2a7fe9..3d87cfb6daeef 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -280,6 +280,20 @@ def test_dict_unpacking_in_dict(self):
         self.check_ast_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
         self.check_ast_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
=20
+    def test_ext_slices(self):
+        self.check_ast_roundtrip("a[i]")
+        self.check_ast_roundtrip("a[i,]")
+        self.check_ast_roundtrip("a[i, j]")
+        self.check_ast_roundtrip("a[()]")
+        self.check_ast_roundtrip("a[i:j]")
+        self.check_ast_roundtrip("a[:j]")
+        self.check_ast_roundtrip("a[i:]")
+        self.check_ast_roundtrip("a[i:j:k]")
+        self.check_ast_roundtrip("a[:j:k]")
+        self.check_ast_roundtrip("a[i::k]")
+        self.check_ast_roundtrip("a[i:j,]")
+        self.check_ast_roundtrip("a[i:j, k]")
+
     def test_invalid_raise(self):
         self.check_invalid(ast.Raise(exc=3DNone, cause=3Dast.Name(id=3D"X")))
=20
@@ -310,6 +324,12 @@ def test_docstrings(self):
             # check as Module docstrings for easy testing
             self.check_ast_roundtrip(f"'{docstring}'")
=20
+    def test_constant_tuples(self):
+        self.check_src_roundtrip(ast.Constant(value=3D(1,), kind=3DNone), "(=
1,)")
+        self.check_src_roundtrip(
+            ast.Constant(value=3D(1, 2, 3), kind=3DNone), "(1, 2, 3)"
+        )
+
=20
 class CosmeticTestCase(ASTTestCase):
     """Test if there are cosmetic issues caused by unnecesary additions"""
@@ -344,20 +364,6 @@ def test_simple_expressions_parens(self):
         self.check_src_roundtrip("call((yield x))")
         self.check_src_roundtrip("return x + (yield x)")
=20
-    def test_subscript(self):
-        self.check_src_roundtrip("a[i]")
-        self.check_src_roundtrip("a[i,]")
-        self.check_src_roundtrip("a[i, j]")
-        self.check_src_roundtrip("a[()]")
-        self.check_src_roundtrip("a[i:j]")
-        self.check_src_roundtrip("a[:j]")
-        self.check_src_roundtrip("a[i:]")
-        self.check_src_roundtrip("a[i:j:k]")
-        self.check_src_roundtrip("a[:j:k]")
-        self.check_src_roundtrip("a[i::k]")
-        self.check_src_roundtrip("a[i:j,]")
-        self.check_src_roundtrip("a[i:j, k]")
-
     def test_docstrings(self):
         docstrings =3D (
             '"""simple doc string"""',



More information about the Python-checkins mailing list