Skip to content

Commit 223f40b

Browse files
committed
Simplify fix_filter
Part of #72
1 parent 3172c7e commit 223f40b

File tree

2 files changed

+12
-63
lines changed

2 files changed

+12
-63
lines changed

libmodernize/fixes/fix_filter.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_filter
66

77

8-
class FixFilter(fixer_base.ConditionalFix):
8+
class FixFilter(fix_filter.FixFilter):
99

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

14-
PATTERN = """
15-
power< 'filter'
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.filter 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'filter', 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(FixFilter, self).transform(node, results)
14+
# Keep performance improvement from six.moves.filter in iterator
15+
# contexts on Python 2.7.
16+
fixer_util.touch_import(u'six.moves', u'filter', node)
17+
return result

tests/test_fix_filter.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,25 @@
22

33

44
FILTER_CALL = ("""\
5-
filter(None, [1])
5+
filter(func, [1])
66
""", """\
77
from six.moves import filter
8-
list(filter(None, [1]))
8+
list(filter(func, [1]))
99
""")
1010

11-
FILTER_TOO_FEW_ARGS = ("""\
12-
filter(None)
13-
""", """\
14-
filter(None)
15-
""")
16-
17-
FILTER_TOO_MANY_ARGS = ("""\
18-
filter(None, [1], [2])
19-
""", """\
20-
filter(None, [1], [2])
21-
""")
22-
23-
FILTER_KWARGS = ("""\
24-
filter(function=None, [1])
25-
""", """\
26-
filter(function=None, [1])
27-
""")
2811

2912
FILTER_ITERATOR_CONTEXT = ("""\
30-
for a in filter(None, [1]):
13+
for a in filter(func, [1]):
3114
pass
3215
""", """\
3316
from six.moves import filter
34-
for a in filter(None, [1]):
17+
for a in filter(func, [1]):
3518
pass
3619
""")
3720

3821

3922
def test_filter_call():
4023
check_on_input(*FILTER_CALL)
4124

42-
def test_filter_too_few_args():
43-
check_on_input(*FILTER_TOO_FEW_ARGS)
44-
45-
def test_filter_too_many_args():
46-
check_on_input(*FILTER_TOO_MANY_ARGS)
47-
48-
def test_filter_kwargs():
49-
check_on_input(*FILTER_KWARGS)
50-
5125
def test_filter_iterator_context():
5226
check_on_input(*FILTER_ITERATOR_CONTEXT)

0 commit comments

Comments
 (0)