-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
pyupgrade.py
1380 lines (1134 loc) · 43.3 KB
/
pyupgrade.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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
from __future__ import unicode_literals
import argparse
import ast
import collections
import io
import keyword
import re
import string
import sys
import tokenize
import warnings
from tokenize_rt import ESCAPED_NL
from tokenize_rt import Offset
from tokenize_rt import reversed_enumerate
from tokenize_rt import src_to_tokens
from tokenize_rt import Token
from tokenize_rt import tokens_to_src
from tokenize_rt import UNIMPORTANT_WS
_stdlib_parse_format = string.Formatter().parse
def parse_format(s):
"""Makes the empty string not a special case. In the stdlib, there's
loss of information (the type) on the empty string.
"""
parsed = tuple(_stdlib_parse_format(s))
if not parsed:
return ((s, None, None, None),)
else:
return parsed
def unparse_parsed_string(parsed):
stype = type(parsed[0][0])
j = stype()
def _convert_tup(tup):
ret, field_name, format_spec, conversion = tup
ret = ret.replace(stype('{'), stype('{{'))
ret = ret.replace(stype('}'), stype('}}'))
if field_name is not None:
ret += stype('{') + field_name
if conversion:
ret += stype('!') + conversion
if format_spec:
ret += stype(':') + format_spec
ret += stype('}')
return ret
return j.join(_convert_tup(tup) for tup in parsed)
NON_CODING_TOKENS = frozenset(('COMMENT', ESCAPED_NL, 'NL', UNIMPORTANT_WS))
def _ast_to_offset(node):
return Offset(node.lineno, node.col_offset)
def ast_parse(contents_text):
# intentionally ignore warnings, we might be fixing warning-ridden syntax
with warnings.catch_warnings():
warnings.simplefilter('ignore')
return ast.parse(contents_text.encode('UTF-8'))
def inty(s):
try:
int(s)
return True
except (ValueError, TypeError):
return False
def _rewrite_string_literal(literal):
try:
parsed_fmt = parse_format(literal)
except ValueError:
# Well, the format literal was malformed, so skip it
return literal
last_int = -1
# The last segment will always be the end of the string and not a format
# We slice it off here to avoid a "None" format key
for _, fmtkey, spec, _ in parsed_fmt[:-1]:
if inty(fmtkey) and int(fmtkey) == last_int + 1 and '{' not in spec:
last_int += 1
else:
return literal
def _remove_fmt(tup):
if tup[1] is None:
return tup
else:
return (tup[0], '', tup[2], tup[3])
removed = [_remove_fmt(tup) for tup in parsed_fmt]
return unparse_parsed_string(removed)
def _fix_format_literals(contents_text):
tokens = src_to_tokens(contents_text)
to_replace = []
string_start = None
string_end = None
seen_dot = False
for i, token in enumerate(tokens):
if string_start is None and token.name == 'STRING':
string_start = i
string_end = i + 1
elif string_start is not None and token.name == 'STRING':
string_end = i + 1
elif string_start is not None and token.src == '.':
seen_dot = True
elif seen_dot and token.src == 'format':
to_replace.append((string_start, string_end))
string_start, string_end, seen_dot = None, None, False
elif token.name not in NON_CODING_TOKENS:
string_start, string_end, seen_dot = None, None, False
for start, end in reversed(to_replace):
src = tokens_to_src(tokens[start:end])
new_src = _rewrite_string_literal(src)
tokens[start:end] = [Token('STRING', new_src)]
return tokens_to_src(tokens)
def _has_kwargs(call):
return bool(call.keywords) or bool(getattr(call, 'kwargs', None))
BRACES = {'(': ')', '[': ']', '{': '}'}
SET_TRANSFORM = (ast.List, ast.ListComp, ast.GeneratorExp, ast.Tuple)
def _is_wtf(func, tokens, i):
return tokens[i].src != func or tokens[i + 1].src != '('
def _process_set_empty_literal(tokens, start):
if _is_wtf('set', tokens, start):
return
i = start + 2
brace_stack = ['(']
while brace_stack:
token = tokens[i].src
if token == BRACES[brace_stack[-1]]:
brace_stack.pop()
elif token in BRACES:
brace_stack.append(token)
elif '\n' in token:
# Contains a newline, could cause a SyntaxError, bail
return
i += 1
# Remove the inner tokens
del tokens[start + 2:i - 1]
def _search_until(tokens, idx, arg):
while (
idx < len(tokens) and
not (
tokens[idx].line == arg.lineno and
tokens[idx].utf8_byte_offset == arg.col_offset
)
):
idx += 1
return idx
if sys.version_info >= (3, 8): # pragma: no cover (py38+)
# python 3.8 fixed the offsets of generators / tuples
def _arg_token_index(tokens, i, arg):
idx = _search_until(tokens, i, arg) + 1
while idx < len(tokens) and tokens[idx].name in NON_CODING_TOKENS:
idx += 1
return idx
else: # pragma: no cover (<py38)
def _arg_token_index(tokens, i, arg):
# lists containing non-tuples report the first element correctly
if isinstance(arg, ast.List):
# If the first element is a tuple, the ast lies to us about its col
# offset. We must find the first `(` token after the start of the
# list element.
if isinstance(arg.elts[0], ast.Tuple):
i = _search_until(tokens, i, arg)
while tokens[i].src != '(':
i += 1
return i
else:
return _search_until(tokens, i, arg.elts[0])
# others' start position points at their first child node already
else:
return _search_until(tokens, i, arg)
Victims = collections.namedtuple(
'Victims', ('starts', 'ends', 'first_comma_index', 'arg_index'),
)
def _victims(tokens, start, arg, gen):
starts = [start]
start_depths = [1]
ends = []
first_comma_index = None
arg_depth = None
arg_index = _arg_token_index(tokens, start, arg)
brace_stack = [tokens[start].src]
i = start + 1
while brace_stack:
token = tokens[i].src
is_start_brace = token in BRACES
is_end_brace = token == BRACES[brace_stack[-1]]
if i == arg_index:
arg_depth = len(brace_stack)
if is_start_brace:
brace_stack.append(token)
# Remove all braces before the first element of the inner
# comprehension's target.
if is_start_brace and arg_depth is None:
start_depths.append(len(brace_stack))
starts.append(i)
if (
token == ',' and
len(brace_stack) == arg_depth and
first_comma_index is None
):
first_comma_index = i
if is_end_brace and len(brace_stack) in start_depths:
if tokens[i - 2].src == ',' and tokens[i - 1].src == ' ':
ends.extend((i - 2, i - 1, i))
elif tokens[i - 1].src == ',':
ends.extend((i - 1, i))
else:
ends.append(i)
if is_end_brace:
brace_stack.pop()
i += 1
# May need to remove a trailing comma for a comprehension
if gen:
i -= 2
while tokens[i].name in NON_CODING_TOKENS:
i -= 1
if tokens[i].src == ',':
ends = sorted(set(ends + [i]))
return Victims(starts, ends, first_comma_index, arg_index)
def _is_on_a_line_by_self(tokens, i):
return (
tokens[i - 2].name == 'NL' and
tokens[i - 1].name == UNIMPORTANT_WS and
tokens[i - 1].src.isspace() and
tokens[i + 1].name == 'NL'
)
def _remove_brace(tokens, i):
if _is_on_a_line_by_self(tokens, i):
del tokens[i - 1:i + 2]
else:
del tokens[i]
def _process_set_literal(tokens, start, arg):
if _is_wtf('set', tokens, start):
return
gen = isinstance(arg, ast.GeneratorExp)
set_victims = _victims(tokens, start + 1, arg, gen=gen)
del set_victims.starts[0]
end_index = set_victims.ends.pop()
tokens[end_index] = Token('OP', '}')
for index in reversed(set_victims.starts + set_victims.ends):
_remove_brace(tokens, index)
tokens[start:start + 2] = [Token('OP', '{')]
def _process_dict_comp(tokens, start, arg):
if _is_wtf('dict', tokens, start):
return
dict_victims = _victims(tokens, start + 1, arg, gen=True)
elt_victims = _victims(tokens, dict_victims.arg_index, arg.elt, gen=True)
del dict_victims.starts[0]
end_index = dict_victims.ends.pop()
tokens[end_index] = Token('OP', '}')
for index in reversed(dict_victims.ends):
_remove_brace(tokens, index)
# See #6, Fix SyntaxError from rewriting dict((a, b)for a, b in y)
if tokens[elt_victims.ends[-1] + 1].src == 'for':
tokens.insert(elt_victims.ends[-1] + 1, Token(UNIMPORTANT_WS, ' '))
for index in reversed(elt_victims.ends):
_remove_brace(tokens, index)
tokens[elt_victims.first_comma_index] = Token('OP', ':')
for index in reversed(dict_victims.starts + elt_victims.starts):
_remove_brace(tokens, index)
tokens[start:start + 2] = [Token('OP', '{')]
class FindDictsSetsVisitor(ast.NodeVisitor):
def __init__(self):
self.dicts = {}
self.sets = {}
self.set_empty_literals = {}
def visit_Call(self, node):
if (
isinstance(node.func, ast.Name) and
node.func.id == 'set' and
len(node.args) == 1 and
not _has_kwargs(node) and
isinstance(node.args[0], SET_TRANSFORM)
):
arg, = node.args
key = _ast_to_offset(node.func)
if isinstance(arg, (ast.List, ast.Tuple)) and not arg.elts:
self.set_empty_literals[key] = arg
else:
self.sets[key] = arg
elif (
isinstance(node.func, ast.Name) and
node.func.id == 'dict' and
len(node.args) == 1 and
not _has_kwargs(node) and
isinstance(node.args[0], (ast.ListComp, ast.GeneratorExp)) and
isinstance(node.args[0].elt, (ast.Tuple, ast.List)) and
len(node.args[0].elt.elts) == 2
):
arg, = node.args
self.dicts[_ast_to_offset(node.func)] = arg
self.generic_visit(node)
def _fix_dict_set(contents_text):
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = FindDictsSetsVisitor()
visitor.visit(ast_obj)
if not any((visitor.dicts, visitor.sets, visitor.set_empty_literals)):
return contents_text
tokens = src_to_tokens(contents_text)
for i, token in reversed_enumerate(tokens):
if token.offset in visitor.dicts:
_process_dict_comp(tokens, i, visitor.dicts[token.offset])
elif token.offset in visitor.set_empty_literals:
_process_set_empty_literal(tokens, i)
elif token.offset in visitor.sets:
_process_set_literal(tokens, i, visitor.sets[token.offset])
return tokens_to_src(tokens)
def _imports_unicode_literals(contents_text):
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return False
for node in ast_obj.body:
# Docstring
if isinstance(node, ast.Expr) and isinstance(node.value, ast.Str):
continue
elif isinstance(node, ast.ImportFrom):
if (
node.module == '__future__' and
any(name.name == 'unicode_literals' for name in node.names)
):
return True
elif node.module == '__future__':
continue
else:
return False
else:
return False
STRING_PREFIXES_RE = re.compile('^([^\'"]*)(.*)$', re.DOTALL)
# https://docs.python.org/3/reference/lexical_analysis.html
ESCAPE_STARTS = frozenset((
'\n', '\r', '\\', "'", '"', 'a', 'b', 'f', 'n', 'r', 't', 'v',
'0', '1', '2', '3', '4', '5', '6', '7', # octal escapes
'x', # hex escapes
# only valid in non-bytestrings
'N', 'u', 'U',
))
ESCAPE_STARTS_BYTES = ESCAPE_STARTS - frozenset(('N', 'u', 'U'))
ESCAPE_RE = re.compile(r'\\.', re.DOTALL)
def _parse_string_literal(s):
match = STRING_PREFIXES_RE.match(s)
return match.group(1), match.group(2)
def _fix_escape_sequences(token):
prefix, rest = _parse_string_literal(token.src)
actual_prefix = prefix.lower()
if 'r' in actual_prefix or '\\' not in rest:
return token
if 'b' in actual_prefix:
valid_escapes = ESCAPE_STARTS_BYTES
else:
valid_escapes = ESCAPE_STARTS
escape_sequences = {m[1] for m in ESCAPE_RE.findall(rest)}
has_valid_escapes = escape_sequences & valid_escapes
has_invalid_escapes = escape_sequences - valid_escapes
def cb(match):
matched = match.group()
if matched[1] in valid_escapes:
return matched
else:
return r'\{}'.format(matched)
if has_invalid_escapes and (has_valid_escapes or 'u' in actual_prefix):
return token._replace(src=prefix + ESCAPE_RE.sub(cb, rest))
elif has_invalid_escapes and not has_valid_escapes:
return token._replace(src=prefix + 'r' + rest)
else:
return token
def _remove_u_prefix(token):
prefix, rest = _parse_string_literal(token.src)
if 'u' not in prefix.lower():
return token
else:
new_prefix = prefix.replace('u', '').replace('U', '')
return Token('STRING', new_prefix + rest)
def _fix_ur_literals(token):
prefix, rest = _parse_string_literal(token.src)
if prefix.lower() != 'ur':
return token
else:
def cb(match):
escape = match.group()
if escape[1].lower() == 'u':
return escape
else:
return '\\' + match.group()
rest = ESCAPE_RE.sub(cb, rest)
prefix = prefix.replace('r', '').replace('R', '')
return token._replace(src=prefix + rest)
def _fix_long(src):
return src.rstrip('lL')
def _fix_octal(s):
if not s.startswith('0') or not s.isdigit() or s == len(s) * '0':
return s
elif len(s) == 2: # pragma: no cover (py2 only)
return s[1:]
else: # pragma: no cover (py2 only)
return '0o' + s[1:]
def _is_string_prefix(token):
return token.name == 'NAME' and set(token.src.lower()) <= set('bfru')
def _fix_tokens(contents_text, py3_plus):
remove_u_prefix = py3_plus or _imports_unicode_literals(contents_text)
try:
tokens = src_to_tokens(contents_text)
except tokenize.TokenError:
return contents_text
for i, token in enumerate(tokens):
if token.name == 'NUMBER':
tokens[i] = token._replace(src=_fix_long(_fix_octal(token.src)))
elif token.name == 'STRING':
# when a string prefix is not recognized, the tokenizer produces a
# NAME token followed by a STRING token
if i > 0 and _is_string_prefix(tokens[i - 1]):
tokens[i] = token._replace(src=tokens[i - 1].src + token.src)
tokens[i - 1] = tokens[i - 1]._replace(src='')
tokens[i] = _fix_ur_literals(tokens[i])
if remove_u_prefix:
tokens[i] = _remove_u_prefix(tokens[i])
tokens[i] = _fix_escape_sequences(tokens[i])
return tokens_to_src(tokens)
MAPPING_KEY_RE = re.compile(r'\(([^()]*)\)')
CONVERSION_FLAG_RE = re.compile('[#0+ -]*')
WIDTH_RE = re.compile(r'(?:\*|\d*)')
PRECISION_RE = re.compile(r'(?:\.(?:\*|\d*))?')
LENGTH_RE = re.compile('[hlL]?')
def parse_percent_format(s):
def _parse_inner():
string_start = 0
string_end = 0
in_fmt = False
i = 0
while i < len(s):
if not in_fmt:
try:
i = s.index('%', i)
except ValueError: # no more % fields!
yield s[string_start:], None
return
else:
string_end = i
i += 1
in_fmt = True
else:
key_match = MAPPING_KEY_RE.match(s, i)
if key_match:
key = key_match.group(1)
i = key_match.end()
else:
key = None
conversion_flag_match = CONVERSION_FLAG_RE.match(s, i)
conversion_flag = conversion_flag_match.group() or None
i = conversion_flag_match.end()
width_match = WIDTH_RE.match(s, i)
width = width_match.group() or None
i = width_match.end()
precision_match = PRECISION_RE.match(s, i)
precision = precision_match.group() or None
i = precision_match.end()
# length modifier is ignored
i = LENGTH_RE.match(s, i).end()
conversion = s[i]
i += 1
fmt = (key, conversion_flag, width, precision, conversion)
yield s[string_start:string_end], fmt
in_fmt = False
string_start = i
return tuple(_parse_inner())
class FindPercentFormats(ast.NodeVisitor):
def __init__(self):
self.found = {}
def visit_BinOp(self, node):
if isinstance(node.op, ast.Mod) and isinstance(node.left, ast.Str):
for _, fmt in parse_percent_format(node.left.s):
if not fmt:
continue
key, conversion_flag, width, precision, conversion = fmt
# timid: these require out-of-order parameter consumption
if width == '*' or precision == '.*':
break
# timid: these conversions require modification of parameters
if conversion in {'d', 'i', 'u', 'c'}:
break
# timid: py2: %#o formats different from {:#o} (TODO: --py3)
if '#' in (conversion_flag or '') and conversion == 'o':
break
# timid: no equivalent in format
if key == '':
break
# timid: py2: conversion is subject to modifiers (TODO: --py3)
nontrivial_fmt = any((conversion_flag, width, precision))
if conversion == '%' and nontrivial_fmt:
break
# timid: no equivalent in format
if conversion in {'a', 'r'} and nontrivial_fmt:
break
# all dict substitutions must be named
if isinstance(node.right, ast.Dict) and not key:
break
else:
self.found[_ast_to_offset(node)] = node
self.generic_visit(node)
def _simplify_conversion_flag(flag):
parts = []
for c in flag:
if c in parts:
continue
c = c.replace('-', '<')
parts.append(c)
if c == '<' and '0' in parts:
parts.remove('0')
elif c == '+' and ' ' in parts:
parts.remove(' ')
return ''.join(parts)
def _percent_to_format(s):
def _handle_part(part):
s, fmt = part
s = s.replace('{', '{{').replace('}', '}}')
if fmt is None:
return s
else:
key, conversion_flag, width, precision, conversion = fmt
if conversion == '%':
return s + '%'
parts = [s, '{']
if width and conversion == 's' and not conversion_flag:
conversion_flag = '>'
if conversion == 's':
conversion = None
if key:
parts.append(key)
if conversion in {'r', 'a'}:
converter = '!{}'.format(conversion)
conversion = ''
else:
converter = ''
if any((conversion_flag, width, precision, conversion)):
parts.append(':')
if conversion_flag:
parts.append(_simplify_conversion_flag(conversion_flag))
parts.extend(x for x in (width, precision, conversion) if x)
parts.extend(converter)
parts.append('}')
return ''.join(parts)
return ''.join(_handle_part(part) for part in parse_percent_format(s))
def _is_bytestring(s):
for c in s:
if c in '"\'':
return False
elif c.lower() == 'b':
return True
else:
return False
def _is_ascii(s):
if sys.version_info >= (3, 7): # pragma: no cover (py37+)
return s.isascii()
else: # pragma: no cover (<py37)
return all(c in string.printable for c in s)
def _fix_percent_format_tuple(tokens, start, node):
# TODO: this is overly timid
paren = start + 4
if tokens_to_src(tokens[start + 1:paren + 1]) != ' % (':
return
victims = _victims(tokens, paren, node.right, gen=False)
victims.ends.pop()
for index in reversed(victims.starts + victims.ends):
_remove_brace(tokens, index)
newsrc = _percent_to_format(tokens[start].src)
tokens[start] = tokens[start]._replace(src=newsrc)
tokens[start + 1:paren] = [Token('Format', '.format'), Token('OP', '(')]
IDENT_RE = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$')
def _fix_percent_format_dict(tokens, start, node):
seen_keys = set()
keys = {}
for k in node.right.keys:
# not a string key
if not isinstance(k, ast.Str):
return
# duplicate key
elif k.s in seen_keys:
return
# not an identifier
elif not IDENT_RE.match(k.s):
return
# a keyword
elif k.s in keyword.kwlist:
return
seen_keys.add(k.s)
keys[_ast_to_offset(k)] = k
# TODO: this is overly timid
brace = start + 4
if tokens_to_src(tokens[start + 1:brace + 1]) != ' % {':
return
victims = _victims(tokens, brace, node.right, gen=False)
brace_end = victims.ends[-1]
key_indices = []
for i, token in enumerate(tokens[brace:brace_end], brace):
k = keys.pop(token.offset, None)
if k is None:
continue
# we found the key, but the string didn't match (implicit join?)
elif ast.literal_eval(token.src) != k.s:
return
# the map uses some strange syntax that's not `'k': v`
elif tokens_to_src(tokens[i + 1:i + 3]) != ': ':
return
else:
key_indices.append((i, k.s))
assert not keys, keys
tokens[brace_end] = tokens[brace_end]._replace(src=')')
for (key_index, s) in reversed(key_indices):
tokens[key_index:key_index + 3] = [Token('CODE', '{}='.format(s))]
newsrc = _percent_to_format(tokens[start].src)
tokens[start] = tokens[start]._replace(src=newsrc)
tokens[start + 1:brace + 1] = [Token('CODE', '.format'), Token('OP', '(')]
def _fix_percent_format(contents_text):
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = FindPercentFormats()
visitor.visit(ast_obj)
tokens = src_to_tokens(contents_text)
for i, token in reversed_enumerate(tokens):
node = visitor.found.get(token.offset)
if node is None:
continue
# no .format() equivalent for bytestrings in py3
# note that this code is only necessary when running in python2
if _is_bytestring(tokens[i].src): # pragma: no cover (py2-only)
continue
if isinstance(node.right, ast.Tuple):
_fix_percent_format_tuple(tokens, i, node)
elif isinstance(node.right, ast.Dict):
_fix_percent_format_dict(tokens, i, node)
return tokens_to_src(tokens)
# PY2: arguments are `Name`, PY3: arguments are `arg`
ARGATTR = 'id' if str is bytes else 'arg'
class FindSuper(ast.NodeVisitor):
class ClassInfo:
def __init__(self, name):
self.name = name
self.def_depth = 0
self.first_arg_name = ''
def __init__(self):
self.class_info_stack = []
self.found = {}
self.in_comp = 0
def visit_ClassDef(self, node):
self.class_info_stack.append(FindSuper.ClassInfo(node.name))
self.generic_visit(node)
self.class_info_stack.pop()
def _visit_func(self, node):
if self.class_info_stack:
class_info = self.class_info_stack[-1]
class_info.def_depth += 1
if class_info.def_depth == 1 and node.args.args:
class_info.first_arg_name = getattr(node.args.args[0], ARGATTR)
self.generic_visit(node)
class_info.def_depth -= 1
else:
self.generic_visit(node)
visit_FunctionDef = visit_Lambda = _visit_func
def _visit_comp(self, node):
self.in_comp += 1
self.generic_visit(node)
self.in_comp -= 1
visit_ListComp = visit_SetComp = _visit_comp
visit_DictComp = visit_GeneratorExp = _visit_comp
def visit_Call(self, node):
if (
not self.in_comp and
self.class_info_stack and
self.class_info_stack[-1].def_depth == 1 and
isinstance(node.func, ast.Name) and
node.func.id == 'super' and
len(node.args) == 2 and
all(isinstance(arg, ast.Name) for arg in node.args) and
node.args[0].id == self.class_info_stack[-1].name and
node.args[1].id == self.class_info_stack[-1].first_arg_name
):
self.found[_ast_to_offset(node)] = node
self.generic_visit(node)
def _fix_super(contents_text):
try:
ast_obj = ast_parse(contents_text)
except SyntaxError:
return contents_text
visitor = FindSuper()
visitor.visit(ast_obj)
tokens = src_to_tokens(contents_text)
for i, token in reversed_enumerate(tokens):
call = visitor.found.get(token.offset)
if not call:
continue
while tokens[i].name != 'OP':
i += 1
victims = _victims(tokens, i, call, gen=False)
del tokens[victims.starts[0] + 1:victims.ends[-1]]
return tokens_to_src(tokens)
SIX_SIMPLE_ATTRS = {
'text_type': 'str',
'binary_type': 'bytes',
'class_types': '(type,)',
'string_types': '(str,)',
'integer_types': '(int,)',
'unichr': 'chr',
'iterbytes': 'iter',
'print_': 'print',
'exec_': 'exec',
'advance_iterator': 'next',
'next': 'next',
'callable': 'callable',
}
SIX_TYPE_CTX_ATTRS = dict(
SIX_SIMPLE_ATTRS,
class_types='type',
string_types='str',
integer_types='int',
)
SIX_CALLS = {
'u': '{args[0]}',
'b': 'b{args[0]}',
'byte2int': '{arg0}[0]',
'indexbytes': '{args[0]}[{rest}]',
'iteritems': '{args[0]}.items()',
'iterkeys': '{args[0]}.keys()',
'itervalues': '{args[0]}.values()',
'viewitems': '{args[0]}.items()',
'viewkeys': '{args[0]}.keys()',
'viewvalues': '{args[0]}.values()',
'create_unbound_method': '{args[0]}',
'get_unbound_method': '{args[0]}',
'get_method_function': '{args[0]}.__func__',
'get_method_self': '{args[0]}.__self__',
'get_function_closure': '{args[0]}.__closure__',
'get_function_code': '{args[0]}.__code__',
'get_function_defaults': '{args[0]}.__defaults__',
'get_function_globals': '{args[0]}.__globals__',
'assertCountEqual': '{args[0]}.assertCountEqual({rest})',
'assertRaisesRegex': '{args[0]}.assertRaisesRegex({rest})',
'assertRegex': '{args[0]}.assertRegex({rest})',
}
SIX_RAISES = {
'raise_from': 'raise {args[0]} from {rest}',
'reraise': 'raise {args[1]}.with_traceback({args[2]})',
}
SIX_UNICODE_COMPATIBLE = 'python_2_unicode_compatible'
class FindSixAndClassesUsage(ast.NodeVisitor):
def __init__(self):
super(FindSixAndClassesUsage, self).__init__()
self.call_attrs = {}
self.call_names = {}
self.classes = set()
self.raise_attrs = {}
self.raise_names = {}
self.simple_attrs = {}
self.simple_names = {}
self.type_ctx_attrs = {}
self.type_ctx_names = {}
self.remove_decorators = set()
self.six_from_imports = set()
self._previous_node = None
def visit_ImportFrom(self, node):
if node.module == 'six':
for name in node.names:
if not name.asname:
self.six_from_imports.add(name.name)
self.generic_visit(node)
def visit_ClassDef(self, node):
for decorator in node.decorator_list:
if (
(
isinstance(decorator, ast.Name) and
decorator.id in self.six_from_imports and
decorator.id == SIX_UNICODE_COMPATIBLE
) or (
isinstance(decorator, ast.Attribute) and
isinstance(decorator.value, ast.Name) and
decorator.value.id == 'six' and
decorator.attr == SIX_UNICODE_COMPATIBLE
)
):
self.remove_decorators.add(_ast_to_offset(decorator))
for i, base in enumerate(node.bases):
if isinstance(base, ast.Name) and base.id == 'object':
self.classes.add(_ast_to_offset(base))
elif (
isinstance(base, ast.Name) and
base.id == 'Iterator' and
base.id in self.six_from_imports
):
self.classes.add(_ast_to_offset(base))
elif (
isinstance(base, ast.Attribute) and
isinstance(base.value, ast.Name) and
base.value.id == 'six' and
base.attr == 'Iterator'
):
self.classes.add(_ast_to_offset(base))
self.generic_visit(node)
def _is_six_attr(self, node):
return (
isinstance(node, ast.Attribute) and
isinstance(node.value, ast.Name) and
node.value.id == 'six' and
node.attr in SIX_SIMPLE_ATTRS
)
def _is_six_name(self, node):
return (
isinstance(node, ast.Name) and
node.id in SIX_SIMPLE_ATTRS and
node.id in self.six_from_imports
)
def visit_Name(self, node):
if self._is_six_name(node):
self.simple_names[_ast_to_offset(node)] = node
self.generic_visit(node)
def visit_Attribute(self, node):
if self._is_six_attr(node):
self.simple_attrs[_ast_to_offset(node)] = node
self.generic_visit(node)
def visit_Call(self, node):
if (