-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
versioned_branches.py
238 lines (215 loc) · 8.13 KB
/
versioned_branches.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
import ast
from typing import cast
from typing import Iterable
from typing import List
from typing import Tuple
from typing import Type
from typing import Union
from tokenize_rt import Offset
from tokenize_rt import Token
from pyupgrade._ast_helpers import ast_to_offset
from pyupgrade._ast_helpers import is_name_attr
from pyupgrade._data import register
from pyupgrade._data import State
from pyupgrade._data import TokenFunc
from pyupgrade._token_helpers import Block
def _find_if_else_block(tokens: List[Token], i: int) -> Tuple[Block, Block]:
if_block = Block.find(tokens, i)
i = if_block.end
while tokens[i].src != 'else':
i += 1
else_block = Block.find(tokens, i, trim_end=True)
return if_block, else_block
def _find_elif(tokens: List[Token], i: int) -> int:
while tokens[i].src != 'elif': # pragma: no cover (only for <3.8.1)
i -= 1
return i
def _fix_py3_block(i: int, tokens: List[Token]) -> None:
if tokens[i].src == 'if':
if_block = Block.find(tokens, i)
if_block.dedent(tokens)
del tokens[if_block.start:if_block.block]
else:
if_block = Block.find(tokens, _find_elif(tokens, i))
if_block.replace_condition(tokens, [Token('NAME', 'else')])
def _fix_py2_block(i: int, tokens: List[Token]) -> None:
if tokens[i].src == 'if':
if_block, else_block = _find_if_else_block(tokens, i)
else_block.dedent(tokens)
del tokens[if_block.start:else_block.block]
else:
j = _find_elif(tokens, i)
if_block, else_block = _find_if_else_block(tokens, j)
del tokens[if_block.start:else_block.start]
def _fix_py3_block_else(i: int, tokens: List[Token]) -> None:
if tokens[i].src == 'if':
if_block, else_block = _find_if_else_block(tokens, i)
if_block.dedent(tokens)
del tokens[if_block.end:else_block.end]
del tokens[if_block.start:if_block.block]
else:
j = _find_elif(tokens, i)
if_block, else_block = _find_if_else_block(tokens, j)
del tokens[if_block.end:else_block.end]
if_block.replace_condition(tokens, [Token('NAME', 'else')])
def _eq(test: ast.Compare, n: int) -> bool:
return (
isinstance(test.ops[0], ast.Eq) and
isinstance(test.comparators[0], ast.Num) and
test.comparators[0].n == n
)
def _compare_to_3(
test: ast.Compare,
op: Union[Type[ast.cmpop], Tuple[Type[ast.cmpop], ...]],
minor: int = 0,
) -> bool:
min_len = 2 if minor else 1
if not (
isinstance(test.ops[0], op) and
isinstance(test.comparators[0], ast.Tuple) and
len(test.comparators[0].elts) >= min_len and
all(isinstance(n, ast.Num) for n in test.comparators[0].elts)
):
return False
# checked above but mypy needs help
elts = cast('List[ast.Num]', test.comparators[0].elts)
retv = elts[0].n == 3
offset = 1
if minor:
retv &= elts[1].n == minor
offset += 1
retv &= all(n.n == 0 for n in elts[offset:])
return retv
@register(ast.If)
def visit_If(
state: State,
node: ast.If,
parent: ast.AST,
) -> Iterable[Tuple[Offset, TokenFunc]]:
if state.settings.min_version >= (3,) and (
len(state.settings.min_version) >= 2
):
py3_minor = state.settings.min_version[1]
if (
state.settings.min_version >= (3,) and (
# if six.PY2:
is_name_attr(node.test, state.from_imports, 'six', ('PY2',)) or
# if not six.PY3:
(
isinstance(node.test, ast.UnaryOp) and
isinstance(node.test.op, ast.Not) and
is_name_attr(
node.test.operand,
state.from_imports,
'six',
('PY3',),
)
) or
# sys.version_info == 2 or < (3,)
(
isinstance(node.test, ast.Compare) and
is_name_attr(
node.test.left,
state.from_imports,
'sys',
('version_info',),
) and
len(node.test.ops) == 1 and (
_eq(node.test, 2) or
_compare_to_3(node.test, ast.Lt)
)
) or
# sys.version_info < (3, n) (with n<=m)
# or sys.version_info <= (3, n) (with n<m)
(
len(state.settings.min_version) >= 2 and (
isinstance(node.test, ast.Compare) and
is_name_attr(
node.test.left,
state.from_imports,
'sys',
('version_info',),
) and
len(node.test.ops) == 1 and (
_compare_to_3(
node.test,
ast.Lt,
minor=py3_minor,
) or any(
_compare_to_3(
node.test,
(ast.Lt, ast.LtE),
minor=minor,
)
for minor in range(py3_minor)
)
)
)
)
)
):
if node.orelse and not isinstance(node.orelse[0], ast.If):
yield ast_to_offset(node), _fix_py2_block
elif (
state.settings.min_version >= (3,) and (
# if six.PY3:
is_name_attr(node.test, state.from_imports, 'six', ('PY3',)) or
# if not six.PY2:
(
isinstance(node.test, ast.UnaryOp) and
isinstance(node.test.op, ast.Not) and
is_name_attr(
node.test.operand,
state.from_imports,
'six',
('PY2',),
)
) or
# sys.version_info == 3 or >= (3,) or > (3,)
(
isinstance(node.test, ast.Compare) and
is_name_attr(
node.test.left,
state.from_imports,
'sys',
('version_info',),
) and
len(node.test.ops) == 1 and (
_eq(node.test, 3) or
_compare_to_3(node.test, (ast.Gt, ast.GtE))
)
) or
# sys.version_info >= (3, n) (with n<=m)
# or sys.version_info > (3, n) (with n<m)
(
len(state.settings.min_version) >= 2 and (
isinstance(node.test, ast.Compare) and
is_name_attr(
node.test.left,
state.from_imports,
'sys',
('version_info',),
) and
len(node.test.ops) == 1 and
(
_compare_to_3(
node.test,
ast.GtE,
minor=py3_minor,
) or any(
_compare_to_3(
node.test,
(ast.Gt, ast.GtE),
minor=minor,
)
for minor in range(py3_minor)
)
)
)
)
)
):
if node.orelse and not isinstance(node.orelse[0], ast.If):
yield ast_to_offset(node), _fix_py3_block_else
elif not node.orelse:
yield ast_to_offset(node), _fix_py3_block