-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
__init__.py
344 lines (311 loc) · 14 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from __future__ import annotations
import ast
import sys
from typing import Any
from typing import Generator
if sys.version_info >= (3, 8):
from importlib.metadata import version
else:
from importlib_metadata import version
class ComprehensionChecker:
"""
Flake8 plugin to help you write better list/set/dict comprehensions.
"""
name = "flake8-comprehensions"
version = version("flake8-comprehensions")
__slots__ = ("tree",)
def __init__(self, tree: ast.AST) -> None:
self.tree = tree
messages = {
"C400": "C400 Unnecessary generator - rewrite as a list comprehension.",
"C401": "C401 Unnecessary generator - rewrite as a set comprehension.",
"C402": "C402 Unnecessary generator - rewrite as a dict comprehension.",
"C403": "C403 Unnecessary list comprehension - rewrite as a set comprehension.",
"C404": (
"C404 Unnecessary list comprehension - rewrite as a dict comprehension."
),
"C405": "C405 Unnecessary {type} literal - ",
"C406": "C406 Unnecessary {type} literal - ",
"C408": "C408 Unnecessary {type} call - rewrite as a literal.",
"C409": "C409 Unnecessary {type} passed to tuple() - ",
"C410": "C410 Unnecessary {type} passed to list() - ",
"C411": "C411 Unnecessary list call - remove the outer call to list().",
"C413": "C413 Unnecessary {outer} call around {inner}(){remediation}.",
"C414": "C414 Unnecessary {inner} call within {outer}().",
"C415": "C415 Unnecessary subscript reversal of iterable within {func}().",
"C416": "C416 Unnecessary {type} comprehension - rewrite using {type}().",
"C417": "C417 Unnecessary use of map - use a {comp} instead.",
}
def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
# Stores previously seen map() nodes, to avoid raising C417 on it twice.
visited_map_calls: set[ast.Call] = set()
for node in ast.walk(self.tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
num_positional_args = len(node.args)
num_keyword_args = len(node.keywords)
if (
num_positional_args == 1
and isinstance(node.args[0], ast.GeneratorExp)
and node.func.id in ("list", "set")
):
msg_key = {"list": "C400", "set": "C401"}[node.func.id]
yield (
node.lineno,
node.col_offset,
self.messages[msg_key],
type(self),
)
elif (
num_positional_args == 1
and node.func.id == "dict"
and len(node.keywords) == 0
and isinstance(node.args[0], (ast.GeneratorExp, ast.ListComp))
and isinstance(node.args[0].elt, ast.Tuple)
and len(node.args[0].elt.elts) == 2
):
if isinstance(node.args[0], ast.GeneratorExp):
msg = "C402"
else:
msg = "C404"
yield (
node.lineno,
node.col_offset,
self.messages[msg],
type(self),
)
elif (
num_positional_args == 1
and isinstance(node.args[0], ast.ListComp)
and node.func.id in ("list", "set")
):
msg_key = {"list": "C411", "set": "C403"}[node.func.id]
yield (
node.lineno,
node.col_offset,
self.messages[msg_key],
type(self),
)
elif num_positional_args == 1 and (
isinstance(node.args[0], ast.Tuple)
and node.func.id == "tuple"
or isinstance(node.args[0], ast.List)
and node.func.id == "list"
):
suffix = "remove the outer call to {func}()."
msg_key = {"tuple": "C409", "list": "C410"}[node.func.id]
msg = self.messages[msg_key] + suffix
yield (
node.lineno,
node.col_offset,
msg.format(
type=type(node.args[0]).__name__.lower(), func=node.func.id
),
type(self),
)
elif (
num_positional_args == 1
and isinstance(node.args[0], (ast.Tuple, ast.List))
and node.func.id in ("tuple", "list", "set", "dict")
):
suffix = "rewrite as a {func} literal."
msg_key = {
"tuple": "C409",
"list": "C410",
"set": "C405",
"dict": "C406",
}[node.func.id]
msg = self.messages[msg_key] + suffix
yield (
node.lineno,
node.col_offset,
msg.format(
type=type(node.args[0]).__name__.lower(), func=node.func.id
),
type(self),
)
elif (
num_positional_args == 0
and not has_star_args(node)
and not has_double_star_args(node)
and node.func.id == "dict"
):
yield (
node.lineno,
node.col_offset,
self.messages["C408"].format(type=node.func.id),
type(self),
)
elif (
num_positional_args == 0
and num_keyword_args == 0
and node.func.id in ("tuple", "list")
):
yield (
node.lineno,
node.col_offset,
self.messages["C408"].format(type=node.func.id),
type(self),
)
elif (
node.func.id in {"list", "reversed"}
and num_positional_args > 0
and isinstance(node.args[0], ast.Call)
and isinstance(node.args[0].func, ast.Name)
and node.args[0].func.id == "sorted"
):
remediation = ""
if node.func.id == "reversed":
reverse_flag_value: bool | None = False
for keyword in node.args[0].keywords:
if keyword.arg != "reverse":
continue
if isinstance(keyword.value, ast.NameConstant):
reverse_flag_value = keyword.value.value
elif isinstance(keyword.value, ast.Num):
reverse_flag_value = bool(keyword.value.n)
else:
# Complex value
reverse_flag_value = None
if reverse_flag_value is None:
remediation = " - toggle reverse argument to sorted()"
else:
remediation = " - use sorted(..., reverse={!r})".format(
not reverse_flag_value
)
msg = self.messages["C413"].format(
inner=node.args[0].func.id,
outer=node.func.id,
remediation=remediation,
)
yield (
node.lineno,
node.col_offset,
msg,
type(self),
)
elif (
num_positional_args > 0
and isinstance(node.args[0], ast.Call)
and isinstance(node.args[0].func, ast.Name)
and (
(
node.func.id in {"set", "sorted"}
and node.args[0].func.id
in {"list", "reversed", "sorted", "tuple"}
)
or (
node.func.id in {"list", "tuple"}
and node.args[0].func.id in {"list", "tuple"}
)
or (node.func.id == "set" and node.args[0].func.id == "set")
)
):
yield (
node.lineno,
node.col_offset,
self.messages["C414"].format(
inner=node.args[0].func.id, outer=node.func.id
),
type(self),
)
elif (
node.func.id in {"reversed", "set", "sorted"}
and num_positional_args > 0
and isinstance(node.args[0], ast.Subscript)
and isinstance(node.args[0].slice, ast.Slice)
and node.args[0].slice.lower is None
and node.args[0].slice.upper is None
and isinstance(node.args[0].slice.step, ast.UnaryOp)
and isinstance(node.args[0].slice.step.op, ast.USub)
and isinstance(node.args[0].slice.step.operand, ast.Num)
and node.args[0].slice.step.operand.n == 1
):
yield (
node.lineno,
node.col_offset,
self.messages["C415"].format(func=node.func.id),
type(self),
)
elif (
node.func.id == "map"
and node not in visited_map_calls
and len(node.args) == 2
and isinstance(node.args[0], ast.Lambda)
):
yield (
node.lineno,
node.col_offset,
self.messages["C417"].format(comp="generator expression"),
type(self),
)
elif (
node.func.id in ("list", "set", "dict")
and len(node.args) == 1
and isinstance(node.args[0], ast.Call)
and isinstance(node.args[0].func, ast.Name)
and node.args[0].func.id == "map"
and len(node.args[0].args) == 2
and isinstance(node.args[0].args[0], ast.Lambda)
):
# To avoid raising C417 on the map() call inside the list/set/dict.
map_call = node.args[0]
visited_map_calls.add(map_call)
rewriteable = True
if node.func.id == "dict":
# For the generator expression to be rewriteable as a
# dict comprehension, its lambda must return a 2-tuple.
lambda_node = node.args[0].args[0]
if (
not isinstance(lambda_node.body, (ast.List, ast.Tuple))
or len(lambda_node.body.elts) != 2
):
rewriteable = False
if rewriteable:
comprehension_type = f"{node.func.id} comprehension"
yield (
node.lineno,
node.col_offset,
self.messages["C417"].format(comp=comprehension_type),
type(self),
)
elif isinstance(node, (ast.DictComp, ast.ListComp, ast.SetComp)):
if (
len(node.generators) == 1
and not node.generators[0].ifs
and not node.generators[0].is_async
and (
(
isinstance(node, (ast.ListComp, ast.SetComp))
and isinstance(node.elt, ast.Name)
and isinstance(node.generators[0].target, ast.Name)
and node.elt.id == node.generators[0].target.id
)
or (
isinstance(node, ast.DictComp)
and isinstance(node.key, ast.Name)
and isinstance(node.value, ast.Name)
and isinstance(node.generators[0].target, ast.Tuple)
and tuple(
x.id
for x in node.generators[0].target.elts
if isinstance(x, ast.Name)
)
== (node.key.id, node.value.id)
)
)
):
yield (
node.lineno,
node.col_offset,
self.messages["C416"].format(type=comp_type[node.__class__]),
type(self),
)
def has_star_args(call_node: ast.Call) -> bool:
return any(isinstance(a, ast.Starred) for a in call_node.args)
def has_double_star_args(call_node: ast.Call) -> bool:
return any(k.arg is None for k in call_node.keywords)
comp_type = {
ast.DictComp: "dict",
ast.ListComp: "list",
ast.SetComp: "set",
}