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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
# This file is part of the lib3to6 project
# https://github.com/mbarkhau/lib3to6
#
# Copyright (c) 2019-2021 Manuel Barkhau (mbarkhau@gmail.com) - MIT License
# SPDX-License-Identifier: MIT
import ast
import typing as typ
from . import common
from . import fixer_base as fb
AstStr = getattr(ast, 'Str', ast.Constant)
ArgUnpackNodes = (ast.Call, ast.List, ast.Tuple, ast.Set)
KwArgUnpackNodes = (ast.Call, ast.Dict)
def _is_dict_call(node: ast.expr) -> bool:
return isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "dict"
def _has_stararg_g12n(node: ast.expr) -> bool:
if isinstance(node, ast.Call):
elts = node.args
elif isinstance(node, (ast.List, ast.Tuple, ast.Set)):
elts = node.elts
else:
raise TypeError(f"Unexpected node: {node}")
has_starred_arg = False
for arg in elts:
# Anything after * means we have to apply the fix
if has_starred_arg:
return True
has_starred_arg = isinstance(arg, ast.Starred)
return False
def _has_starstarargs_g12n(node: ast.expr) -> bool:
if isinstance(node, ast.Call):
has_kwstarred_arg = False
for keyword in node.keywords:
if has_kwstarred_arg:
# Anything after ** means we have to apply the fix
return True
has_kwstarred_arg = keyword.arg is None
return False
elif isinstance(node, ast.Dict):
has_kwstarred_arg = False
for key in node.keys:
if has_kwstarred_arg:
# Anything after ** means we have to apply the fix
return True
has_kwstarred_arg = key is None
return False
else:
raise TypeError(f"Unexpected node: {node}")
def _node_with_elts(node: ast.AST, new_elts: typ.List[ast.expr]) -> ast.expr:
if isinstance(node, ast.Call):
node.args = new_elts
return node
elif isinstance(node, ast.List):
return ast.List(elts=new_elts)
elif isinstance(node, ast.Set):
return ast.Set(elts=new_elts)
elif isinstance(node, ast.Tuple):
return ast.Tuple(elts=new_elts)
else:
raise TypeError(f"Unexpected node type {type(node)}")
def _node_with_binop(node: ast.AST, binop: ast.BinOp) -> ast.expr:
if isinstance(node, ast.Call):
node.args = [ast.Starred(value=binop, ctx=ast.Load())]
return node
elif isinstance(node, ast.List):
# NOTE (mb 2018-06-29): Operands of the binop are always lists
return binop
elif isinstance(node, ast.Set):
return ast.Call(func=ast.Name(id="set", ctx=ast.Load()), args=[binop], keywords=[])
elif isinstance(node, ast.Tuple):
return ast.Call(func=ast.Name(id="tuple", ctx=ast.Load()), args=[binop], keywords=[])
else:
raise TypeError(f"Unexpected node type {type(node)}")
def _is_stmtlist(nodelist: typ.Any) -> bool:
return isinstance(nodelist, list) and all(isinstance(n, ast.stmt) for n in nodelist)
def _iter_walkable_fields(node: ast.AST) -> typ.Iterable[typ.Any]:
for field_name, field_node in ast.iter_fields(node):
if isinstance(field_node, ast.arguments):
continue
if isinstance(field_node, ast.expr_context):
continue
if isinstance(field_node, common.LeafNodeTypes):
continue
yield field_name, field_node
def _expand_stararg_g12n(node: ast.AST) -> ast.expr:
"""Convert fn(*x, *[1, 2], z) -> fn(*(list(x) + [1, 2, z])).
NOTE (mb 2018-07-06): The goal here is to create an expression
which is a list, by either creating
1. a single list node
2. a BinOp tree where all of the node.elts/args
are converted to lists and concatenated.
"""
if isinstance(node, ast.Call):
elts = node.args
elif isinstance(node, common.ContainerNodes):
elts = node.elts
else:
raise TypeError(f"Unexpected node: {node}")
operands: typ.List[ast.expr] = [ast.List(elts=[])]
for elt in elts:
tail_list = operands[-1]
assert isinstance(tail_list, ast.List)
tail_elts = tail_list.elts # pylint:disable=no-member; yes it does
if not isinstance(elt, ast.Starred):
# NOTE (mb 2018-07-06): Simple case, just a new
# element for right leaf: fn(*x, *[1, 2], >z<)
tail_elts.append(elt)
continue
val = elt.value
if isinstance(val, common.ContainerNodes):
# NOTE (mb 2018-07-06): Another simple case
# elements for right leaf: fn(*x,, >*[1, 2]<, z)
tail_elts.extend(val.elts)
continue
# NOTE (mb 2018-07-06): Something which we can
# be only be sure must be an iterable, so we
# call list(x) and add it in the binop tree
# elements for right leaf: fn(*>x<, *[1, 2], z)
new_val_node = ast.Call(func=ast.Name(id="list", ctx=ast.Load()), args=[val], keywords=[])
if len(tail_elts) == 0:
operands[-1] = new_val_node
else:
operands.append(new_val_node)
operands.append(ast.List(elts=[]))
tail_list = operands[-1]
assert isinstance(tail_list, ast.List)
if len(tail_list.elts) == 0: # pylint:disable=no-member; yes it does
operands = operands[:-1]
if len(operands) == 1:
tail_list = operands[0]
assert isinstance(tail_list, ast.List)
return _node_with_elts(node, tail_list.elts) # pylint:disable=no-member; yes it does
if len(operands) > 1:
binop = ast.BinOp(left=operands[0], op=ast.Add(), right=operands[1])
for operand in operands[2:]:
binop = ast.BinOp(left=binop, op=ast.Add(), right=operand)
return _node_with_binop(node, binop)
# NOTE (mb 2018-07-06): expand should not even have been
# invoked if there were no args/elts, so this signifies
# an error generating the operands or in detecting
# unpacking generalizations.
raise RuntimeError("This should not happen")
class UnpackingGeneralizationsFixer(fb.FixerBase):
version_info = common.VersionInfo(apply_since="2.0", apply_until="3.4")
def expand_starstararg_g12n(self, node: ast.expr) -> ast.expr:
chain_values: typ.List[ast.expr] = []
chain_val : ast.expr
if isinstance(node, ast.Dict):
for key, val in zip(node.keys, node.values):
if key is None:
chain_val = val
else:
chain_val = ast.Dict(keys=[key], values=[val])
chain_values.append(chain_val)
elif isinstance(node, ast.Call):
for keyword in node.keywords:
if keyword.arg is None:
chain_val = keyword.value
else:
chain_val = ast.Dict(keys=[AstStr(s=keyword.arg)], values=[keyword.value])
chain_values.append(chain_val)
else:
raise TypeError(f"Unexpected node type {node}")
# collapse consecutive Dict chain values
# [{"a": 1}, {"b": 2}] -> {"a": 1, "b": 2}
collapsed_chain_values: typ.List[ast.expr] = []
for chain_val in chain_values:
# NOTE (mb 2018-06-30): We only look at the previous
# value for a Dict, but in principle we could look
# at any value. The question is, what happens when
# the same key is assigned to multiple times. The
# behaviour of unpacking generalizations is to :
#
# raise TypeError(
# "Type object got multiple values for keyword argument '{}'"
# )
#
# One could argue therefore, that the behaviour for
# the transpiled/fixed code (which doesn't raise a
# TypeError) is undefined and we can just collapse
# all ast.Dict objects into one, letting an
# arbitrary one of the multiple values win.
if len(collapsed_chain_values) == 0:
collapsed_chain_values.append(chain_val)
else:
prev_chain_val = collapsed_chain_values[-1]
if isinstance(chain_val, ast.Dict) and isinstance(prev_chain_val, ast.Dict):
for key, val in zip(chain_val.keys, chain_val.values):
prev_chain_val.keys.append(key)
prev_chain_val.values.append(val)
else:
collapsed_chain_values.append(chain_val)
assert len(collapsed_chain_values) > 0
if len(collapsed_chain_values) == 1:
# NOTE (mb 2018-06-30): No need for itertools.chain if there's only
# a single value left after doing collapse
collapsed_chain_value = collapsed_chain_values[0]
if isinstance(node, ast.Dict):
return collapsed_chain_value
elif isinstance(node, ast.Call):
node_func = node.func
node_args = node.args
if isinstance(node_func, ast.Name) and node_func.id == 'dict':
# value_node
return collapsed_chain_value
else:
return ast.Call(
func=node_func,
args=node_args,
keywords=[ast.keyword(arg=None, value=collapsed_chain_value)],
)
else:
raise TypeError(f"Unexpected node type {node}")
else:
assert isinstance(node, ast.Call)
self.required_imports.add(common.ImportDecl("itertools", None, None))
chain_args = []
for val in chain_values:
items_func = ast.Attribute(value=val, attr='items', ctx=ast.Load())
chain_args.append(ast.Call(func=items_func, args=[], keywords=[]))
value_node = ast.Call(
func=ast.Name(id='dict', ctx=ast.Load()),
args=[
ast.Call(
func=ast.Attribute(
value=ast.Name(id='itertools', ctx=ast.Load()),
attr='chain',
ctx=ast.Load(),
),
args=chain_args,
keywords=[],
)
],
keywords=[],
)
node.keywords = [ast.keyword(arg=None, value=value_node)]
return node
def visit_expr(self, node: ast.expr) -> ast.expr:
new_node = node
if isinstance(node, ArgUnpackNodes) and _has_stararg_g12n(node):
new_node = _expand_stararg_g12n(new_node)
if isinstance(node, KwArgUnpackNodes) and _has_starstarargs_g12n(node):
new_node = self.expand_starstararg_g12n(new_node)
return new_node
def walk_stmtlist(self, stmtlist: typ.List[ast.stmt]) -> typ.List[ast.stmt]:
assert _is_stmtlist(stmtlist)
new_stmts: typ.List[ast.stmt] = []
for stmt in stmtlist:
new_stmt = self.walk_stmt(stmt)
new_stmts.append(new_stmt)
return new_stmts
def walk_node(self, node: ast.AST) -> ast.AST:
if isinstance(node, common.LeafNodeTypes):
return node
for field_name, field_node in _iter_walkable_fields(node):
if isinstance(field_node, ast.AST):
new_node = self.walk_node(field_node)
setattr(node, field_name, new_node)
elif isinstance(field_node, list):
new_field_node = []
new_sub_node: ast.AST
for sub_node in field_node:
if isinstance(sub_node, common.LeafNodeTypes):
new_sub_node = sub_node
elif isinstance(sub_node, ast.AST):
new_sub_node = self.walk_node(sub_node)
else:
new_sub_node = sub_node
new_field_node.append(new_sub_node)
setattr(node, field_name, new_field_node)
if not isinstance(node, ast.expr):
return node
new_expr_node = self.visit_expr(node)
if isinstance(new_expr_node, ast.Call):
is_single_dict_splat = (
_is_dict_call(new_expr_node)
and len(new_expr_node.args ) == 0
and len(new_expr_node.keywords) == 1
and new_expr_node.keywords[0].arg is None
)
if is_single_dict_splat:
keyword_node = new_expr_node.keywords[0]
if _is_dict_call(keyword_node.value) or isinstance(keyword_node.value, ast.Dict):
return keyword_node.value
return new_expr_node
def walk_stmt(self, node: ast.stmt) -> ast.stmt:
assert not _is_stmtlist(node)
for field_name, field_node in _iter_walkable_fields(node):
if _is_stmtlist(field_node):
old_field_nodelist = field_node
new_field_nodelist = self.walk_stmtlist(old_field_nodelist)
setattr(node, field_name, new_field_nodelist)
elif isinstance(field_node, ast.stmt):
new_stmt = self.walk_stmt(field_node)
setattr(node, field_name, new_stmt)
elif isinstance(field_node, ast.AST):
new_node = self.walk_node(field_node)
setattr(node, field_name, new_node)
elif isinstance(field_node, list):
new_field_node = []
new_sub_node: ast.AST
for sub_node in field_node:
if isinstance(sub_node, common.LeafNodeTypes):
new_sub_node = sub_node
elif isinstance(sub_node, ast.AST):
new_sub_node = self.walk_node(sub_node)
else:
new_sub_node = sub_node
new_field_node.append(new_sub_node)
setattr(node, field_name, new_field_node)
else:
continue
return node
def apply_fix(self, ctx: common.BuildContext, tree: ast.Module) -> ast.Module:
tree.body = self.walk_stmtlist(tree.body)
return tree
|