Skip to content

Commit b09fb15

Browse files
committed
Simplify fix_zip
Part of #72
1 parent 223f40b commit b09fb15

File tree

2 files changed

+8
-40
lines changed

2 files changed

+8
-40
lines changed

libmodernize/fixes/fix_zip.py

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +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 Name, Call, touch_import, in_special_context
4+
from lib2to3 import fixer_util
5+
from lib2to3.fixes import fix_zip
66

77

8-
class FixZip(fixer_base.ConditionalFix):
8+
class FixZip(fix_zip.FixZip):
99

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

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

tests/test_fix_zip.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
list(zip(*args))
3232
""")
3333

34-
ZIP_KWARGS = ("""\
35-
zip(arg1=[1])
36-
""", """\
37-
zip(arg1=[1])
38-
""")
39-
4034
ZIP_ITERATOR_CONTEXT = ("""\
4135
for a in zip(x):
4236
pass
@@ -59,8 +53,5 @@ def test_zip_call_2_args():
5953
def test_zip_call_star_args():
6054
check_on_input(*ZIP_CALL_STAR_ARGS)
6155

62-
def test_zip_kwargs():
63-
check_on_input(*ZIP_KWARGS)
64-
6556
def test_zip_iterator_context():
6657
check_on_input(*ZIP_ITERATOR_CONTEXT)

0 commit comments

Comments
 (0)