[Python-checkins] bpo-44081: improve ast.unparse() for lambdas with no parameters (GH-26000)

isidentical webhook-mailer at python.org
Sat May 15 08:56:09 EDT 2021


https://github.com/python/cpython/commit/e4e931a67e49cf3c61263dc94fb0806c34f972cd
commit: e4e931a67e49cf3c61263dc94fb0806c34f972cd
branch: main
author: Batuhan Taskaya <isidentical at gmail.com>
committer: isidentical <isidentical at gmail.com>
date: 2021-05-15T15:55:53+03:00
summary:

bpo-44081: improve ast.unparse() for lambdas with no parameters (GH-26000)

files:
A Misc/NEWS.d/next/Library/2021-05-09-03-26-31.bpo-44081.A-Mrto.rst
M Lib/ast.py
M Lib/test/test_unparse.py

diff --git a/Lib/ast.py b/Lib/ast.py
index 18163d6b7bd163..0aef172516b3fd 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -716,9 +716,9 @@ def fill(self, text=""):
         self.maybe_newline()
         self.write("    " * self._indent + text)
 
-    def write(self, text):
-        """Append a piece of text"""
-        self._source.append(text)
+    def write(self, *text):
+        """Add new source parts"""
+        self._source.extend(text)
 
     @contextmanager
     def buffered(self, buffer = None):
@@ -1566,8 +1566,11 @@ def visit_keyword(self, node):
 
     def visit_Lambda(self, node):
         with self.require_parens(_Precedence.TEST, node):
-            self.write("lambda ")
-            self.traverse(node.args)
+            self.write("lambda")
+            with self.buffered() as buffer:
+                self.traverse(node.args)
+            if buffer:
+                self.write(" ", *buffer)
             self.write(": ")
             self.set_precedence(_Precedence.TEST, node.body)
             self.traverse(node.body)
diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py
index 534431bc969835..4d3340e26ff02f 100644
--- a/Lib/test/test_unparse.py
+++ b/Lib/test/test_unparse.py
@@ -531,6 +531,17 @@ def test_slices(self):
         self.check_src_roundtrip("a[1, 2]")
         self.check_src_roundtrip("a[(1, *a)]")
 
+    def test_lambda_parameters(self):
+        self.check_src_roundtrip("lambda: something")
+        self.check_src_roundtrip("four = lambda: 2 + 2")
+        self.check_src_roundtrip("lambda x: x * 2")
+        self.check_src_roundtrip("square = lambda n: n ** 2")
+        self.check_src_roundtrip("lambda x, y: x + y")
+        self.check_src_roundtrip("add = lambda x, y: x + y")
+        self.check_src_roundtrip("lambda x, y, /, z, q, *, u: None")
+        self.check_src_roundtrip("lambda x, *y, **z: None")
+
+
 class DirectoryTestCase(ASTTestCase):
     """Test roundtrip behaviour on all files in Lib and Lib/test."""
 
diff --git a/Misc/NEWS.d/next/Library/2021-05-09-03-26-31.bpo-44081.A-Mrto.rst b/Misc/NEWS.d/next/Library/2021-05-09-03-26-31.bpo-44081.A-Mrto.rst
new file mode 100644
index 00000000000000..e4a09e366bd807
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-05-09-03-26-31.bpo-44081.A-Mrto.rst
@@ -0,0 +1,2 @@
+:func:`ast.unparse` now doesn't use redundant spaces to separate ``lambda``
+and the ``:`` if there are no parameters.



More information about the Python-checkins mailing list