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

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