1 | n/a | # Copyright 2006 Google, Inc. All Rights Reserved. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Fixer for has_key(). |
---|
5 | n/a | |
---|
6 | n/a | Calls to .has_key() methods are expressed in terms of the 'in' |
---|
7 | n/a | operator: |
---|
8 | n/a | |
---|
9 | n/a | d.has_key(k) -> k in d |
---|
10 | n/a | |
---|
11 | n/a | CAVEATS: |
---|
12 | n/a | 1) While the primary target of this fixer is dict.has_key(), the |
---|
13 | n/a | fixer will change any has_key() method call, regardless of its |
---|
14 | n/a | class. |
---|
15 | n/a | |
---|
16 | n/a | 2) Cases like this will not be converted: |
---|
17 | n/a | |
---|
18 | n/a | m = d.has_key |
---|
19 | n/a | if m(k): |
---|
20 | n/a | ... |
---|
21 | n/a | |
---|
22 | n/a | Only *calls* to has_key() are converted. While it is possible to |
---|
23 | n/a | convert the above to something like |
---|
24 | n/a | |
---|
25 | n/a | m = d.__contains__ |
---|
26 | n/a | if m(k): |
---|
27 | n/a | ... |
---|
28 | n/a | |
---|
29 | n/a | this is currently not done. |
---|
30 | n/a | """ |
---|
31 | n/a | |
---|
32 | n/a | # Local imports |
---|
33 | n/a | from .. import pytree |
---|
34 | n/a | from .. import fixer_base |
---|
35 | n/a | from ..fixer_util import Name, parenthesize |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | class FixHasKey(fixer_base.BaseFix): |
---|
39 | n/a | BM_compatible = True |
---|
40 | n/a | |
---|
41 | n/a | PATTERN = """ |
---|
42 | n/a | anchor=power< |
---|
43 | n/a | before=any+ |
---|
44 | n/a | trailer< '.' 'has_key' > |
---|
45 | n/a | trailer< |
---|
46 | n/a | '(' |
---|
47 | n/a | ( not(arglist | argument<any '=' any>) arg=any |
---|
48 | n/a | | arglist<(not argument<any '=' any>) arg=any ','> |
---|
49 | n/a | ) |
---|
50 | n/a | ')' |
---|
51 | n/a | > |
---|
52 | n/a | after=any* |
---|
53 | n/a | > |
---|
54 | n/a | | |
---|
55 | n/a | negation=not_test< |
---|
56 | n/a | 'not' |
---|
57 | n/a | anchor=power< |
---|
58 | n/a | before=any+ |
---|
59 | n/a | trailer< '.' 'has_key' > |
---|
60 | n/a | trailer< |
---|
61 | n/a | '(' |
---|
62 | n/a | ( not(arglist | argument<any '=' any>) arg=any |
---|
63 | n/a | | arglist<(not argument<any '=' any>) arg=any ','> |
---|
64 | n/a | ) |
---|
65 | n/a | ')' |
---|
66 | n/a | > |
---|
67 | n/a | > |
---|
68 | n/a | > |
---|
69 | n/a | """ |
---|
70 | n/a | |
---|
71 | n/a | def transform(self, node, results): |
---|
72 | n/a | assert results |
---|
73 | n/a | syms = self.syms |
---|
74 | n/a | if (node.parent.type == syms.not_test and |
---|
75 | n/a | self.pattern.match(node.parent)): |
---|
76 | n/a | # Don't transform a node matching the first alternative of the |
---|
77 | n/a | # pattern when its parent matches the second alternative |
---|
78 | n/a | return None |
---|
79 | n/a | negation = results.get("negation") |
---|
80 | n/a | anchor = results["anchor"] |
---|
81 | n/a | prefix = node.prefix |
---|
82 | n/a | before = [n.clone() for n in results["before"]] |
---|
83 | n/a | arg = results["arg"].clone() |
---|
84 | n/a | after = results.get("after") |
---|
85 | n/a | if after: |
---|
86 | n/a | after = [n.clone() for n in after] |
---|
87 | n/a | if arg.type in (syms.comparison, syms.not_test, syms.and_test, |
---|
88 | n/a | syms.or_test, syms.test, syms.lambdef, syms.argument): |
---|
89 | n/a | arg = parenthesize(arg) |
---|
90 | n/a | if len(before) == 1: |
---|
91 | n/a | before = before[0] |
---|
92 | n/a | else: |
---|
93 | n/a | before = pytree.Node(syms.power, before) |
---|
94 | n/a | before.prefix = " " |
---|
95 | n/a | n_op = Name("in", prefix=" ") |
---|
96 | n/a | if negation: |
---|
97 | n/a | n_not = Name("not", prefix=" ") |
---|
98 | n/a | n_op = pytree.Node(syms.comp_op, (n_not, n_op)) |
---|
99 | n/a | new = pytree.Node(syms.comparison, (arg, n_op, before)) |
---|
100 | n/a | if after: |
---|
101 | n/a | new = parenthesize(new) |
---|
102 | n/a | new = pytree.Node(syms.power, (new,) + tuple(after)) |
---|
103 | n/a | if node.parent.type in (syms.comparison, syms.expr, syms.xor_expr, |
---|
104 | n/a | syms.and_expr, syms.shift_expr, |
---|
105 | n/a | syms.arith_expr, syms.term, |
---|
106 | n/a | syms.factor, syms.power): |
---|
107 | n/a | new = parenthesize(new) |
---|
108 | n/a | new.prefix = prefix |
---|
109 | n/a | return new |
---|