Skip to content

Commit 3172c7e

Browse files
committed
Simplify fix_map
Part of #72
1 parent 638cba8 commit 3172c7e

File tree

2 files changed

+17
-51
lines changed

2 files changed

+17
-51
lines changed

libmodernize/fixes/fix_map.py

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
11
# Copyright 2008 Armin Ronacher.
22
# Licensed to PSF under a Contributor Agreement.
33

4-
from lib2to3 import fixer_base
5-
from lib2to3.fixer_util import Call, Name, touch_import, in_special_context
4+
from lib2to3 import fixer_util
5+
from lib2to3.fixes import fix_map
66

77

8-
class FixMap(fixer_base.ConditionalFix):
8+
class FixMap(fix_map.FixMap):
99

10-
BM_compatible = True
11-
order = "pre"
1210
skip_on = "six.moves.map"
1311

14-
PATTERN = """
15-
power< 'map'
16-
trailer< '('
17-
arglist< (
18-
not(argument<any '=' any>) any ','
19-
(not(argument<any '=' any>) any)+
20-
) >
21-
')' >
22-
>
23-
"""
24-
2512
def transform(self, node, results):
26-
if self.should_skip(node):
27-
# Make the fixer idempotent - if six.moves.map is already imported,
28-
# skip it. should_skip() caches the state the first time we check,
29-
# so it doesn't skip after *we've* added the six.moves import.
30-
return
31-
32-
touch_import(u'six.moves', u'map', node)
33-
if in_special_context(node):
34-
# The node is somewhere where it only needs to be an iterator,
35-
# e.g. a for loop - don't wrap in list()
36-
return
37-
38-
new = node.clone()
39-
new.prefix = ""
40-
new = Call(Name("list"), [new])
41-
new.prefix = node.prefix
42-
return new
13+
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+
fixer_util.touch_import(u'six.moves', u'map', node)
17+
return result

tests/test_fix_map.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from utils import check_on_input
22

3+
MAP_1_ARG = ("""\
4+
map(*args)
5+
""", """\
6+
from six.moves import map
7+
list(map(*args))
8+
""")
39

410
MAP_2_ARGS = ("""\
511
map(x, [1])
@@ -22,18 +28,6 @@
2228
list(map(x, [1], [2], [3]))
2329
""")
2430

25-
MAP_TOO_FEW_ARGS = ("""\
26-
map(x)
27-
""", """\
28-
map(x)
29-
""")
30-
31-
MAP_KWARGS = ("""\
32-
map(function=x, [1])
33-
""", """\
34-
map(function=x, [1])
35-
""")
36-
3731
MAP_REF = ("""\
3832
x = map
3933
""", """\
@@ -50,6 +44,9 @@
5044
""")
5145

5246

47+
def test_map_1_arg():
48+
check_on_input(*MAP_1_ARG)
49+
5350
def test_map_2_args():
5451
check_on_input(*MAP_2_ARGS)
5552

@@ -59,12 +56,6 @@ def test_map_3_args():
5956
def test_map_4_args():
6057
check_on_input(*MAP_4_ARGS)
6158

62-
def test_map_too_few_args():
63-
check_on_input(*MAP_TOO_FEW_ARGS)
64-
65-
def test_map_kwargs():
66-
check_on_input(*MAP_KWARGS)
67-
6859
def test_map_ref():
6960
check_on_input(*MAP_REF)
7061

0 commit comments

Comments
 (0)