1 | n/a | """Adjust some old Python 2 idioms to their modern counterparts. |
---|
2 | n/a | |
---|
3 | n/a | * Change some type comparisons to isinstance() calls: |
---|
4 | n/a | type(x) == T -> isinstance(x, T) |
---|
5 | n/a | type(x) is T -> isinstance(x, T) |
---|
6 | n/a | type(x) != T -> not isinstance(x, T) |
---|
7 | n/a | type(x) is not T -> not isinstance(x, T) |
---|
8 | n/a | |
---|
9 | n/a | * Change "while 1:" into "while True:". |
---|
10 | n/a | |
---|
11 | n/a | * Change both |
---|
12 | n/a | |
---|
13 | n/a | v = list(EXPR) |
---|
14 | n/a | v.sort() |
---|
15 | n/a | foo(v) |
---|
16 | n/a | |
---|
17 | n/a | and the more general |
---|
18 | n/a | |
---|
19 | n/a | v = EXPR |
---|
20 | n/a | v.sort() |
---|
21 | n/a | foo(v) |
---|
22 | n/a | |
---|
23 | n/a | into |
---|
24 | n/a | |
---|
25 | n/a | v = sorted(EXPR) |
---|
26 | n/a | foo(v) |
---|
27 | n/a | """ |
---|
28 | n/a | # Author: Jacques Frechet, Collin Winter |
---|
29 | n/a | |
---|
30 | n/a | # Local imports |
---|
31 | n/a | from .. import fixer_base |
---|
32 | n/a | from ..fixer_util import Call, Comma, Name, Node, BlankLine, syms |
---|
33 | n/a | |
---|
34 | n/a | CMP = "(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)" |
---|
35 | n/a | TYPE = "power< 'type' trailer< '(' x=any ')' > >" |
---|
36 | n/a | |
---|
37 | n/a | class FixIdioms(fixer_base.BaseFix): |
---|
38 | n/a | explicit = True # The user must ask for this fixer |
---|
39 | n/a | |
---|
40 | n/a | PATTERN = r""" |
---|
41 | n/a | isinstance=comparison< %s %s T=any > |
---|
42 | n/a | | |
---|
43 | n/a | isinstance=comparison< T=any %s %s > |
---|
44 | n/a | | |
---|
45 | n/a | while_stmt< 'while' while='1' ':' any+ > |
---|
46 | n/a | | |
---|
47 | n/a | sorted=any< |
---|
48 | n/a | any* |
---|
49 | n/a | simple_stmt< |
---|
50 | n/a | expr_stmt< id1=any '=' |
---|
51 | n/a | power< list='list' trailer< '(' (not arglist<any+>) any ')' > > |
---|
52 | n/a | > |
---|
53 | n/a | '\n' |
---|
54 | n/a | > |
---|
55 | n/a | sort= |
---|
56 | n/a | simple_stmt< |
---|
57 | n/a | power< id2=any |
---|
58 | n/a | trailer< '.' 'sort' > trailer< '(' ')' > |
---|
59 | n/a | > |
---|
60 | n/a | '\n' |
---|
61 | n/a | > |
---|
62 | n/a | next=any* |
---|
63 | n/a | > |
---|
64 | n/a | | |
---|
65 | n/a | sorted=any< |
---|
66 | n/a | any* |
---|
67 | n/a | simple_stmt< expr_stmt< id1=any '=' expr=any > '\n' > |
---|
68 | n/a | sort= |
---|
69 | n/a | simple_stmt< |
---|
70 | n/a | power< id2=any |
---|
71 | n/a | trailer< '.' 'sort' > trailer< '(' ')' > |
---|
72 | n/a | > |
---|
73 | n/a | '\n' |
---|
74 | n/a | > |
---|
75 | n/a | next=any* |
---|
76 | n/a | > |
---|
77 | n/a | """ % (TYPE, CMP, CMP, TYPE) |
---|
78 | n/a | |
---|
79 | n/a | def match(self, node): |
---|
80 | n/a | r = super(FixIdioms, self).match(node) |
---|
81 | n/a | # If we've matched one of the sort/sorted subpatterns above, we |
---|
82 | n/a | # want to reject matches where the initial assignment and the |
---|
83 | n/a | # subsequent .sort() call involve different identifiers. |
---|
84 | n/a | if r and "sorted" in r: |
---|
85 | n/a | if r["id1"] == r["id2"]: |
---|
86 | n/a | return r |
---|
87 | n/a | return None |
---|
88 | n/a | return r |
---|
89 | n/a | |
---|
90 | n/a | def transform(self, node, results): |
---|
91 | n/a | if "isinstance" in results: |
---|
92 | n/a | return self.transform_isinstance(node, results) |
---|
93 | n/a | elif "while" in results: |
---|
94 | n/a | return self.transform_while(node, results) |
---|
95 | n/a | elif "sorted" in results: |
---|
96 | n/a | return self.transform_sort(node, results) |
---|
97 | n/a | else: |
---|
98 | n/a | raise RuntimeError("Invalid match") |
---|
99 | n/a | |
---|
100 | n/a | def transform_isinstance(self, node, results): |
---|
101 | n/a | x = results["x"].clone() # The thing inside of type() |
---|
102 | n/a | T = results["T"].clone() # The type being compared against |
---|
103 | n/a | x.prefix = "" |
---|
104 | n/a | T.prefix = " " |
---|
105 | n/a | test = Call(Name("isinstance"), [x, Comma(), T]) |
---|
106 | n/a | if "n" in results: |
---|
107 | n/a | test.prefix = " " |
---|
108 | n/a | test = Node(syms.not_test, [Name("not"), test]) |
---|
109 | n/a | test.prefix = node.prefix |
---|
110 | n/a | return test |
---|
111 | n/a | |
---|
112 | n/a | def transform_while(self, node, results): |
---|
113 | n/a | one = results["while"] |
---|
114 | n/a | one.replace(Name("True", prefix=one.prefix)) |
---|
115 | n/a | |
---|
116 | n/a | def transform_sort(self, node, results): |
---|
117 | n/a | sort_stmt = results["sort"] |
---|
118 | n/a | next_stmt = results["next"] |
---|
119 | n/a | list_call = results.get("list") |
---|
120 | n/a | simple_expr = results.get("expr") |
---|
121 | n/a | |
---|
122 | n/a | if list_call: |
---|
123 | n/a | list_call.replace(Name("sorted", prefix=list_call.prefix)) |
---|
124 | n/a | elif simple_expr: |
---|
125 | n/a | new = simple_expr.clone() |
---|
126 | n/a | new.prefix = "" |
---|
127 | n/a | simple_expr.replace(Call(Name("sorted"), [new], |
---|
128 | n/a | prefix=simple_expr.prefix)) |
---|
129 | n/a | else: |
---|
130 | n/a | raise RuntimeError("should not have reached here") |
---|
131 | n/a | sort_stmt.remove() |
---|
132 | n/a | |
---|
133 | n/a | btwn = sort_stmt.prefix |
---|
134 | n/a | # Keep any prefix lines between the sort_stmt and the list_call and |
---|
135 | n/a | # shove them right after the sorted() call. |
---|
136 | n/a | if "\n" in btwn: |
---|
137 | n/a | if next_stmt: |
---|
138 | n/a | # The new prefix should be everything from the sort_stmt's |
---|
139 | n/a | # prefix up to the last newline, then the old prefix after a new |
---|
140 | n/a | # line. |
---|
141 | n/a | prefix_lines = (btwn.rpartition("\n")[0], next_stmt[0].prefix) |
---|
142 | n/a | next_stmt[0].prefix = "\n".join(prefix_lines) |
---|
143 | n/a | else: |
---|
144 | n/a | assert list_call.parent |
---|
145 | n/a | assert list_call.next_sibling is None |
---|
146 | n/a | # Put a blank line after list_call and set its prefix. |
---|
147 | n/a | end_line = BlankLine() |
---|
148 | n/a | list_call.parent.append_child(end_line) |
---|
149 | n/a | assert list_call.next_sibling is end_line |
---|
150 | n/a | # The new prefix should be everything up to the first new line |
---|
151 | n/a | # of sort_stmt's prefix. |
---|
152 | n/a | end_line.prefix = btwn.rpartition("\n")[0] |
---|