Skip to content

Commit 11d3d01

Browse files
committed
Skip six imports for map and filter when changed to a list comp
1 parent 71f6841 commit 11d3d01

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

libmodernize/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,13 @@ def add_future(node, symbol):
6767
def touch_import(package, name, node):
6868
add_future(node, 'absolute_import')
6969
fixer_util.touch_import(package, name, node)
70+
71+
72+
def is_listcomp(node):
73+
return (isinstance(node, Node) and
74+
node.type == syms.atom and
75+
len(node.children) >= 2 and
76+
isinstance(node.children[0], Leaf) and
77+
node.children[0].value == '[' and
78+
isinstance(node.children[-1], Leaf) and
79+
node.children[-1].value == ']')

libmodernize/fixes/fix_filter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
# Licensed to PSF under a Contributor Agreement.
33
from __future__ import absolute_import
44

5+
from lib2to3 import fixer_util
6+
from lib2to3 import pygram
7+
from lib2to3 import pytree
58
from lib2to3.fixes import fix_filter
69
import libmodernize
710

@@ -12,7 +15,8 @@ class FixFilter(fix_filter.FixFilter):
1215

1316
def transform(self, node, results):
1417
result = super(FixFilter, self).transform(node, results)
15-
# Keep performance improvement from six.moves.filter in iterator
16-
# contexts on Python 2.7.
17-
libmodernize.touch_import(u'six.moves', u'filter', node)
18+
if not libmodernize.is_listcomp(result):
19+
# Keep performance improvement from six.moves.filter in iterator
20+
# contexts on Python 2.7.
21+
libmodernize.touch_import(u'six.moves', u'filter', node)
1822
return result

libmodernize/fixes/fix_map.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed to PSF under a Contributor Agreement.
33
from __future__ import absolute_import
44

5+
from lib2to3 import fixer_util
56
from lib2to3.fixes import fix_map
67
import libmodernize
78

@@ -11,7 +12,8 @@ class FixMap(fix_map.FixMap):
1112

1213
def transform(self, node, results):
1314
result = super(FixMap, self).transform(node, results)
14-
# Always use the import even if no change is required so as to have
15-
# improved performance in iterator contexts even on Python 2.7.
16-
libmodernize.touch_import(u'six.moves', u'map', node)
15+
if not libmodernize.is_listcomp(result):
16+
# Always use the import even if no change is required so as to have
17+
# improved performance in iterator contexts even on Python 2.7.
18+
libmodernize.touch_import(u'six.moves', u'map', node)
1719
return result

tests/test_fix_filter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@
2222
pass
2323
""")
2424

25+
FILTER_NONE = ("""\
26+
filter(None, x)
27+
""", """\
28+
[_f for _f in x if _f]
29+
""")
30+
2531

2632
def test_filter_call():
2733
check_on_input(*FILTER_CALL)
2834

2935
def test_filter_iterator_context():
3036
check_on_input(*FILTER_ITERATOR_CONTEXT)
37+
38+
def test_filter_None():
39+
check_on_input(*FILTER_NONE)

tests/test_fix_map.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
pass
5151
""")
5252

53+
MAP_LAMBDA = ("""\
54+
x = map(lambda x: x+1, stuff)
55+
""", """\
56+
x = [x+1 for x in stuff]
57+
""")
58+
5359

5460
def test_map_1_arg():
5561
check_on_input(*MAP_1_ARG)
@@ -68,3 +74,6 @@ def test_map_ref():
6874

6975
def test_map_iterator_context():
7076
check_on_input(*MAP_ITERATOR_CONTEXT)
77+
78+
def test_map_lambda():
79+
check_on_input(*MAP_LAMBDA)

0 commit comments

Comments
 (0)