File tree Expand file tree Collapse file tree 5 files changed +40
-6
lines changed Expand file tree Collapse file tree 5 files changed +40
-6
lines changed Original file line number Diff line number Diff line change @@ -67,3 +67,13 @@ def add_future(node, symbol):
67
67
def touch_import (package , name , node ):
68
68
add_future (node , 'absolute_import' )
69
69
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 == ']' )
Original file line number Diff line number Diff line change 2
2
# Licensed to PSF under a Contributor Agreement.
3
3
from __future__ import absolute_import
4
4
5
+ from lib2to3 import fixer_util
6
+ from lib2to3 import pygram
7
+ from lib2to3 import pytree
5
8
from lib2to3 .fixes import fix_filter
6
9
import libmodernize
7
10
@@ -12,7 +15,8 @@ class FixFilter(fix_filter.FixFilter):
12
15
13
16
def transform (self , node , results ):
14
17
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 )
18
22
return result
Original file line number Diff line number Diff line change 2
2
# Licensed to PSF under a Contributor Agreement.
3
3
from __future__ import absolute_import
4
4
5
+ from lib2to3 import fixer_util
5
6
from lib2to3 .fixes import fix_map
6
7
import libmodernize
7
8
@@ -11,7 +12,8 @@ class FixMap(fix_map.FixMap):
11
12
12
13
def transform (self , node , results ):
13
14
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 )
17
19
return result
Original file line number Diff line number Diff line change 22
22
pass
23
23
""" )
24
24
25
+ FILTER_NONE = ("""\
26
+ filter(None, x)
27
+ """ , """\
28
+ [_f for _f in x if _f]
29
+ """ )
30
+
25
31
26
32
def test_filter_call ():
27
33
check_on_input (* FILTER_CALL )
28
34
29
35
def test_filter_iterator_context ():
30
36
check_on_input (* FILTER_ITERATOR_CONTEXT )
37
+
38
+ def test_filter_None ():
39
+ check_on_input (* FILTER_NONE )
Original file line number Diff line number Diff line change 50
50
pass
51
51
""" )
52
52
53
+ MAP_LAMBDA = ("""\
54
+ x = map(lambda x: x+1, stuff)
55
+ """ , """\
56
+ x = [x+1 for x in stuff]
57
+ """ )
58
+
53
59
54
60
def test_map_1_arg ():
55
61
check_on_input (* MAP_1_ARG )
@@ -68,3 +74,6 @@ def test_map_ref():
68
74
69
75
def test_map_iterator_context ():
70
76
check_on_input (* MAP_ITERATOR_CONTEXT )
77
+
78
+ def test_map_lambda ():
79
+ check_on_input (* MAP_LAMBDA )
You can’t perform that action at this time.
0 commit comments