From a602703a8abc0bb5b67acb2d506124154d1f9049 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 1 Jan 2020 18:45:49 +0900 Subject: [PATCH 1/3] bpo-38871: Fix lib2to3 to be able to use in edge cases when handling filter --- Lib/lib2to3/fixes/fix_filter.py | 10 +++++++--- Lib/lib2to3/tests/test_fixers.py | 5 +++++ .../Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst diff --git a/Lib/lib2to3/fixes/fix_filter.py b/Lib/lib2to3/fixes/fix_filter.py index a7a5a154f6fb113..38e9078f11ac88f 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 3da5dd845c93c66..a28524198181333 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 000000000000000..3a2f73c20313d58 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst @@ -0,0 +1 @@ +Fix :mod:`lib2to3` to be able to use in edge cases when handling filter From 75d1792baaae7f11cafc540b3ffecbe0397845e7 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 7 Jan 2020 11:37:38 +0900 Subject: [PATCH 2/3] bpo-38871: Update NEWS.d --- .../next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 index 3a2f73c20313d58..b778d4d53dadb76 100644 --- 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 @@ -1 +1,3 @@ -Fix :mod:`lib2to3` to be able to use in edge cases when handling filter +Updated the logic of :mod:`lib2to3` when handling filter based statement +with lambda expression is needed to be parenthesized. +Patch by Dong-hee Na. From 4529cb4aa128250390a853277cf587b3c72fee36 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Tue, 7 Jan 2020 21:20:13 +0900 Subject: [PATCH 3/3] bpo-38871: Update NEWS.d --- .../next/Library/2020-01-01-18-44-52.bpo-38871.3EEOLg.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 index b778d4d53dadb76..fe970fd9e3fa1a2 100644 --- 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 @@ -1,3 +1,2 @@ -Updated the logic of :mod:`lib2to3` when handling filter based statement -with lambda expression is needed to be parenthesized. -Patch by Dong-hee Na. +Correctly parenthesize filter-based statements that contain lambda +expressions in mod:`lib2to3`. Patch by Dong-hee Na.