1
# Copyright 2007 Google, Inc. All Rights Reserved.
2
# Licensed to PSF under a Contributor Agreement.
4
"""Fixer that changes filter(F, X) into list(filter(F, X)).
6
We avoid the transformation if the filter() call is directly contained
7
in iter(<>), list(<>), tuple(<>), sorted(<>), ...join(<>), or
10
NOTE: This is still not correct if the original code was depending on
11
filter(F, X) to return a string if X is a string and a tuple if X is a
12
tuple. That would require type inference, which we don't do. Let
13
Python 2.6 figure it out.
17
from ..pgen2 import token
18
from .. import fixer_base
19
from ..fixer_util import Name, Call, ListComp, in_special_context
21
class FixFilter(fixer_base.ConditionalFix):
30
(fp=NAME | vfpdef< '(' fp=NAME ')'> ) ':' xp=any
41
trailer< '(' arglist< none='None' ',' seq=any > ')' >
46
args=trailer< '(' [any] ')' >
50
skip_on = "future_builtins.filter"
52
def transform(self, node, results):
53
if self.should_skip(node):
56
if "filter_lambda" in results:
57
new = ListComp(results.get("fp").clone(),
58
results.get("fp").clone(),
59
results.get("it").clone(),
60
results.get("xp").clone())
62
elif "none" in results:
63
new = ListComp(Name("_f"),
65
results["seq"].clone(),
69
if in_special_context(node):
73
new = Call(Name("list"), [new])
74
new.set_prefix(node.get_prefix())