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