-
Notifications
You must be signed in to change notification settings - Fork 1
/
astgolfer.py
772 lines (764 loc) · 30.3 KB
/
astgolfer.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
from ast import *
from ast import _Unparser, _Precedence, _SINGLE_QUOTES, _MULTI_QUOTES, _ALL_QUOTES
from gc import get_referrers
from itertools import product
names=([*'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ']
+ [*map(''.join, product('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'))])
set_of_names = {*names}
conv_funcs = {97: ascii, 114: repr, 115: str}
class EqualWrapper:
def __init__(self, val):
self.val = val
def __eq__(self, other):
return dump(self.val) == dump(other)
class HasChanging(NodeVisitor):
def __init__(self):
self.has_changing = False
def visit(self, node: AST):
super().visit(node)
return self.has_changing
def visit_NamedExpr(self, node: NamedExpr):
self.has_changing = True
def visit_Call(self, node: Call):
self.has_changing = True
def visit_Subscript(self, node: Subscript):
self.has_changing = True
def visit_Starred(self, node: Starred):
self.has_changing = type(node.ctx) is Load
def visit_BinOp(self, node: BinOp):
self.has_changing = True
def visit_UnaryOp(self, node: UnaryOp):
self.has_changing = True
def visit_ListComp(self, node: ListComp):
self.has_changing = True
def visit_SetComp(self, node: SetComp):
self.has_changing = True
def visit_DictComp(self, node: DictComp):
self.has_changing = True
def visit_List(self, node: List):
if type(node.ctx) is Load:
for x in node.elts:
if self.visit(x):
return
def visit_Tuple(self, node: Tuple):
if type(node.ctx) is Load:
for x in node.elts:
if self.visit(x):
return
def visit_Set(self, node: Set):
for x in node.elts:
if self.visit(x):
return
def visit_Dict(self, node: Dict):
for x in node.keys:
if x is None:
self.has_changing = True
return
if self.visit(x):
return
for x in node.values:
if self.visit(x):
return
class RewriteExpression(NodeTransformer):
def __init__(self, namespace: dict = {}, use_map: bool = False):
self.namespace = namespace
self.use_map = use_map
def visit_Name(self, node: Name) -> Name:
return Name(self.namespace.get(node.id, node.id), ctx=node.ctx)
def visit_NamedExpr(self, node: NamedExpr) -> NamedExpr | Name:
if node.target.id == node.value.id:
return node.value
return node
def visit_JoinedStr(self, node: JoinedStr) -> JoinedStr | Constant:
values = node.values
for i, val in enumerate(values):
if isinstance(val, FormattedValue):
val.value = self.visit(val.value)
if isinstance(val.value, Constant):
res = conv_funcs.get(val.conversion, lambda x: x)(val.value.value)
if type(res) is not str or val.format_spec is not None:
res = res.__format__(val.format_spec or '')
values[i] = Constant(res)
i = 0
while i < (length := len(values)):
if isinstance(cur := values[i], Constant) and i + 1 < length and isinstance(nex := values[i + 1], Constant):
values[i:i+2] = [Constant(f"{cur.value}{nex.value}")]
else:
i += 1
if len(values) == 1 and isinstance(values[0], Constant):
return values[0]
return node
def visit_ListComp(self, node: ListComp) -> ListComp | List:
node.elt = self.visit(node.elt)
for i, x in enumerate(gens := node.generators):
gens[i] = self.visit(x)
if len(node.generators) == 1:
comp = node.generators[0]
if not comp.ifs and not comp.is_async:
tg = comp.target
if isinstance(el := node.elt, Name) and isinstance(tg, Name) and tg.id == el.id:
if isinstance(it := comp.iter, List):
return self.visit(it)
elif isinstance(it, Tuple):
return List(elts=[*map(self.visit, it.elts)], ctx=it.ctx)
elif isinstance(it, Dict):
return List(elts=[*map(self.visit, it.keys)], ctx=Load())
return List(
elts=[
Starred(
value=self.visit(it),
ctx=Load()
)
],
ctx=Load()
)
elif (self.use_map and isinstance(el, Call) and not el.keywords and len(el.args) == 1
and isinstance(nm := el.args[0], Name) and isinstance(tg, Name) and nm.id != el.func.id
and nm.id == tg.id):
return List(
elts=[
Starred(
value=Call(
func=Name(id='map', ctx=Load()),
args=[
el.func,
comp.iter
],
keywords=[]
),
ctx=Load()
)
],
ctx=Load()
)
return node
def visit_SetComp(self, node: SetComp) -> SetComp | Set:
node.elt = self.visit(node.elt)
for i, x in (gens := node.generators):
gens[i] = self.visit(x)
if len(node.generators) == 1:
comp = node.generators[0]
if not comp.ifs and not comp.is_async:
if isinstance(node.elt, Name) and isinstance(comp.target, Name) and comp.target.id == node.elt.id:
if isinstance(it := comp.iter, Set):
return self.visit(it)
elif isinstance(it, (List, Tuple)):
return Set(elts=[*map(self.visit, it.elts)])
elif isinstance(it, Dict):
return Set(elts=[*map(self.visit, it.keys)])
return Set(
elts=[
Starred(
value=it,
ctx=Load()
)
],
ctx=Load()
)
elif (self.use_map and isinstance(el, Call) and not el.keywords and len(el.args) == 1
and isinstance(nm := el.args[0], Name) and isinstance(tg, Name) and nm.id != el.func.id
and nm.id == tg.id):
return Set(
elts=[
Starred(
value=Call(
func=Name(id='map', ctx=Load()),
args=[
el.func,
comp.iter
],
keywords=[]
),
ctx=Load()
)
]
)
return node
def visit_BinOp(self, node: BinOp) -> AST:
node.left = left = self.visit(node.left)
node.right = right = self.visit(node.right)
if (ltyp := type(left)) is (rtyp := type(right)):
if ltyp is Constant:
return Constant(eval(unparse(node)), ctx=Load())
elif ltyp is List and (otyp := type(node.op)) is Add:
return self.visit(List(elts=left.elts + right.elts, ctx=Load()))
elif ltyp is Tuple and otyp is Add:
return self.visit(Tuple(elts=left.elts + right.elts, ctx=Load()))
elif ltyp is Set and otyp is BitOr:
return self.visit(Set(elts=left.elts + right.elts))
return node
def visit_List(self, node: List) -> List:
for i, x in enumerate(elts := node.elts):
elts[i] = memb = self.visit(x)
if isinstance(memb, Starred):
if isinstance(val := memb.value, (List, Tuple)):
elts[i:i+1] = val.elts
elif isinstance(val, Dict):
elts[i:i+1] = val.keys
if len(elts) == 1 and type(el := elts[0]) is Starred:
if (etyp := type(elv := el.value)) is ListComp:
return elv
elif etyp is GeneratorExp:
return ListComp(elt=elv.elt, generators=elv.generators)
return node
def visit_Tuple(self, node: Tuple) -> Tuple:
for i, x in enumerate(elts := node.elts):
elts[i] = memb = self.visit(x)
if isinstance(memb, Starred):
if isinstance(val := memb.value, (List, Tuple)):
elts[i:i+1] = val.elts
elif isinstance(val, Dict):
elts[i:i+1] = val.keys
return node
def visit_Set(self, node: Set) -> Set:
for i, x in enumerate(elts := node.elts):
elts[i] = memb = self.visit(x)
if isinstance(memb, Starred):
if isinstance(val := memb.value, (List, Tuple, Set)):
elts[i:i+1] = val.elts
elif isinstance(val, Dict):
elts[i:i+1] = val.keys
i = 0
while i < len(elts):
x = EqualWrapper(elts[i])
if (count := elts.count(x)) > 1:
elts.remove(x)
while (count := elts.count(x)) > 1:
elts.remove(x)
else:
i += 1
if not elts:
elts.append(
Starred(
value=Tuple(
elts=[],
ctx=Load()
),
ctx=Load()
)
)
elif len(elts) == 1 and type(el := elts[0]) is Starred:
if (etyp := type(elv := el.value)) is SetComp:
return elv
elif etyp in {ListComp, GeneratorExp}:
return SetComp(elt=elv.elt, generators=elv.generators)
return node
def visit_Dict(self, node: Dict) -> Dict:
vals = node.values
for i, x in enumerate(keys := node.keys):
keys[i] = key = self.visit(x)
vals[i] = val = self.visit(vals[i])
if key is None and isinstance(val, Dict):
keys[i:i+1] = val.keys
vals[i:i+1] = val.values
i = 0
while i < len(keys):
x = EqualWrapper(keys[i])
if x is not None and (count := keys.count(x)) > 1:
del keys[ind := keys.index(x)]
del values[ind]
while (count := keys.count(x)) > 1:
del keys[ind := keys.index(x)]
del values[ind]
else:
i += 1
if len(keys) == 1 and keys[0] is None:
if type(val := values[0]) is DictComp:
return val
return node
def visit_BoolOp(self, node: BoolOp) -> AST:
prev_haschange = False
if (otyp := type(node.op)) is And:
i = 0
while i < (length := len(vals := node.values)):
vals[i] = x = self.visit(vals[i])
if (xtyp := type(x)) is Constant and not prev_haschange:
if x.value:
if length > 1:
del vals[i]
continue
else:
del vals[i+1:]
break
elif HasChanging().visit(x):
prev_haschange = True
elif prev_haschange:
prev_haschange = False
else:
if xtyp in (List, Tuple, Set):
if x.elts:
if length > 1:
del vals[i]
continue
else:
del vals[i+1:]
break
elif xtyp is Dict:
if x.keys:
if length > 1:
del vals[i]
continue
else:
del vals[i+1:]
break
i += 1
else:
i = 0
while i < (length := len(vals := node.values)):
if i + 1 == length:
break
vals[i] = x = self.visit(vals[i])
if (xtyp := type(x)) is Constant and not prev_haschange:
if x.value:
del vals[i+1:]
break
else:
del vals[i]
continue
elif HasChanging().visit(x):
prev_haschange = True
elif prev_haschange:
prev_haschange = False
else:
if xtyp in {List, Tuple, Set}:
if x.elts:
del vals[i+1:]
break
elif length > 1:
del vals[i]
continue
elif xtyp is Dict:
if x.keys:
del vals[i+1:]
break
elif length > 1:
del vals[i]
continue
i += 1
if (length := len(node.values)) == 1:
return node.values[0]
return node
def visit_IfExp(self, node: IfExp) -> AST:
node.test = test = self.visit(node.test)
node.body = left = self.visit(node.body)
node.orelse = right = self.visit(node.orelse)
if (ttyp := type(test)) is Constant:
return (left, right)[not test.value]
elif not HasChanging().visit(test):
if ttyp in {List, Tuple, Set}:
return (left, right)[not test.elts]
elif ttyp is Dict:
return (left, right)[not test.keys]
return node
def visit_Lambda(self, node: Lambda) -> Lambda:
node_arguments = node.args
posonlyargs = []
args = []
kwonlyargs = []
d = {}
pargs, nargs, kargs = node_arguments.posonlyargs, node_arguments.args, node_arguments.kwonlyargs
names_set = set_of_names - (GetNames().visit(node)
- set([x.arg for x in pargs])
- set([x.arg for x in nargs])
- set([x.arg for x in kargs]))
i = 0
for x in pargs:
while (name := names[i]) in names_set:
i += 1
d[x.arg] = name
posonlyargs.append(arg(arg=name))
i += 1
for x in nargs:
while (name := names[i]) in names_set:
i += 1
d[x.arg] = name
args.append(arg(arg=name))
i += 1
minus = i
for x in kargs:
while (name := names[i]) in names_set:
i += 1
d[x.arg] = name
kwonlyargs.append(arg(arg=name))
i += 1
return Lambda(
args=arguments(
posonlyargs=posonlyargs,
args=args,
kwonlyargs=kwonlyargs,
kw_defaults=node_arguments.kw_defaults,
defaults=node_arguments.defaults
),
body=fix_missing_locations(RewriteExpression(d, use_map=self.use_map).visit(node.body))
)
def visit_Call(self, node: Call) -> Call:
node.func = func = self.visit(node.func)
has_map = self.use_map and isinstance(func, Name) and func.id == "map"
for i, x in enumerate(args := node.args):
args[i] = arg = self.visit(x)
if isinstance(arg, Starred):
if isinstance(val := arg.value, (List, Tuple, Set)):
args[i:i+1] = val.elts
elif isinstance(val, Dict):
args[i:i+1] = val.keys
elif has_map:
if isinstance(arg, (List, Tuple, Set)) and len(el := arg.elts) == 1 and isinstance(el := el[0], Starred):
args[i] = el.value
i = 0
kwds = node.keywords
while i < len(kwds):
kwds[i] = kwd = self.visit(kwds[i])
if kwd.arg is None and type(val := kwd.value) is Dict:
if not (values := val.values):
del kwds[i]
continue
keys = val.keys
if i > 0 and (kwd2 := kwds[i-1]).arg is None and type(val2 := kwd2.value) is Dict:
pos = i - 1
j = len(val2.keys)
keys = val2.keys + keys
val2.keys = keys
val2.values = values = val2.values + values
del kwds[i]
else:
pos = i
j = 0
while j < len(keys):
if isinstance(x := keys[j], Constant) and isinstance(val := x.value, str):
kwds.insert(pos, keyword(arg=val, value=values[j]))
del keys[j]
del values[j]
else:
j += 1
i += 1
return node
class GetNames(NodeVisitor):
def __init__(self):
self._set = {*()}
def visit(self, node: AST) -> set:
super().visit(node)
return self._set
def visit_Name(self, node: Name):
self._set.add(node.id)
class RewriteFunctions(NodeTransformer):
def __init__(self, use_map=False):
self.use_map = use_map
def visit_FunctionDef(self, node: FunctionDef) -> Assign:
node_arguments = node.args
posonlyargs = []
args = []
kwonlyargs = []
d = {}
pargs, nargs, kargs = node_arguments.posonlyargs, node_arguments.args, node_arguments.kwonlyargs
names_set = (GetNames().visit(node)
- set([x.arg for x in pargs])
- set([x.arg for x in nargs])
- set([x.arg for x in kargs]))
i = 0
for x in pargs:
while (name := names[i]) in names_set:
i += 1
d[x.arg] = name
posonlyargs.append(arg(arg=name))
i += 1
for x in nargs:
while (name := names[i]) in names_set:
i += 1
d[x.arg] = name
args.append(arg(arg=name))
i += 1
minus = i
for x in kargs:
while (name := names[i]) in names_set:
i += 1
d[x.arg] = name
kwonlyargs.append(arg(arg=name))
i += 1
return Assign(
targets=[Name(id=node.name, ctx=Store())],
value=Lambda(
args=arguments(
posonlyargs=posonlyargs,
args=args,
kwonlyargs=kwonlyargs,
kw_defaults=node_arguments.kw_defaults,
defaults=node_arguments.defaults
),
body=fix_missing_locations(RewriteExpression(d, use_map=self.use_map).visit(node.body[0].value))
)
)
class GolfUnparser(_Unparser):
def items_view(self, traverser, items):
"""Traverse and separate the given *items* with a comma and append it to
the buffer. If *items* is a single item sequence, a trailing comma
will be added."""
if len(items) == 1:
traverser(items[0])
self.write(",")
else:
self.interleave(lambda: self.write(","), traverser, items)
def visit_NamedExpr(self, node):
with self.require_parens(_Precedence.TUPLE, node):
self.set_precedence(_Precedence.ATOM, node.target, node.value)
self.traverse(node.target)
self.write(":=")
self.traverse(node.value)
def visit_Assign(self, node):
self.fill()
for target in node.targets:
self.traverse(target)
self.write("=")
self.traverse(node.value)
if type_comment := self.get_type_comment(node):
self.write(type_comment)
def visit_AugAssign(self, node):
self.fill()
self.traverse(node.target)
self.write(f"{self.binop[node.op.__class__.__name__]}=")
self.traverse(node.value)
def visit_AnnAssign(self, node):
self.fill()
with self.delimit_if("(", ")", not node.simple and isinstance(node.target, Name)):
self.traverse(node.target)
self.write(":")
self.traverse(node.annotation)
if node.value:
self.write("=")
self.traverse(node.value)
def visit_List(self, node):
with self.delimit("[", "]"):
self.interleave(lambda: self.write(","), self.traverse, node.elts)
def visit_DictComp(self, node):
with self.delimit("{", "}"):
self.traverse(node.key)
self.write(":")
self.traverse(node.value)
for gen in node.generators:
self.traverse(gen)
def visit_Set(self, node):
if node.elts:
with self.delimit("{", "}"):
self.interleave(lambda: self.write(","), self.traverse, node.elts)
else:
# `{}` would be interpreted as a dictionary literal, and
# `set` might be shadowed. Thus:
self.write('{*()}')
def visit_comprehension(self, node):
if node.is_async:
if self._source[-1][-1] not in {']',')','}','"',"'"}:
self.write(" ")
self.write("async for")
else:
if self._source[-1][-1] not in {']',')','}','"',"'"}:
self.write(" ")
self.write("for")
self.set_precedence(_Precedence.TUPLE, node.target)
if (cond := isinstance(node.target, Name)):
self.write(" ")
self.traverse(node.target)
if cond:
self.write(" ")
self.write("in")
inner_src = []
old_write = self.write
self.write = alt_write = lambda t: inner_src.append(t)
self.set_precedence(_Precedence.TEST.next(), node.iter, *node.ifs)
self.traverse(node.iter)
if inner_src[0][0] not in {'[','(','{','"',"'"}:
old_write(" ")
if not node.ifs:
self._source.extend(inner_src)
else:
for if_clause in node.ifs:
if inner_src[-1][-1] not in {']', ')', '}', '"', "'"}:
alt_write(" ")
alt_write("if")
self._source.extend(inner_src)
self.traverse(if_clause)
if inner_src[0][0] not in {'[','(','{','"',"'"}:
old_write(" ")
self.write = old_write
def visit_IfExp(self, node):
with self.require_parens(_Precedence.TEST, node):
self.set_precedence(_Precedence.TEST.next(), node.body, node.test)
self.traverse(node.body)
if self._source[-1][-1] not in {']', ')', '}', '"', "'"}:
self.write(" ")
self.write("if")
inner_src = []
old_write = self.write
self.write = alt_write = lambda t: inner_src.append(t)
self.traverse(node.test)
if inner_src[0][0] not in {'[','(','{','"',"'"}:
old_write(" ")
self._source.extend(inner_src)
if inner_src[-1][-1] not in {']', ')', '}', '"', "'"}:
old_write(" ")
inner_src.clear()
old_write("else")
self.set_precedence(_Precedence.TEST, node.orelse)
self.traverse(node.orelse)
if inner_src[0][0] not in {'[','(','{','"',"'"}:
old_write(" ")
self._source.extend(inner_src)
self.write = old_write
def visit_Dict(self, node):
def write_key_value_pair(k, v):
self.traverse(k)
self.write(":")
self.traverse(v)
def write_item(item):
k, v = item
if k is None:
# for dictionary unpacking operator in dicts {**{'y': 2}}
# see PEP 448 for details
self.write("**")
self.set_precedence(_Precedence.EXPR, v)
self.traverse(v)
else:
write_key_value_pair(k, v)
with self.delimit("{", "}"):
self.interleave(
lambda: self.write(","), write_item, zip(node.keys, node.values)
)
def visit_Tuple(self, node):
with self.delimit("(", ")"):
self.items_view(self.traverse, node.elts)
def visit_BinOp(self, node):
operator = self.binop[node.op.__class__.__name__]
operator_precedence = self.binop_precedence[operator]
with self.require_parens(operator_precedence, node):
if operator in self.binop_rassoc:
left_precedence = operator_precedence.next()
right_precedence = operator_precedence
else:
left_precedence = operator_precedence
right_precedence = operator_precedence.next()
self.set_precedence(left_precedence, node.left)
self.traverse(node.left)
self.write(operator)
self.set_precedence(right_precedence, node.right)
self.traverse(node.right)
def visit_Compare(self, node):
with self.require_parens(_Precedence.CMP, node):
self.set_precedence(_Precedence.CMP.next(), node.left, *node.comparators)
self.traverse(node.left)
for o, e in zip(node.ops, node.comparators):
self.write(self.cmpops[o.__class__.__name__])
self.traverse(e)
def visit_BoolOp(self, node):
operator = self.boolops[node.op.__class__.__name__]
operator_precedence = self.boolop_precedence[operator]
def increasing_level_traverse(node):
self.set_precedence(operator_precedence, node)
self.traverse(node)
with self.require_parens(operator_precedence, node):
s_before = f" {operator}"
seq = iter(node.values)
try:
increasing_level_traverse(next(seq))
except StopIteration:
pass
else:
for x in seq:
if self._source[-1][-1] not in {']', ')', '}', '"', "'"}:
self.write(" ")
self.write(operator)
old_write = self.write
inner_src = []
self.write = lambda t: inner_src.append(t)
increasing_level_traverse(x)
self.write = old_write
if inner_src[0][0] not in {'[', '(', '{', '"', "'"}:
self.write(" ")
self._source.extend(inner_src)
def visit_Call(self, node):
self.set_precedence(_Precedence.ATOM, node.func)
self.traverse(node.func)
with self.delimit("(", ")"):
comma = False
for e in node.args:
if comma:
self.write(",")
else:
comma = True
self.traverse(e)
if node.keywords:
pe = node.keywords[0]
if comma:
self.write(',')
else:
comma = True
self.traverse(pe)
for e in node.keywords[1:]:
if not pe.arg and not e.arg:
self.write("|")
pe = e
e = e.value
else:
pe = e
if comma:
self.write(",")
else:
comma = True
self.traverse(e)
def visit_arg(self, node):
self.write(node.arg)
if node.annotation:
self.write(":")
self.traverse(node.annotation)
def visit_arguments(self, node):
first = True
# normal arguments
all_args = node.posonlyargs + node.args
defaults = [None] * (len(all_args) - len(node.defaults)) + node.defaults
for index, elements in enumerate(zip(all_args, defaults), 1):
a, d = elements
if first:
first = False
else:
self.write(",")
self.traverse(a)
if d:
self.write("=")
self.traverse(d)
if index == len(node.posonlyargs):
self.write(",/")
# varargs, or bare '*' if no varargs but keyword-only arguments present
if node.vararg or node.kwonlyargs:
if first:
first = False
else:
self.write(",")
self.write("*")
if node.vararg:
self.write(node.vararg.arg)
if node.vararg.annotation:
self.write(":")
self.traverse(node.vararg.annotation)
# keyword-only arguments
if node.kwonlyargs:
for a, d in zip(node.kwonlyargs, node.kw_defaults):
self.write(",")
self.traverse(a)
if d:
self.write("=")
self.traverse(d)
# kwargs
if node.kwarg:
if first:
first = False
else:
self.write(",")
self.write("**" + node.kwarg.arg)
if node.kwarg.annotation:
self.write(":")
self.traverse(node.kwarg.annotation)
def visit_Lambda(self, node):
with self.require_parens(_Precedence.TEST, node):
self.write("lambda ")
self.traverse(node.args)
self.write(":")
self.set_precedence(_Precedence.TEST, node.body)
self.traverse(node.body)