[Python-checkins] bpo-38871: Fix lib2to3 for filter-based statements that contain lambda (GH-17780)

Miss Islington (bot) webhook-mailer at python.org
Tue Jan 7 12:52:12 EST 2020


https://github.com/python/cpython/commit/39a5c889d30d03a88102e56f03ee0c95db198fb3
commit: 39a5c889d30d03a88102e56f03ee0c95db198fb3
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-01-07T09:52:06-08:00
summary:

bpo-38871: Fix lib2to3 for filter-based statements that contain lambda (GH-17780)


Correctly parenthesize filter-based statements that contain lambda
expressions in lib2to3.
(cherry picked from commit b821173b5458d137c8d5edb6e9b4997aac800a38)

Co-authored-by: Dong-hee Na <donghee.na92 at gmail.com>

files:
A Misc/NEWS.d/next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst
M Lib/lib2to3/fixes/fix_filter.py
M Lib/lib2to3/tests/test_fixers.py

diff --git a/Lib/lib2to3/fixes/fix_filter.py b/Lib/lib2to3/fixes/fix_filter.py
index a7a5a154f6fb1..38e9078f11ac8 100644
--- a/Lib/lib2to3/fixes/fix_filter.py
+++ b/Lib/lib2to3/fixes/fix_filter.py
@@ -17,7 +17,7 @@
 from .. import fixer_base
 from ..pytree import Node
 from ..pygram import python_symbols as syms
-from ..fixer_util import Name, ArgList, ListComp, in_special_context
+from ..fixer_util import Name, ArgList, ListComp, in_special_context, parenthesize
 
 
 class FixFilter(fixer_base.ConditionalFix):
@@ -65,10 +65,14 @@ def transform(self, node, results):
                 trailers.append(t.clone())
 
         if "filter_lambda" in results:
+            xp = results.get("xp").clone()
+            if xp.type == syms.test:
+                xp.prefix = ""
+                xp = parenthesize(xp)
+
             new = ListComp(results.get("fp").clone(),
                            results.get("fp").clone(),
-                           results.get("it").clone(),
-                           results.get("xp").clone())
+                           results.get("it").clone(), xp)
             new = Node(syms.power, [new] + trailers, prefix="")
 
         elif "none" in results:
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
index 3da5dd845c93c..a285241981813 100644
--- a/Lib/lib2to3/tests/test_fixers.py
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -2954,6 +2954,11 @@ def test_filter_basic(self):
         a = """x = [x for x in range(10) if x%2 == 0]"""
         self.check(b, a)
 
+        # bpo-38871
+        b = """filter(lambda x: True if x > 2 else False, [1, 2, 3])"""
+        a = """[x for x in [1, 2, 3] if (True if x > 2 else False)]"""
+        self.check(b, a)
+
     def test_filter_trailers(self):
         b = """x = filter(None, 'abc')[0]"""
         a = """x = [_f for _f in 'abc' if _f][0]"""
diff --git a/Misc/NEWS.d/next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst b/Misc/NEWS.d/next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst
new file mode 100644
index 0000000000000..fe970fd9e3fa1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst
@@ -0,0 +1,2 @@
+Correctly parenthesize filter-based statements that contain lambda
+expressions in mod:`lib2to3`. Patch by Dong-hee Na.



More information about the Python-checkins mailing list