-
Notifications
You must be signed in to change notification settings - Fork 19
/
ast_for.py
207 lines (186 loc) · 6.01 KB
/
ast_for.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
# Core Library
import ast
from typing import List, Tuple
# First party
from flake8_simplify.constants import BOOL_CONST_TYPES
from flake8_simplify.utils import (
For,
body_contains_continue,
is_constant_increase,
to_source,
)
def get_sim104(node: ast.For) -> List[Tuple[int, int, str]]:
"""
Get a list of all "iterate and yield" patterns.
for item in iterable:
yield item
which is
For(
target=Name(id='item', ctx=Store()),
iter=Name(id='iterable', ctx=Load()),
body=[
Expr(
value=Yield(
value=Name(id='item', ctx=Load()),
),
),
],
orelse=[],
type_comment=None,
),
"""
RULE = "SIM104 Use 'yield from {iterable}'"
errors: List[Tuple[int, int, str]] = []
if (
len(node.body) != 1
or not isinstance(node.body[0], ast.Expr)
or not isinstance(node.body[0].value, ast.Yield)
or not isinstance(node.target, ast.Name)
or not isinstance(node.body[0].value.value, ast.Name)
or node.target.id != node.body[0].value.value.id
or node.orelse != []
):
return errors
parent = getattr(node, "parent", None)
while (parent
and hasattr(parent, "parent")
and parent.parent is not parent
and not isinstance(parent, ast.AsyncFunctionDef)):
parent = getattr(parent, "parent", None)
if isinstance(parent, ast.AsyncFunctionDef): # type: ignore
return errors
iterable = to_source(node.iter)
errors.append(
(node.lineno, node.col_offset, RULE.format(iterable=iterable))
)
return errors
def get_sim110_sim111(node: ast.For) -> List[Tuple[int, int, str]]:
"""
Check if any / all could be used.
For(
target=Name(id='x', ctx=Store()),
iter=Name(id='iterable', ctx=Load()),
body=[
If(
test=Call(
func=Name(id='check', ctx=Load()),
args=[Name(id='x', ctx=Load())],
keywords=[],
),
body=[
Return(
value=Constant(value=True, kind=None),
),
],
orelse=[],
),
],
orelse=[],
type_comment=None,
),
Return(value=Constant(value=False, kind=None))
"""
SIM110 = "SIM110 Use 'return any({check} for {target} in {iterable})'"
SIM111 = "SIM111 Use 'return all({check} for {target} in {iterable})'"
errors: List[Tuple[int, int, str]] = []
if not (
len(node.body) == 1
and isinstance(node.body[0], ast.If)
and len(node.body[0].body) == 1
and isinstance(node.body[0].body[0], ast.Return)
and isinstance(node.body[0].body[0].value, BOOL_CONST_TYPES)
):
return errors
if not hasattr(node.body[0].body[0].value, "value"):
return errors
if not isinstance(node.next_sibling, ast.Return): # type: ignore
return errors
check = to_source(node.body[0].test)
target = to_source(node.target)
iterable = to_source(node.iter)
if node.body[0].body[0].value.value is True:
errors.append(
(
node.lineno,
node.col_offset,
SIM110.format(check=check, target=target, iterable=iterable),
)
)
elif node.body[0].body[0].value.value is False:
is_compound_expression = " and " in check or " or " in check
if is_compound_expression:
check = f"not ({check})"
else:
if check.startswith("not "):
check = check[len("not ") :]
else:
check = f"not {check}"
errors.append(
(
node.lineno,
node.col_offset,
SIM111.format(check=check, target=target, iterable=iterable),
)
)
return errors
def get_sim113(node: For) -> List[Tuple[int, int, str]]:
"""
Find loops in which "enumerate" should be used.
For(
target=Name(id='el', ctx=Store()),
iter=Name(id='iterable', ctx=Load()),
body=[
Expr(
value=Constant(value=Ellipsis, kind=None),
),
AugAssign( -- argument and assign, aka "+= 1"
target=Name(id='idx', ctx=Store()),
op=Add(),
value=Constant(value=1, kind=None),
),
],
orelse=[],
type_comment=None,
),
"""
errors: List[Tuple[int, int, str]] = []
variable_candidates = []
if body_contains_continue(node.body):
return errors
# Find variables that might just count the iteration of the current loop
for expression in node.body:
if (
isinstance(expression, ast.AugAssign)
and is_constant_increase(expression)
and isinstance(expression.target, ast.Name)
):
variable_candidates.append(expression.target)
str_candidates = [to_source(x) for x in variable_candidates]
older_siblings = []
for older_sibling in node.parent.body: # type: ignore
if older_sibling is node:
break
older_siblings.append(older_sibling)
matches = [
n.targets[0]
for n in older_siblings
if isinstance(n, ast.Assign)
and len(n.targets) == 1
and isinstance(n.targets[0], ast.Name)
and to_source(n.targets[0]) in str_candidates
]
if len(matches) == 0:
return errors
sibling = node.previous_sibling
while sibling is not None:
sibling = sibling.previous_sibling
for match in matches:
variable = to_source(match)
errors.append(
(
match.lineno,
match.col_offset,
f"SIM113 Use enumerate for '{variable}'",
)
)
return errors