1 | n/a | # Copyright 2007 Google, Inc. All Rights Reserved. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Fixer for dict methods. |
---|
5 | n/a | |
---|
6 | n/a | d.keys() -> list(d.keys()) |
---|
7 | n/a | d.items() -> list(d.items()) |
---|
8 | n/a | d.values() -> list(d.values()) |
---|
9 | n/a | |
---|
10 | n/a | d.iterkeys() -> iter(d.keys()) |
---|
11 | n/a | d.iteritems() -> iter(d.items()) |
---|
12 | n/a | d.itervalues() -> iter(d.values()) |
---|
13 | n/a | |
---|
14 | n/a | d.viewkeys() -> d.keys() |
---|
15 | n/a | d.viewitems() -> d.items() |
---|
16 | n/a | d.viewvalues() -> d.values() |
---|
17 | n/a | |
---|
18 | n/a | Except in certain very specific contexts: the iter() can be dropped |
---|
19 | n/a | when the context is list(), sorted(), iter() or for...in; the list() |
---|
20 | n/a | can be dropped when the context is list() or sorted() (but not iter() |
---|
21 | n/a | or for...in!). Special contexts that apply to both: list(), sorted(), tuple() |
---|
22 | n/a | set(), any(), all(), sum(). |
---|
23 | n/a | |
---|
24 | n/a | Note: iter(d.keys()) could be written as iter(d) but since the |
---|
25 | n/a | original d.iterkeys() was also redundant we don't fix this. And there |
---|
26 | n/a | are (rare) contexts where it makes a difference (e.g. when passing it |
---|
27 | n/a | as an argument to a function that introspects the argument). |
---|
28 | n/a | """ |
---|
29 | n/a | |
---|
30 | n/a | # Local imports |
---|
31 | n/a | from .. import pytree |
---|
32 | n/a | from .. import patcomp |
---|
33 | n/a | from .. import fixer_base |
---|
34 | n/a | from ..fixer_util import Name, Call, Dot |
---|
35 | n/a | from .. import fixer_util |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | iter_exempt = fixer_util.consuming_calls | {"iter"} |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | class FixDict(fixer_base.BaseFix): |
---|
42 | n/a | BM_compatible = True |
---|
43 | n/a | |
---|
44 | n/a | PATTERN = """ |
---|
45 | n/a | power< head=any+ |
---|
46 | n/a | trailer< '.' method=('keys'|'items'|'values'| |
---|
47 | n/a | 'iterkeys'|'iteritems'|'itervalues'| |
---|
48 | n/a | 'viewkeys'|'viewitems'|'viewvalues') > |
---|
49 | n/a | parens=trailer< '(' ')' > |
---|
50 | n/a | tail=any* |
---|
51 | n/a | > |
---|
52 | n/a | """ |
---|
53 | n/a | |
---|
54 | n/a | def transform(self, node, results): |
---|
55 | n/a | head = results["head"] |
---|
56 | n/a | method = results["method"][0] # Extract node for method name |
---|
57 | n/a | tail = results["tail"] |
---|
58 | n/a | syms = self.syms |
---|
59 | n/a | method_name = method.value |
---|
60 | n/a | isiter = method_name.startswith("iter") |
---|
61 | n/a | isview = method_name.startswith("view") |
---|
62 | n/a | if isiter or isview: |
---|
63 | n/a | method_name = method_name[4:] |
---|
64 | n/a | assert method_name in ("keys", "items", "values"), repr(method) |
---|
65 | n/a | head = [n.clone() for n in head] |
---|
66 | n/a | tail = [n.clone() for n in tail] |
---|
67 | n/a | special = not tail and self.in_special_context(node, isiter) |
---|
68 | n/a | args = head + [pytree.Node(syms.trailer, |
---|
69 | n/a | [Dot(), |
---|
70 | n/a | Name(method_name, |
---|
71 | n/a | prefix=method.prefix)]), |
---|
72 | n/a | results["parens"].clone()] |
---|
73 | n/a | new = pytree.Node(syms.power, args) |
---|
74 | n/a | if not (special or isview): |
---|
75 | n/a | new.prefix = "" |
---|
76 | n/a | new = Call(Name("iter" if isiter else "list"), [new]) |
---|
77 | n/a | if tail: |
---|
78 | n/a | new = pytree.Node(syms.power, [new] + tail) |
---|
79 | n/a | new.prefix = node.prefix |
---|
80 | n/a | return new |
---|
81 | n/a | |
---|
82 | n/a | P1 = "power< func=NAME trailer< '(' node=any ')' > any* >" |
---|
83 | n/a | p1 = patcomp.compile_pattern(P1) |
---|
84 | n/a | |
---|
85 | n/a | P2 = """for_stmt< 'for' any 'in' node=any ':' any* > |
---|
86 | n/a | | comp_for< 'for' any 'in' node=any any* > |
---|
87 | n/a | """ |
---|
88 | n/a | p2 = patcomp.compile_pattern(P2) |
---|
89 | n/a | |
---|
90 | n/a | def in_special_context(self, node, isiter): |
---|
91 | n/a | if node.parent is None: |
---|
92 | n/a | return False |
---|
93 | n/a | results = {} |
---|
94 | n/a | if (node.parent.parent is not None and |
---|
95 | n/a | self.p1.match(node.parent.parent, results) and |
---|
96 | n/a | results["node"] is node): |
---|
97 | n/a | if isiter: |
---|
98 | n/a | # iter(d.iterkeys()) -> iter(d.keys()), etc. |
---|
99 | n/a | return results["func"].value in iter_exempt |
---|
100 | n/a | else: |
---|
101 | n/a | # list(d.keys()) -> list(d.keys()), etc. |
---|
102 | n/a | return results["func"].value in fixer_util.consuming_calls |
---|
103 | n/a | if not isiter: |
---|
104 | n/a | return False |
---|
105 | n/a | # for ... in d.iterkeys() -> for ... in d.keys(), etc. |
---|
106 | n/a | return self.p2.match(node.parent, results) and results["node"] is node |
---|