ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_filter.py

Python code coverage for Lib/lib2to3/fixes/fix_filter.py

#countcontent
1n/a# Copyright 2007 Google, Inc. All Rights Reserved.
2n/a# Licensed to PSF under a Contributor Agreement.
3n/a
4n/a"""Fixer that changes filter(F, X) into list(filter(F, X)).
5n/a
6n/aWe avoid the transformation if the filter() call is directly contained
7n/ain iter(<>), list(<>), tuple(<>), sorted(<>), ...join(<>), or
8n/afor V in <>:.
9n/a
10n/aNOTE: This is still not correct if the original code was depending on
11n/afilter(F, X) to return a string if X is a string and a tuple if X is a
12n/atuple. That would require type inference, which we don't do. Let
13n/aPython 2.6 figure it out.
14n/a"""
15n/a
16n/a# Local imports
17n/afrom .. import fixer_base
18n/afrom ..fixer_util import Name, Call, ListComp, in_special_context
19n/a
20n/aclass FixFilter(fixer_base.ConditionalFix):
21n/a BM_compatible = True
22n/a
23n/a PATTERN = """
24n/a filter_lambda=power<
25n/a 'filter'
26n/a trailer<
27n/a '('
28n/a arglist<
29n/a lambdef< 'lambda'
30n/a (fp=NAME | vfpdef< '(' fp=NAME ')'> ) ':' xp=any
31n/a >
32n/a ','
33n/a it=any
34n/a >
35n/a ')'
36n/a >
37n/a >
38n/a |
39n/a power<
40n/a 'filter'
41n/a trailer< '(' arglist< none='None' ',' seq=any > ')' >
42n/a >
43n/a |
44n/a power<
45n/a 'filter'
46n/a args=trailer< '(' [any] ')' >
47n/a >
48n/a """
49n/a
50n/a skip_on = "future_builtins.filter"
51n/a
52n/a def transform(self, node, results):
53n/a if self.should_skip(node):
54n/a return
55n/a
56n/a if "filter_lambda" in results:
57n/a new = ListComp(results.get("fp").clone(),
58n/a results.get("fp").clone(),
59n/a results.get("it").clone(),
60n/a results.get("xp").clone())
61n/a
62n/a elif "none" in results:
63n/a new = ListComp(Name("_f"),
64n/a Name("_f"),
65n/a results["seq"].clone(),
66n/a Name("_f"))
67n/a
68n/a else:
69n/a if in_special_context(node):
70n/a return None
71n/a new = node.clone()
72n/a new.prefix = ""
73n/a new = Call(Name("list"), [new])
74n/a new.prefix = node.prefix
75n/a return new