1
#!/usr/bin/env python2.5
2
""" Test suite for the fixer modules """
3
# Author: Collin Winter
7
from tests import support
14
from itertools import chain
15
from operator import itemgetter
18
from lib2to3 import pygram, pytree, refactor, fixer_util
21
class FixerTestCase(support.TestCase):
22
def setUp(self, fix_list=None):
24
fix_list = [self.fixer]
25
options = {"print_function" : False}
26
self.refactor = support.get_refactorer(fix_list, options)
28
self.filename = "<string>"
30
for fixer in chain(self.refactor.pre_order,
31
self.refactor.post_order):
32
fixer.log = self.fixer_log
34
def _check(self, before, after):
35
before = support.reformat(before)
36
after = support.reformat(after)
37
tree = self.refactor.refactor_string(before, self.filename)
38
self.failUnlessEqual(after, str(tree))
41
def check(self, before, after, ignore_warnings=False):
42
tree = self._check(before, after)
43
self.failUnless(tree.was_changed)
44
if not ignore_warnings:
45
self.failUnlessEqual(self.fixer_log, [])
47
def warns(self, before, after, message, unchanged=False):
48
tree = self._check(before, after)
49
self.failUnless(message in "".join(self.fixer_log))
51
self.failUnless(tree.was_changed)
53
def warns_unchanged(self, before, message):
54
self.warns(before, before, message, unchanged=True)
56
def unchanged(self, before, ignore_warnings=False):
57
self._check(before, before)
58
if not ignore_warnings:
59
self.failUnlessEqual(self.fixer_log, [])
61
def assert_runs_after(self, *names):
64
options = {"print_function" : False}
65
r = support.get_refactorer(fixes, options)
66
(pre, post) = r.get_fixers()
67
n = "fix_" + self.fixer
68
if post and post[-1].__class__.__module__.endswith(n):
69
# We're the last fixer to run
71
if pre and pre[-1].__class__.__module__.endswith(n) and not post:
72
# We're the last in pre and post is empty
74
self.fail("Fixer run order (%s) is incorrect; %s should be last."\
75
%(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
77
class Test_ne(FixerTestCase):
88
def test_no_spaces(self):
96
def test_chained(self):
104
class Test_has_key(FixerTestCase):
108
b = """x = d.has_key("x") or d.has_key("y")"""
109
a = """x = "x" in d or "y" in d"""
113
b = """x = a.b.c.d.has_key("x") ** 3"""
114
a = """x = ("x" in a.b.c.d) ** 3"""
118
b = """x = a.b.has_key(1 + 2).__repr__()"""
119
a = """x = (1 + 2 in a.b).__repr__()"""
123
b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
124
a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
128
b = """x = a.has_key(f or g)"""
129
a = """x = (f or g) in a"""
133
b = """x = a + b.has_key(c)"""
134
a = """x = a + (c in b)"""
138
b = """x = a.has_key(lambda: 12)"""
139
a = """x = (lambda: 12) in a"""
143
b = """x = a.has_key(a for a in b)"""
144
a = """x = (a for a in b) in a"""
148
b = """if not a.has_key(b): pass"""
149
a = """if b not in a: pass"""
153
b = """if not a.has_key(b).__repr__(): pass"""
154
a = """if not (b in a).__repr__(): pass"""
158
b = """if not a.has_key(b) ** 2: pass"""
159
a = """if not (b in a) ** 2: pass"""
162
class Test_apply(FixerTestCase):
166
b = """x = apply(f, g + h)"""
167
a = """x = f(*g + h)"""
171
b = """y = apply(f, g, h)"""
172
a = """y = f(*g, **h)"""
176
b = """z = apply(fs[0], g or h, h or g)"""
177
a = """z = fs[0](*g or h, **h or g)"""
181
b = """apply(f, (x, y) + t)"""
182
a = """f(*(x, y) + t)"""
186
b = """apply(f, args,)"""
191
b = """apply(f, args, kwds,)"""
192
a = """f(*args, **kwds)"""
195
# Test that complex functions are parenthesized
197
def test_complex_1(self):
198
b = """x = apply(f+g, args)"""
199
a = """x = (f+g)(*args)"""
202
def test_complex_2(self):
203
b = """x = apply(f*g, args)"""
204
a = """x = (f*g)(*args)"""
207
def test_complex_3(self):
208
b = """x = apply(f**g, args)"""
209
a = """x = (f**g)(*args)"""
212
# But dotted names etc. not
214
def test_dotted_name(self):
215
b = """x = apply(f.g, args)"""
216
a = """x = f.g(*args)"""
219
def test_subscript(self):
220
b = """x = apply(f[x], args)"""
221
a = """x = f[x](*args)"""
225
b = """x = apply(f(), args)"""
226
a = """x = f()(*args)"""
230
def test_extreme(self):
231
b = """x = apply(a.b.c.d.e.f, args, kwds)"""
232
a = """x = a.b.c.d.e.f(*args, **kwds)"""
235
# XXX Comments in weird places still get lost
236
def test_weird_comments(self):
243
# These should *not* be touched
245
def test_unchanged_1(self):
249
def test_unchanged_2(self):
253
def test_unchanged_3(self):
257
def test_unchanged_4(self):
258
s = """apply(f, args, kwds, extras)"""
261
def test_unchanged_5(self):
262
s = """apply(f, *args, **kwds)"""
265
def test_unchanged_6(self):
266
s = """apply(f, *args)"""
269
def test_unchanged_7(self):
270
s = """apply(func=f, args=args, kwds=kwds)"""
273
def test_unchanged_8(self):
274
s = """apply(f, args=args, kwds=kwds)"""
277
def test_unchanged_9(self):
278
s = """apply(f, args, kwds=kwds)"""
281
def test_space_1(self):
282
a = """apply( f, args, kwds)"""
283
b = """f(*args, **kwds)"""
286
def test_space_2(self):
287
a = """apply( f ,args,kwds )"""
288
b = """f(*args, **kwds)"""
291
class Test_intern(FixerTestCase):
294
def test_prefix_preservation(self):
295
b = """x = intern( a )"""
296
a = """import sys\nx = sys.intern( a )"""
299
b = """y = intern("b" # test
301
a = """import sys\ny = sys.intern("b" # test
305
b = """z = intern(a+b+c.d, )"""
306
a = """import sys\nz = sys.intern(a+b+c.d, )"""
310
b = """x = intern(a)"""
311
a = """import sys\nx = sys.intern(a)"""
314
b = """z = intern(a+b+c.d,)"""
315
a = """import sys\nz = sys.intern(a+b+c.d,)"""
318
b = """intern("y%s" % 5).replace("y", "")"""
319
a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
322
# These should not be refactored
324
def test_unchanged(self):
325
s = """intern(a=1)"""
328
s = """intern(f, g)"""
334
s = """intern(**i)"""
340
class Test_reduce(FixerTestCase):
343
def test_simple_call(self):
344
b = "reduce(a, b, c)"
345
a = "from functools import reduce\nreduce(a, b, c)"
348
def test_call_with_lambda(self):
349
b = "reduce(lambda x, y: x + y, seq)"
350
a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)"
353
def test_unchanged(self):
357
s = "reduce(a, b=42)"
360
s = "reduce(a, b, c, d)"
369
class Test_print(FixerTestCase):
372
def test_prefix_preservation(self):
373
b = """print 1, 1+1, 1+1+1"""
374
a = """print(1, 1+1, 1+1+1)"""
377
def test_idempotency(self):
384
def test_idempotency_print_as_function(self):
385
print_stmt = pygram.python_grammar.keywords.pop("print")
387
s = """print(1, 1+1, 1+1+1)"""
396
pygram.python_grammar.keywords["print"] = print_stmt
399
b = """print 1, 1+1, 1+1+1"""
400
a = """print(1, 1+1, 1+1+1)"""
405
a = """print(1, 2)"""
415
b = """print whatever; print"""
416
a = """print(whatever); print()"""
420
b = """print; print whatever;"""
421
a = """print(); print(whatever);"""
423
def test_tuple(self):
424
b = """print (a, b, c)"""
425
a = """print((a, b, c))"""
430
def test_trailing_comma_1(self):
431
b = """print 1, 2, 3,"""
432
a = """print(1, 2, 3, end=' ')"""
435
def test_trailing_comma_2(self):
436
b = """print 1, 2,"""
437
a = """print(1, 2, end=' ')"""
440
def test_trailing_comma_3(self):
442
a = """print(1, end=' ')"""
447
def test_vargs_without_trailing_comma(self):
448
b = """print >>sys.stderr, 1, 2, 3"""
449
a = """print(1, 2, 3, file=sys.stderr)"""
452
def test_with_trailing_comma(self):
453
b = """print >>sys.stderr, 1, 2,"""
454
a = """print(1, 2, end=' ', file=sys.stderr)"""
457
def test_no_trailing_comma(self):
458
b = """print >>sys.stderr, 1+1"""
459
a = """print(1+1, file=sys.stderr)"""
462
def test_spaces_before_file(self):
463
b = """print >> sys.stderr"""
464
a = """print(file=sys.stderr)"""
467
# With from __future__ import print_function
468
def test_with_future_print_function(self):
469
# XXX: These tests won't actually do anything until the parser
470
# is fixed so it won't crash when it sees print(x=y).
471
# When #2412 is fixed, the try/except block can be taken
472
# out and the tests can be run like normal.
473
# MvL: disable entirely for now, so that it doesn't print to stdout
476
s = "from __future__ import print_function\n"\
477
"print('Hai!', end=' ')"
480
b = "print 'Hello, world!'"
481
a = "print('Hello, world!')"
484
s = "from __future__ import *\n"\
485
"print('Hai!', end=' ')"
490
self.assertFalse(True, "#2421 has been fixed -- printing tests "\
491
"need to be updated!")
493
class Test_exec(FixerTestCase):
496
def test_prefix_preservation(self):
497
b = """ exec code in ns1, ns2"""
498
a = """ exec(code, ns1, ns2)"""
501
def test_basic(self):
506
def test_with_globals(self):
507
b = """exec code in ns"""
508
a = """exec(code, ns)"""
511
def test_with_globals_locals(self):
512
b = """exec code in ns1, ns2"""
513
a = """exec(code, ns1, ns2)"""
516
def test_complex_1(self):
517
b = """exec (a.b()) in ns"""
518
a = """exec((a.b()), ns)"""
521
def test_complex_2(self):
522
b = """exec a.b() + c in ns"""
523
a = """exec(a.b() + c, ns)"""
526
# These should not be touched
528
def test_unchanged_1(self):
532
def test_unchanged_2(self):
533
s = """exec (code)"""
536
def test_unchanged_3(self):
537
s = """exec(code, ns)"""
540
def test_unchanged_4(self):
541
s = """exec(code, ns1, ns2)"""
544
class Test_repr(FixerTestCase):
547
def test_prefix_preservation(self):
548
b = """x = `1 + 2`"""
549
a = """x = repr(1 + 2)"""
552
def test_simple_1(self):
553
b = """x = `1 + 2`"""
554
a = """x = repr(1 + 2)"""
557
def test_simple_2(self):
559
a = """y = repr(x)"""
562
def test_complex(self):
563
b = """z = `y`.__repr__()"""
564
a = """z = repr(y).__repr__()"""
567
def test_tuple(self):
568
b = """x = `1, 2, 3`"""
569
a = """x = repr((1, 2, 3))"""
572
def test_nested(self):
573
b = """x = `1 + `2``"""
574
a = """x = repr(1 + repr(2))"""
577
def test_nested_tuples(self):
578
b = """x = `1, 2 + `3, 4``"""
579
a = """x = repr((1, 2 + repr((3, 4))))"""
582
class Test_except(FixerTestCase):
585
def test_prefix_preservation(self):
589
except (RuntimeError, ImportError), e:
594
except (RuntimeError, ImportError) as e:
598
def test_simple(self):
611
def test_simple_no_space_before_target(self):
624
def test_tuple_unpack(self):
629
except Exception, (f, e):
631
except ImportError, e:
638
except Exception as xxx_todo_changeme:
639
(f, e) = xxx_todo_changeme.args
641
except ImportError as e:
645
def test_multi_class(self):
649
except (RuntimeError, ImportError), e:
655
except (RuntimeError, ImportError) as e:
659
def test_list_unpack(self):
663
except Exception, [a, b]:
669
except Exception as xxx_todo_changeme:
670
[a, b] = xxx_todo_changeme.args
674
def test_weird_target_1(self):
678
except Exception, d[5]:
684
except Exception as xxx_todo_changeme:
685
d[5] = xxx_todo_changeme
689
def test_weird_target_2(self):
693
except Exception, a.foo:
699
except Exception as xxx_todo_changeme:
700
a.foo = xxx_todo_changeme
704
def test_weird_target_3(self):
708
except Exception, a().foo:
714
except Exception as xxx_todo_changeme:
715
a().foo = xxx_todo_changeme
719
def test_bare_except(self):
731
except Exception as a:
737
def test_bare_except_and_else_finally(self):
753
except Exception as a:
763
def test_multi_fixed_excepts_before_bare_except(self):
777
except TypeError as b:
779
except Exception as a:
785
# These should not be touched:
787
def test_unchanged_1(self):
795
def test_unchanged_2(self):
803
def test_unchanged_3(self):
807
except (Exception, SystemExit):
811
class Test_raise(FixerTestCase):
814
def test_basic(self):
815
b = """raise Exception, 5"""
816
a = """raise Exception(5)"""
819
def test_prefix_preservation(self):
820
b = """raise Exception,5"""
821
a = """raise Exception(5)"""
824
b = """raise Exception, 5"""
825
a = """raise Exception(5)"""
828
def test_with_comments(self):
829
b = """raise Exception, 5 # foo"""
830
a = """raise Exception(5) # foo"""
833
b = """raise E, (5, 6) % (a, b) # foo"""
834
a = """raise E((5, 6) % (a, b)) # foo"""
838
raise Exception, 5, 6 # foo"""
840
raise Exception(5).with_traceback(6) # foo"""
843
def test_tuple_value(self):
844
b = """raise Exception, (5, 6, 7)"""
845
a = """raise Exception(5, 6, 7)"""
848
def test_tuple_detection(self):
849
b = """raise E, (5, 6) % (a, b)"""
850
a = """raise E((5, 6) % (a, b))"""
853
def test_tuple_exc_1(self):
854
b = """raise (((E1, E2), E3), E4), V"""
855
a = """raise E1(V)"""
858
def test_tuple_exc_2(self):
859
b = """raise (E1, (E2, E3), E4), V"""
860
a = """raise E1(V)"""
863
# These should produce a warning
865
def test_string_exc(self):
866
s = """raise 'foo'"""
867
self.warns_unchanged(s, "Python 3 does not support string exceptions")
869
def test_string_exc_val(self):
870
s = """raise "foo", 5"""
871
self.warns_unchanged(s, "Python 3 does not support string exceptions")
873
def test_string_exc_val_tb(self):
874
s = """raise "foo", 5, 6"""
875
self.warns_unchanged(s, "Python 3 does not support string exceptions")
877
# These should result in traceback-assignment
881
raise Exception, 5, 6"""
883
raise Exception(5).with_traceback(6)"""
889
raise Exception, 5, 6
893
raise Exception(5).with_traceback(6)
899
raise Exception,5,6"""
901
raise Exception(5).with_traceback(6)"""
911
raise Exception(5).with_traceback(6)
917
raise Exception, (5, 6, 7), 6"""
919
raise Exception(5, 6, 7).with_traceback(6)"""
925
raise Exception, (5, 6, 7), 6
929
raise Exception(5, 6, 7).with_traceback(6)
933
class Test_throw(FixerTestCase):
937
b = """g.throw(Exception, 5)"""
938
a = """g.throw(Exception(5))"""
942
b = """g.throw(Exception,5)"""
943
a = """g.throw(Exception(5))"""
947
b = """g.throw(Exception, (5, 6, 7))"""
948
a = """g.throw(Exception(5, 6, 7))"""
952
b = """5 + g.throw(Exception, 5)"""
953
a = """5 + g.throw(Exception(5))"""
956
# These should produce warnings
958
def test_warn_1(self):
959
s = """g.throw("foo")"""
960
self.warns_unchanged(s, "Python 3 does not support string exceptions")
962
def test_warn_2(self):
963
s = """g.throw("foo", 5)"""
964
self.warns_unchanged(s, "Python 3 does not support string exceptions")
966
def test_warn_3(self):
967
s = """g.throw("foo", 5, 6)"""
968
self.warns_unchanged(s, "Python 3 does not support string exceptions")
970
# These should not be touched
972
def test_untouched_1(self):
973
s = """g.throw(Exception)"""
976
def test_untouched_2(self):
977
s = """g.throw(Exception(5, 6))"""
980
def test_untouched_3(self):
981
s = """5 + g.throw(Exception(5, 6))"""
984
# These should result in traceback-assignment
988
g.throw(Exception, 5, 6)"""
990
g.throw(Exception(5).with_traceback(6))"""
996
g.throw(Exception, 5, 6)
1000
g.throw(Exception(5).with_traceback(6))
1004
def test_tb_3(self):
1006
g.throw(Exception,5,6)"""
1008
g.throw(Exception(5).with_traceback(6))"""
1011
def test_tb_4(self):
1014
g.throw(Exception,5,6)
1018
g.throw(Exception(5).with_traceback(6))
1022
def test_tb_5(self):
1024
g.throw(Exception, (5, 6, 7), 6)"""
1026
g.throw(Exception(5, 6, 7).with_traceback(6))"""
1029
def test_tb_6(self):
1032
g.throw(Exception, (5, 6, 7), 6)
1036
g.throw(Exception(5, 6, 7).with_traceback(6))
1040
def test_tb_7(self):
1042
a + g.throw(Exception, 5, 6)"""
1044
a + g.throw(Exception(5).with_traceback(6))"""
1047
def test_tb_8(self):
1050
a + g.throw(Exception, 5, 6)
1054
a + g.throw(Exception(5).with_traceback(6))
1058
class Test_long(FixerTestCase):
1062
b = """x = long(x)"""
1063
a = """x = int(x)"""
1067
b = """y = isinstance(x, long)"""
1068
a = """y = isinstance(x, int)"""
1072
b = """z = type(x) in (int, long)"""
1073
a = """z = type(x) in (int, int)"""
1076
def test_unchanged(self):
1077
s = """long = True"""
1080
s = """s.long = True"""
1083
s = """def long(): pass"""
1086
s = """class long(): pass"""
1089
s = """def f(long): pass"""
1092
s = """def f(g, long): pass"""
1095
s = """def f(x, long=True): pass"""
1098
def test_prefix_preservation(self):
1099
b = """x = long( x )"""
1100
a = """x = int( x )"""
1104
class Test_execfile(FixerTestCase):
1107
def test_conversion(self):
1108
b = """execfile("fn")"""
1109
a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
1112
b = """execfile("fn", glob)"""
1113
a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
1116
b = """execfile("fn", glob, loc)"""
1117
a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
1120
b = """execfile("fn", globals=glob)"""
1121
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
1124
b = """execfile("fn", locals=loc)"""
1125
a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
1128
b = """execfile("fn", globals=glob, locals=loc)"""
1129
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
1132
def test_spacing(self):
1133
b = """execfile( "fn" )"""
1134
a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
1137
b = """execfile("fn", globals = glob)"""
1138
a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)"""
1142
class Test_isinstance(FixerTestCase):
1143
fixer = "isinstance"
1145
def test_remove_multiple_items(self):
1146
b = """isinstance(x, (int, int, int))"""
1147
a = """isinstance(x, int)"""
1150
b = """isinstance(x, (int, float, int, int, float))"""
1151
a = """isinstance(x, (int, float))"""
1154
b = """isinstance(x, (int, float, int, int, float, str))"""
1155
a = """isinstance(x, (int, float, str))"""
1158
b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
1159
a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
1162
def test_prefix_preservation(self):
1163
b = """if isinstance( foo(), ( bar, bar, baz )) : pass"""
1164
a = """if isinstance( foo(), ( bar, baz )) : pass"""
1167
def test_unchanged(self):
1168
self.unchanged("isinstance(x, (str, int))")
1170
class Test_dict(FixerTestCase):
1173
def test_prefix_preservation(self):
1174
b = "if d. keys ( ) : pass"
1175
a = "if list(d. keys ( )) : pass"
1178
b = "if d. items ( ) : pass"
1179
a = "if list(d. items ( )) : pass"
1182
b = "if d. iterkeys ( ) : pass"
1183
a = "if iter(d. keys ( )) : pass"
1186
b = "[i for i in d. iterkeys( ) ]"
1187
a = "[i for i in d. keys( ) ]"
1190
def test_trailing_comment(self):
1191
b = "d.keys() # foo"
1192
a = "list(d.keys()) # foo"
1195
b = "d.items() # foo"
1196
a = "list(d.items()) # foo"
1199
b = "d.iterkeys() # foo"
1200
a = "iter(d.keys()) # foo"
1203
b = """[i for i in d.iterkeys() # foo
1205
a = """[i for i in d.keys() # foo
1209
def test_unchanged(self):
1210
for wrapper in fixer_util.consuming_calls:
1211
s = "s = %s(d.keys())" % wrapper
1214
s = "s = %s(d.values())" % wrapper
1217
s = "s = %s(d.items())" % wrapper
1222
a = "list(d.keys())"
1225
b = "a[0].foo().keys()"
1226
a = "list(a[0].foo().keys())"
1231
a = "list(d.items())"
1236
a = "list(d.values())"
1241
a = "iter(d.keys())"
1246
a = "iter(d.items())"
1250
b = "d.itervalues()"
1251
a = "iter(d.values())"
1255
s = "list(d.keys())"
1259
s = "sorted(d.keys())"
1263
b = "iter(d.keys())"
1264
a = "iter(list(d.keys()))"
1269
a = "foo(list(d.keys()))"
1273
b = "for i in d.keys(): print i"
1274
a = "for i in list(d.keys()): print i"
1278
b = "for i in d.iterkeys(): print i"
1279
a = "for i in d.keys(): print i"
1283
b = "[i for i in d.keys()]"
1284
a = "[i for i in list(d.keys())]"
1288
b = "[i for i in d.iterkeys()]"
1289
a = "[i for i in d.keys()]"
1293
b = "(i for i in d.keys())"
1294
a = "(i for i in list(d.keys()))"
1298
b = "(i for i in d.iterkeys())"
1299
a = "(i for i in d.keys())"
1303
b = "iter(d.iterkeys())"
1304
a = "iter(d.keys())"
1308
b = "list(d.iterkeys())"
1309
a = "list(d.keys())"
1313
b = "sorted(d.iterkeys())"
1314
a = "sorted(d.keys())"
1318
b = "foo(d.iterkeys())"
1319
a = "foo(iter(d.keys()))"
1323
b = "print h.iterkeys().next()"
1324
a = "print iter(h.keys()).next()"
1328
b = "print h.keys()[0]"
1329
a = "print list(h.keys())[0]"
1333
b = "print list(h.iterkeys().next())"
1334
a = "print list(iter(h.keys()).next())"
1338
b = "for x in h.keys()[0]: print x"
1339
a = "for x in list(h.keys())[0]: print x"
1342
class Test_xrange(FixerTestCase):
1345
def test_prefix_preservation(self):
1346
b = """x = xrange( 10 )"""
1347
a = """x = range( 10 )"""
1350
b = """x = xrange( 1 , 10 )"""
1351
a = """x = range( 1 , 10 )"""
1354
b = """x = xrange( 0 , 10 , 2 )"""
1355
a = """x = range( 0 , 10 , 2 )"""
1358
def test_single_arg(self):
1359
b = """x = xrange(10)"""
1360
a = """x = range(10)"""
1363
def test_two_args(self):
1364
b = """x = xrange(1, 10)"""
1365
a = """x = range(1, 10)"""
1368
def test_three_args(self):
1369
b = """x = xrange(0, 10, 2)"""
1370
a = """x = range(0, 10, 2)"""
1373
def test_wrap_in_list(self):
1374
b = """x = range(10, 3, 9)"""
1375
a = """x = list(range(10, 3, 9))"""
1378
b = """x = foo(range(10, 3, 9))"""
1379
a = """x = foo(list(range(10, 3, 9)))"""
1382
b = """x = range(10, 3, 9) + [4]"""
1383
a = """x = list(range(10, 3, 9)) + [4]"""
1386
b = """x = range(10)[::-1]"""
1387
a = """x = list(range(10))[::-1]"""
1390
b = """x = range(10) [3]"""
1391
a = """x = list(range(10)) [3]"""
1394
def test_xrange_in_for(self):
1395
b = """for i in xrange(10):\n j=i"""
1396
a = """for i in range(10):\n j=i"""
1399
b = """[i for i in xrange(10)]"""
1400
a = """[i for i in range(10)]"""
1403
def test_range_in_for(self):
1404
self.unchanged("for i in range(10): pass")
1405
self.unchanged("[i for i in range(10)]")
1407
def test_in_contains_test(self):
1408
self.unchanged("x in range(10, 3, 9)")
1410
def test_in_consuming_context(self):
1411
for call in fixer_util.consuming_calls:
1412
self.unchanged("a = %s(range(10))" % call)
1414
class Test_raw_input(FixerTestCase):
1417
def test_prefix_preservation(self):
1418
b = """x = raw_input( )"""
1419
a = """x = input( )"""
1422
b = """x = raw_input( '' )"""
1423
a = """x = input( '' )"""
1427
b = """x = raw_input()"""
1428
a = """x = input()"""
1432
b = """x = raw_input('')"""
1433
a = """x = input('')"""
1437
b = """x = raw_input('prompt')"""
1438
a = """x = input('prompt')"""
1442
b = """x = raw_input(foo(a) + 6)"""
1443
a = """x = input(foo(a) + 6)"""
1447
b = """x = raw_input(invite).split()"""
1448
a = """x = input(invite).split()"""
1452
b = """x = raw_input(invite) . split ()"""
1453
a = """x = input(invite) . split ()"""
1457
b = "x = int(raw_input())"
1458
a = "x = int(input())"
1461
class Test_funcattrs(FixerTestCase):
1464
attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
1467
for attr in self.attrs:
1468
b = "a.func_%s" % attr
1469
a = "a.__%s__" % attr
1472
b = "self.foo.func_%s.foo_bar" % attr
1473
a = "self.foo.__%s__.foo_bar" % attr
1476
def test_unchanged(self):
1477
for attr in self.attrs:
1478
s = "foo(func_%s + 5)" % attr
1481
s = "f(foo.__%s__)" % attr
1484
s = "f(foo.__%s__.foo)" % attr
1487
class Test_xreadlines(FixerTestCase):
1488
fixer = "xreadlines"
1490
def test_call(self):
1491
b = "for x in f.xreadlines(): pass"
1492
a = "for x in f: pass"
1495
b = "for x in foo().xreadlines(): pass"
1496
a = "for x in foo(): pass"
1499
b = "for x in (5 + foo()).xreadlines(): pass"
1500
a = "for x in (5 + foo()): pass"
1503
def test_attr_ref(self):
1504
b = "foo(f.xreadlines + 5)"
1505
a = "foo(f.__iter__ + 5)"
1508
b = "foo(f().xreadlines + 5)"
1509
a = "foo(f().__iter__ + 5)"
1512
b = "foo((5 + f()).xreadlines + 5)"
1513
a = "foo((5 + f()).__iter__ + 5)"
1516
def test_unchanged(self):
1517
s = "for x in f.xreadlines(5): pass"
1520
s = "for x in f.xreadlines(k=5): pass"
1523
s = "for x in f.xreadlines(*k, **v): pass"
1526
s = "foo(xreadlines)"
1530
class ImportsFixerTests:
1532
def test_import_module(self):
1533
for old, new in self.modules.items():
1534
b = "import %s" % old
1535
a = "import %s" % new
1538
b = "import foo, %s, bar" % old
1539
a = "import foo, %s, bar" % new
1542
def test_import_from(self):
1543
for old, new in self.modules.items():
1544
b = "from %s import foo" % old
1545
a = "from %s import foo" % new
1548
b = "from %s import foo, bar" % old
1549
a = "from %s import foo, bar" % new
1552
b = "from %s import (yes, no)" % old
1553
a = "from %s import (yes, no)" % new
1556
def test_import_module_as(self):
1557
for old, new in self.modules.items():
1558
b = "import %s as foo_bar" % old
1559
a = "import %s as foo_bar" % new
1562
b = "import %s as foo_bar" % old
1563
a = "import %s as foo_bar" % new
1566
def test_import_from_as(self):
1567
for old, new in self.modules.items():
1568
b = "from %s import foo as bar" % old
1569
a = "from %s import foo as bar" % new
1572
def test_star(self):
1573
for old, new in self.modules.items():
1574
b = "from %s import *" % old
1575
a = "from %s import *" % new
1578
def test_import_module_usage(self):
1579
for old, new in self.modules.items():
1610
""" % (old, old, old)
1614
""" % (new, new, new)
1628
class Test_imports(FixerTestCase, ImportsFixerTests):
1630
from ..fixes.fix_imports import MAPPING as modules
1632
def test_multiple_imports(self):
1633
b = """import urlparse, cStringIO"""
1634
a = """import urllib.parse, io"""
1637
def test_multiple_imports_as(self):
1639
import copy_reg as bar, HTMLParser as foo, urlparse
1640
s = urlparse.spam(bar.foo())
1643
import copyreg as bar, html.parser as foo, urllib.parse
1644
s = urllib.parse.spam(bar.foo())
1649
class Test_imports2(FixerTestCase, ImportsFixerTests):
1651
from ..fixes.fix_imports2 import MAPPING as modules
1654
class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
1657
super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
1658
from ..fixes.fix_imports2 import MAPPING as mapping2
1659
self.modules = mapping2.copy()
1660
from ..fixes.fix_imports import MAPPING as mapping1
1661
for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
1662
self.modules[key] = mapping1[key]
1665
class Test_urllib(FixerTestCase):
1667
from ..fixes.fix_urllib import MAPPING as modules
1669
def test_import_module(self):
1670
for old, changes in self.modules.items():
1671
b = "import %s" % old
1672
a = "import %s" % ", ".join(map(itemgetter(0), changes))
1675
def test_import_from(self):
1676
for old, changes in self.modules.items():
1678
for new, members in changes:
1679
for member in members:
1680
all_members.append(member)
1681
b = "from %s import %s" % (old, member)
1682
a = "from %s import %s" % (new, member)
1685
s = "from foo import %s" % member
1688
b = "from %s import %s" % (old, ", ".join(members))
1689
a = "from %s import %s" % (new, ", ".join(members))
1692
s = "from foo import %s" % ", ".join(members)
1695
# test the breaking of a module into multiple replacements
1696
b = "from %s import %s" % (old, ", ".join(all_members))
1697
a = "\n".join(["from %s import %s" % (new, ", ".join(members))
1698
for (new, members) in changes])
1701
def test_import_module_as(self):
1702
for old in self.modules:
1703
s = "import %s as foo" % old
1704
self.warns_unchanged(s, "This module is now multiple modules")
1706
def test_import_from_as(self):
1707
for old, changes in self.modules.items():
1708
for new, members in changes:
1709
for member in members:
1710
b = "from %s import %s as foo_bar" % (old, member)
1711
a = "from %s import %s as foo_bar" % (new, member)
1714
def test_star(self):
1715
for old in self.modules:
1716
s = "from %s import *" % old
1717
self.warns_unchanged(s, "Cannot handle star imports")
1719
def test_import_module_usage(self):
1720
for old, changes in self.modules.items():
1721
for new, members in changes:
1722
for member in members:
1726
""" % (old, old, member)
1730
""" % (", ".join([n for (n, mems)
1731
in self.modules[old]]),
1736
class Test_input(FixerTestCase):
1739
def test_prefix_preservation(self):
1740
b = """x = input( )"""
1741
a = """x = eval(input( ))"""
1744
b = """x = input( '' )"""
1745
a = """x = eval(input( '' ))"""
1748
def test_trailing_comment(self):
1749
b = """x = input() # foo"""
1750
a = """x = eval(input()) # foo"""
1753
def test_idempotency(self):
1754
s = """x = eval(input())"""
1757
s = """x = eval(input(''))"""
1760
s = """x = eval(input(foo(5) + 9))"""
1764
b = """x = input()"""
1765
a = """x = eval(input())"""
1769
b = """x = input('')"""
1770
a = """x = eval(input(''))"""
1774
b = """x = input('prompt')"""
1775
a = """x = eval(input('prompt'))"""
1779
b = """x = input(foo(5) + 9)"""
1780
a = """x = eval(input(foo(5) + 9))"""
1783
class Test_tuple_params(FixerTestCase):
1784
fixer = "tuple_params"
1786
def test_unchanged_1(self):
1787
s = """def foo(): pass"""
1790
def test_unchanged_2(self):
1791
s = """def foo(a, b, c): pass"""
1794
def test_unchanged_3(self):
1795
s = """def foo(a=3, b=4, c=5): pass"""
1800
def foo(((a, b), c)):
1804
def foo(xxx_todo_changeme):
1805
((a, b), c) = xxx_todo_changeme
1811
def foo(((a, b), c), d):
1815
def foo(xxx_todo_changeme, d):
1816
((a, b), c) = xxx_todo_changeme
1822
def foo(((a, b), c), d) -> e:
1826
def foo(xxx_todo_changeme, d) -> e:
1827
((a, b), c) = xxx_todo_changeme
1831
def test_semicolon(self):
1833
def foo(((a, b), c)): x = 5; y = 7"""
1836
def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
1839
def test_keywords(self):
1841
def foo(((a, b), c), d, e=5) -> z:
1845
def foo(xxx_todo_changeme, d, e=5) -> z:
1846
((a, b), c) = xxx_todo_changeme
1850
def test_varargs(self):
1852
def foo(((a, b), c), d, *vargs, **kwargs) -> z:
1856
def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z:
1857
((a, b), c) = xxx_todo_changeme
1861
def test_multi_1(self):
1863
def foo(((a, b), c), (d, e, f)) -> z:
1867
def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
1868
((a, b), c) = xxx_todo_changeme
1869
(d, e, f) = xxx_todo_changeme1
1873
def test_multi_2(self):
1875
def foo(x, ((a, b), c), d, (e, f, g), y) -> z:
1879
def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z:
1880
((a, b), c) = xxx_todo_changeme
1881
(e, f, g) = xxx_todo_changeme1
1885
def test_docstring(self):
1887
def foo(((a, b), c), (d, e, f)) -> z:
1892
def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
1894
((a, b), c) = xxx_todo_changeme
1895
(d, e, f) = xxx_todo_changeme1
1899
def test_lambda_no_change(self):
1900
s = """lambda x: x + 5"""
1903
def test_lambda_parens_single_arg(self):
1904
b = """lambda (x): x + 5"""
1905
a = """lambda x: x + 5"""
1908
b = """lambda(x): x + 5"""
1909
a = """lambda x: x + 5"""
1912
b = """lambda ((((x)))): x + 5"""
1913
a = """lambda x: x + 5"""
1916
b = """lambda((((x)))): x + 5"""
1917
a = """lambda x: x + 5"""
1920
def test_lambda_simple(self):
1921
b = """lambda (x, y): x + f(y)"""
1922
a = """lambda x_y: x_y[0] + f(x_y[1])"""
1925
b = """lambda(x, y): x + f(y)"""
1926
a = """lambda x_y: x_y[0] + f(x_y[1])"""
1929
b = """lambda (((x, y))): x + f(y)"""
1930
a = """lambda x_y: x_y[0] + f(x_y[1])"""
1933
b = """lambda(((x, y))): x + f(y)"""
1934
a = """lambda x_y: x_y[0] + f(x_y[1])"""
1937
def test_lambda_one_tuple(self):
1938
b = """lambda (x,): x + f(x)"""
1939
a = """lambda x1: x1[0] + f(x1[0])"""
1942
b = """lambda (((x,))): x + f(x)"""
1943
a = """lambda x1: x1[0] + f(x1[0])"""
1946
def test_lambda_simple_multi_use(self):
1947
b = """lambda (x, y): x + x + f(x) + x"""
1948
a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]"""
1951
def test_lambda_simple_reverse(self):
1952
b = """lambda (x, y): y + x"""
1953
a = """lambda x_y: x_y[1] + x_y[0]"""
1956
def test_lambda_nested(self):
1957
b = """lambda (x, (y, z)): x + y + z"""
1958
a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
1961
b = """lambda (((x, (y, z)))): x + y + z"""
1962
a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
1965
def test_lambda_nested_multi_use(self):
1966
b = """lambda (x, (y, z)): x + y + f(y)"""
1967
a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])"""
1970
class Test_methodattrs(FixerTestCase):
1971
fixer = "methodattrs"
1973
attrs = ["func", "self", "class"]
1976
for attr in self.attrs:
1977
b = "a.im_%s" % attr
1979
a = "a.__self__.__class__"
1981
a = "a.__%s__" % attr
1984
b = "self.foo.im_%s.foo_bar" % attr
1986
a = "self.foo.__self__.__class__.foo_bar"
1988
a = "self.foo.__%s__.foo_bar" % attr
1991
def test_unchanged(self):
1992
for attr in self.attrs:
1993
s = "foo(im_%s + 5)" % attr
1996
s = "f(foo.__%s__)" % attr
1999
s = "f(foo.__%s__.foo)" % attr
2002
class Test_next(FixerTestCase):
2011
b = """a.b.c.d.next()"""
2012
a = """next(a.b.c.d)"""
2016
b = """(a + b).next()"""
2017
a = """next((a + b))"""
2021
b = """a().next()"""
2026
b = """a().next() + b"""
2027
a = """next(a()) + b"""
2031
b = """c( a().next() + b)"""
2032
a = """c( next(a()) + b)"""
2035
def test_prefix_preservation_1(self):
2048
def test_prefix_preservation_2(self):
2063
def test_prefix_preservation_3(self):
2076
self.check(b, a, ignore_warnings=True)
2078
def test_prefix_preservation_4(self):
2093
self.check(b, a, ignore_warnings=True)
2095
def test_prefix_preservation_5(self):
2108
self.check(b, a, ignore_warnings=True)
2110
def test_prefix_preservation_6(self):
2123
def test_method_1(self):
2136
def test_method_2(self):
2149
def test_method_3(self):
2162
def test_method_4(self):
2165
def __init__(self, foo):
2176
def __init__(self, foo):
2187
def test_method_unchanged(self):
2190
def next(self, a, b):
2195
def test_shadowing_assign_simple(self):
2200
def next(self, a, b):
2203
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2205
def test_shadowing_assign_tuple_1(self):
2210
def next(self, a, b):
2213
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2215
def test_shadowing_assign_tuple_2(self):
2217
(a, (b, (next, c)), a) = foo
2220
def next(self, a, b):
2223
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2225
def test_shadowing_assign_list_1(self):
2230
def next(self, a, b):
2233
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2235
def test_shadowing_assign_list_2(self):
2237
[a, [b, [next, c]], a] = foo
2240
def next(self, a, b):
2243
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2245
def test_builtin_assign(self):
2248
__builtin__.next = foo
2251
def next(self, a, b):
2254
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2256
def test_builtin_assign_in_tuple(self):
2259
(a, __builtin__.next) = foo
2262
def next(self, a, b):
2265
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2267
def test_builtin_assign_in_list(self):
2270
[a, __builtin__.next] = foo
2273
def next(self, a, b):
2276
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2278
def test_assign_to_next(self):
2284
def next(self, a, b):
2289
def test_assign_to_next_in_tuple(self):
2295
def next(self, a, b):
2300
def test_assign_to_next_in_list(self):
2306
def next(self, a, b):
2311
def test_shadowing_import_1(self):
2313
import foo.bar as next
2316
def next(self, a, b):
2319
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2321
def test_shadowing_import_2(self):
2323
import bar, bar.foo as next
2326
def next(self, a, b):
2329
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2331
def test_shadowing_import_3(self):
2333
import bar, bar.foo as next, baz
2336
def next(self, a, b):
2339
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2341
def test_shadowing_import_from_1(self):
2346
def next(self, a, b):
2349
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2351
def test_shadowing_import_from_2(self):
2353
from x.a import next
2356
def next(self, a, b):
2359
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2361
def test_shadowing_import_from_3(self):
2363
from x import a, next, b
2366
def next(self, a, b):
2369
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2371
def test_shadowing_import_from_4(self):
2373
from x.a import a, next, b
2376
def next(self, a, b):
2379
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2381
def test_shadowing_funcdef_1(self):
2387
def next(self, a, b):
2390
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2392
def test_shadowing_funcdef_2(self):
2413
self.warns(b, a, "Calls to builtin next() possibly shadowed")
2415
def test_shadowing_global_1(self):
2421
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2423
def test_shadowing_global_2(self):
2429
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2431
def test_shadowing_for_simple(self):
2439
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2441
def test_shadowing_for_tuple_1(self):
2443
for next, b in it():
2449
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2451
def test_shadowing_for_tuple_2(self):
2453
for a, (next, c), b in it():
2459
self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2461
def test_noncall_access_1(self):
2462
b = """gnext = g.next"""
2463
a = """gnext = g.__next__"""
2466
def test_noncall_access_2(self):
2467
b = """f(g.next + 5)"""
2468
a = """f(g.__next__ + 5)"""
2471
def test_noncall_access_3(self):
2472
b = """f(g().next + 5)"""
2473
a = """f(g().__next__ + 5)"""
2476
class Test_nonzero(FixerTestCase):
2482
def __nonzero__(self):
2495
def __nonzero__(self):
2505
def test_unchanged_1(self):
2513
def test_unchanged_2(self):
2516
def __nonzero__(self, a):
2521
def test_unchanged_func(self):
2523
def __nonzero__(self):
2528
class Test_numliterals(FixerTestCase):
2529
fixer = "numliterals"
2531
def test_octal_1(self):
2536
def test_long_int_1(self):
2541
def test_long_int_2(self):
2546
def test_long_hex(self):
2551
def test_comments_and_spacing(self):
2556
b = """b = 0755 # spam"""
2557
a = """b = 0o755 # spam"""
2560
def test_unchanged_int(self):
2564
def test_unchanged_float(self):
2568
def test_unchanged_octal(self):
2572
def test_unchanged_hex(self):
2576
def test_unchanged_exp(self):
2580
def test_unchanged_complex_int(self):
2584
def test_unchanged_complex_float(self):
2585
s = """5.4 + 4.9j"""
2588
def test_unchanged_complex_bare(self):
2594
class Test_renames(FixerTestCase):
2597
modules = {"sys": ("maxint", "maxsize"),
2600
def test_import_from(self):
2601
for mod, (old, new) in list(self.modules.items()):
2602
b = "from %s import %s" % (mod, old)
2603
a = "from %s import %s" % (mod, new)
2606
s = "from foo import %s" % old
2609
def test_import_from_as(self):
2610
for mod, (old, new) in list(self.modules.items()):
2611
b = "from %s import %s as foo_bar" % (mod, old)
2612
a = "from %s import %s as foo_bar" % (mod, new)
2615
def test_import_module_usage(self):
2616
for mod, (old, new) in list(self.modules.items()):
2620
""" % (mod, mod, mod, old)
2624
""" % (mod, mod, mod, new)
2627
def XXX_test_from_import_usage(self):
2628
# not implemented yet
2629
for mod, (old, new) in list(self.modules.items()):
2633
""" % (mod, old, mod, old)
2637
""" % (mod, new, mod, new)
2640
class Test_unicode(FixerTestCase):
2643
def test_unicode_call(self):
2644
b = """unicode(x, y, z)"""
2645
a = """str(x, y, z)"""
2648
def test_unicode_literal_1(self):
2653
def test_unicode_literal_2(self):
2658
def test_unicode_literal_3(self):
2663
class Test_callable(FixerTestCase):
2666
def test_prefix_preservation(self):
2667
b = """callable( x)"""
2668
a = """hasattr( x, '__call__')"""
2671
b = """if callable(x): pass"""
2672
a = """if hasattr(x, '__call__'): pass"""
2675
def test_callable_call(self):
2676
b = """callable(x)"""
2677
a = """hasattr(x, '__call__')"""
2680
def test_callable_should_not_change(self):
2681
a = """callable(*x)"""
2684
a = """callable(x, y)"""
2687
a = """callable(x, kw=y)"""
2690
a = """callable()"""
2693
class Test_filter(FixerTestCase):
2696
def test_prefix_preservation(self):
2697
b = """x = filter( foo, 'abc' )"""
2698
a = """x = list(filter( foo, 'abc' ))"""
2701
b = """x = filter( None , 'abc' )"""
2702
a = """x = [_f for _f in 'abc' if _f]"""
2705
def test_filter_basic(self):
2706
b = """x = filter(None, 'abc')"""
2707
a = """x = [_f for _f in 'abc' if _f]"""
2710
b = """x = len(filter(f, 'abc'))"""
2711
a = """x = len(list(filter(f, 'abc')))"""
2714
b = """x = filter(lambda x: x%2 == 0, range(10))"""
2715
a = """x = [x for x in range(10) if x%2 == 0]"""
2718
# Note the parens around x
2719
b = """x = filter(lambda (x): x%2 == 0, range(10))"""
2720
a = """x = [x for x in range(10) if x%2 == 0]"""
2723
# XXX This (rare) case is not supported
2724
## b = """x = filter(f, 'abc')[0]"""
2725
## a = """x = list(filter(f, 'abc'))[0]"""
2728
def test_filter_nochange(self):
2729
a = """b.join(filter(f, 'abc'))"""
2731
a = """(a + foo(5)).join(filter(f, 'abc'))"""
2733
a = """iter(filter(f, 'abc'))"""
2735
a = """list(filter(f, 'abc'))"""
2737
a = """list(filter(f, 'abc'))[0]"""
2739
a = """set(filter(f, 'abc'))"""
2741
a = """set(filter(f, 'abc')).pop()"""
2743
a = """tuple(filter(f, 'abc'))"""
2745
a = """any(filter(f, 'abc'))"""
2747
a = """all(filter(f, 'abc'))"""
2749
a = """sum(filter(f, 'abc'))"""
2751
a = """sorted(filter(f, 'abc'))"""
2753
a = """sorted(filter(f, 'abc'), key=blah)"""
2755
a = """sorted(filter(f, 'abc'), key=blah)[0]"""
2757
a = """for i in filter(f, 'abc'): pass"""
2759
a = """[x for x in filter(f, 'abc')]"""
2761
a = """(x for x in filter(f, 'abc'))"""
2764
def test_future_builtins(self):
2765
a = "from future_builtins import spam, filter; filter(f, 'ham')"
2768
b = """from future_builtins import spam; x = filter(f, 'abc')"""
2769
a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
2772
a = "from future_builtins import *; filter(f, 'ham')"
2775
class Test_map(FixerTestCase):
2778
def check(self, b, a):
2779
self.unchanged("from future_builtins import map; " + b, a)
2780
super(Test_map, self).check(b, a)
2782
def test_prefix_preservation(self):
2783
b = """x = map( f, 'abc' )"""
2784
a = """x = list(map( f, 'abc' ))"""
2787
def test_trailing_comment(self):
2788
b = """x = map(f, 'abc') # foo"""
2789
a = """x = list(map(f, 'abc')) # foo"""
2792
def test_map_basic(self):
2793
b = """x = map(f, 'abc')"""
2794
a = """x = list(map(f, 'abc'))"""
2797
b = """x = len(map(f, 'abc', 'def'))"""
2798
a = """x = len(list(map(f, 'abc', 'def')))"""
2801
b = """x = map(None, 'abc')"""
2802
a = """x = list('abc')"""
2805
b = """x = map(None, 'abc', 'def')"""
2806
a = """x = list(map(None, 'abc', 'def'))"""
2809
b = """x = map(lambda x: x+1, range(4))"""
2810
a = """x = [x+1 for x in range(4)]"""
2813
# Note the parens around x
2814
b = """x = map(lambda (x): x+1, range(4))"""
2815
a = """x = [x+1 for x in range(4)]"""
2828
self.warns(b, a, "You should use a for loop here")
2830
# XXX This (rare) case is not supported
2831
## b = """x = map(f, 'abc')[0]"""
2832
## a = """x = list(map(f, 'abc'))[0]"""
2835
def test_map_nochange(self):
2836
a = """b.join(map(f, 'abc'))"""
2838
a = """(a + foo(5)).join(map(f, 'abc'))"""
2840
a = """iter(map(f, 'abc'))"""
2842
a = """list(map(f, 'abc'))"""
2844
a = """list(map(f, 'abc'))[0]"""
2846
a = """set(map(f, 'abc'))"""
2848
a = """set(map(f, 'abc')).pop()"""
2850
a = """tuple(map(f, 'abc'))"""
2852
a = """any(map(f, 'abc'))"""
2854
a = """all(map(f, 'abc'))"""
2856
a = """sum(map(f, 'abc'))"""
2858
a = """sorted(map(f, 'abc'))"""
2860
a = """sorted(map(f, 'abc'), key=blah)"""
2862
a = """sorted(map(f, 'abc'), key=blah)[0]"""
2864
a = """for i in map(f, 'abc'): pass"""
2866
a = """[x for x in map(f, 'abc')]"""
2868
a = """(x for x in map(f, 'abc'))"""
2871
def test_future_builtins(self):
2872
a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
2875
b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
2876
a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
2879
a = "from future_builtins import *; map(f, 'ham')"
2882
class Test_zip(FixerTestCase):
2885
def check(self, b, a):
2886
self.unchanged("from future_builtins import zip; " + b, a)
2887
super(Test_zip, self).check(b, a)
2889
def test_zip_basic(self):
2890
b = """x = zip(a, b, c)"""
2891
a = """x = list(zip(a, b, c))"""
2894
b = """x = len(zip(a, b))"""
2895
a = """x = len(list(zip(a, b)))"""
2898
def test_zip_nochange(self):
2899
a = """b.join(zip(a, b))"""
2901
a = """(a + foo(5)).join(zip(a, b))"""
2903
a = """iter(zip(a, b))"""
2905
a = """list(zip(a, b))"""
2907
a = """list(zip(a, b))[0]"""
2909
a = """set(zip(a, b))"""
2911
a = """set(zip(a, b)).pop()"""
2913
a = """tuple(zip(a, b))"""
2915
a = """any(zip(a, b))"""
2917
a = """all(zip(a, b))"""
2919
a = """sum(zip(a, b))"""
2921
a = """sorted(zip(a, b))"""
2923
a = """sorted(zip(a, b), key=blah)"""
2925
a = """sorted(zip(a, b), key=blah)[0]"""
2927
a = """for i in zip(a, b): pass"""
2929
a = """[x for x in zip(a, b)]"""
2931
a = """(x for x in zip(a, b))"""
2934
def test_future_builtins(self):
2935
a = "from future_builtins import spam, zip, eggs; zip(a, b)"
2938
b = """from future_builtins import spam, eggs; x = zip(a, b)"""
2939
a = """from future_builtins import spam, eggs; x = list(zip(a, b))"""
2942
a = "from future_builtins import *; zip(a, b)"
2945
class Test_standarderror(FixerTestCase):
2946
fixer = "standarderror"
2949
b = """x = StandardError()"""
2950
a = """x = Exception()"""
2953
b = """x = StandardError(a, b, c)"""
2954
a = """x = Exception(a, b, c)"""
2957
b = """f(2 + StandardError(a, b, c))"""
2958
a = """f(2 + Exception(a, b, c))"""
2961
class Test_types(FixerTestCase):
2964
def test_basic_types_convert(self):
2965
b = """types.StringType"""
2969
b = """types.DictType"""
2973
b = """types . IntType"""
2977
b = """types.ListType"""
2981
b = """types.LongType"""
2985
b = """types.NoneType"""
2986
a = """type(None)"""
2989
class Test_idioms(FixerTestCase):
2992
def test_while(self):
2993
b = """while 1: foo()"""
2994
a = """while True: foo()"""
2997
b = """while 1: foo()"""
2998
a = """while True: foo()"""
3011
def test_while_unchanged(self):
3012
s = """while 11: foo()"""
3015
s = """while 0: foo()"""
3018
s = """while foo(): foo()"""
3021
s = """while []: foo()"""
3024
def test_eq_simple(self):
3025
b = """type(x) == T"""
3026
a = """isinstance(x, T)"""
3029
b = """if type(x) == T: pass"""
3030
a = """if isinstance(x, T): pass"""
3033
def test_eq_reverse(self):
3034
b = """T == type(x)"""
3035
a = """isinstance(x, T)"""
3038
b = """if T == type(x): pass"""
3039
a = """if isinstance(x, T): pass"""
3042
def test_eq_expression(self):
3043
b = """type(x+y) == d.get('T')"""
3044
a = """isinstance(x+y, d.get('T'))"""
3047
b = """type( x + y) == d.get('T')"""
3048
a = """isinstance(x + y, d.get('T'))"""
3051
def test_is_simple(self):
3052
b = """type(x) is T"""
3053
a = """isinstance(x, T)"""
3056
b = """if type(x) is T: pass"""
3057
a = """if isinstance(x, T): pass"""
3060
def test_is_reverse(self):
3061
b = """T is type(x)"""
3062
a = """isinstance(x, T)"""
3065
b = """if T is type(x): pass"""
3066
a = """if isinstance(x, T): pass"""
3069
def test_is_expression(self):
3070
b = """type(x+y) is d.get('T')"""
3071
a = """isinstance(x+y, d.get('T'))"""
3074
b = """type( x + y) is d.get('T')"""
3075
a = """isinstance(x + y, d.get('T'))"""
3078
def test_is_not_simple(self):
3079
b = """type(x) is not T"""
3080
a = """not isinstance(x, T)"""
3083
b = """if type(x) is not T: pass"""
3084
a = """if not isinstance(x, T): pass"""
3087
def test_is_not_reverse(self):
3088
b = """T is not type(x)"""
3089
a = """not isinstance(x, T)"""
3092
b = """if T is not type(x): pass"""
3093
a = """if not isinstance(x, T): pass"""
3096
def test_is_not_expression(self):
3097
b = """type(x+y) is not d.get('T')"""
3098
a = """not isinstance(x+y, d.get('T'))"""
3101
b = """type( x + y) is not d.get('T')"""
3102
a = """not isinstance(x + y, d.get('T'))"""
3105
def test_ne_simple(self):
3106
b = """type(x) != T"""
3107
a = """not isinstance(x, T)"""
3110
b = """if type(x) != T: pass"""
3111
a = """if not isinstance(x, T): pass"""
3114
def test_ne_reverse(self):
3115
b = """T != type(x)"""
3116
a = """not isinstance(x, T)"""
3119
b = """if T != type(x): pass"""
3120
a = """if not isinstance(x, T): pass"""
3123
def test_ne_expression(self):
3124
b = """type(x+y) != d.get('T')"""
3125
a = """not isinstance(x+y, d.get('T'))"""
3128
b = """type( x + y) != d.get('T')"""
3129
a = """not isinstance(x + y, d.get('T'))"""
3132
def test_type_unchanged(self):
3133
a = """type(x).__name__"""
3136
def test_sort_list_call(self):
3149
v = list(foo(b) + d)
3154
v = sorted(foo(b) + d)
3196
def test_sort_simple_expr(self):
3225
v = sorted(b.keys())
3236
v = sorted(foo(b) + d)
3278
def test_sort_unchanged(self):
3293
class Test_basestring(FixerTestCase):
3294
fixer = "basestring"
3296
def test_basestring(self):
3297
b = """isinstance(x, basestring)"""
3298
a = """isinstance(x, str)"""
3301
class Test_buffer(FixerTestCase):
3304
def test_buffer(self):
3305
b = """x = buffer(y)"""
3306
a = """x = memoryview(y)"""
3309
class Test_future(FixerTestCase):
3312
def test_future(self):
3313
b = """from __future__ import braces"""
3317
b = """# comment\nfrom __future__ import braces"""
3318
a = """# comment\n"""
3321
b = """from __future__ import braces\n# comment"""
3322
a = """\n# comment"""
3325
def test_run_order(self):
3326
self.assert_runs_after('print')
3328
class Test_itertools(FixerTestCase):
3331
def checkall(self, before, after):
3332
# Because we need to check with and without the itertools prefix
3333
# and on each of the three functions, these loops make it all
3335
for i in ('itertools.', ''):
3336
for f in ('map', 'filter', 'zip'):
3337
b = before %(i+'i'+f)
3342
# A simple example -- test_1 covers exactly the same thing,
3343
# but it's not quite as clear.
3344
b = "itertools.izip(a, b)"
3354
b = """itertools.ifilterfalse(a, b)"""
3355
a = """itertools.filterfalse(a, b)"""
3359
b = """ifilterfalse(a, b)"""
3360
a = """filterfalse(a, b)"""
3363
def test_space_1(self):
3368
def test_space_2(self):
3369
b = """ itertools.ifilterfalse(a, b)"""
3370
a = """ itertools.filterfalse(a, b)"""
3373
def test_run_order(self):
3374
self.assert_runs_after('map', 'zip', 'filter')
3376
class Test_itertools_imports(FixerTestCase):
3377
fixer = 'itertools_imports'
3379
def test_reduced(self):
3380
b = "from itertools import imap, izip, foo"
3381
a = "from itertools import foo"
3384
b = "from itertools import bar, imap, izip, foo"
3385
a = "from itertools import bar, foo"
3388
def test_comments(self):
3389
b = "#foo\nfrom itertools import imap, izip"
3393
def test_none(self):
3394
b = "from itertools import imap, izip"
3398
b = "from itertools import izip"
3402
def test_import_as(self):
3403
b = "from itertools import izip, bar as bang, imap"
3404
a = "from itertools import bar as bang"
3407
s = "from itertools import bar as bang"
3410
def test_ifilter(self):
3411
b = "from itertools import ifilterfalse"
3412
a = "from itertools import filterfalse"
3415
b = "from itertools import imap, ifilterfalse, foo"
3416
a = "from itertools import filterfalse, foo"
3419
b = "from itertools import bar, ifilterfalse, foo"
3420
a = "from itertools import bar, filterfalse, foo"
3424
def test_unchanged(self):
3425
s = "from itertools import foo"
3428
class Test_import(FixerTestCase):
3432
super(Test_import, self).setUp()
3433
# Need to replace fix_import's exists method
3434
# so we can check that it's doing the right thing
3435
self.files_checked = []
3436
self.present_files = set()
3437
self.always_exists = True
3438
def fake_exists(name):
3439
self.files_checked.append(name)
3440
return self.always_exists or (name in self.present_files)
3442
from ..fixes import fix_import
3443
fix_import.exists = fake_exists
3446
from lib2to3.fixes import fix_import
3447
fix_import.exists = os.path.exists
3449
def check_both(self, b, a):
3450
self.always_exists = True
3451
super(Test_import, self).check(b, a)
3452
self.always_exists = False
3453
super(Test_import, self).unchanged(b)
3455
def test_files_checked(self):
3457
# Takes a unix path and returns a path with correct separators
3458
return os.path.pathsep.join(path.split("/"))
3460
self.always_exists = False
3461
self.present_files = set(['__init__.py'])
3462
expected_extensions = ('.py', os.path.pathsep, '.pyc', '.so',
3464
names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
3466
for name in names_to_test:
3467
self.files_checked = []
3468
self.filename = name
3469
self.unchanged("import jam")
3471
if os.path.dirname(name):
3472
name = os.path.dirname(name) + '/jam'
3475
expected_checks = set(name + ext for ext in expected_extensions)
3476
expected_checks.add("__init__.py")
3478
self.assertEqual(set(self.files_checked), expected_checks)
3480
def test_not_in_package(self):
3482
self.always_exists = False
3483
self.present_files = set(["bar.py"])
3486
def test_in_package(self):
3488
a = "from . import bar"
3489
self.always_exists = False
3490
self.present_files = set(["__init__.py", "bar.py"])
3493
def test_comments_and_indent(self):
3494
b = "import bar # Foo"
3495
a = "from . import bar # Foo"
3498
def test_from(self):
3499
b = "from foo import bar, baz"
3500
a = "from .foo import bar, baz"
3501
self.check_both(b, a)
3503
b = "from foo import bar"
3504
a = "from .foo import bar"
3505
self.check_both(b, a)
3507
b = "from foo import (bar, baz)"
3508
a = "from .foo import (bar, baz)"
3509
self.check_both(b, a)
3511
def test_dotted_from(self):
3512
b = "from green.eggs import ham"
3513
a = "from .green.eggs import ham"
3514
self.check_both(b, a)
3516
def test_from_as(self):
3517
b = "from green.eggs import ham as spam"
3518
a = "from .green.eggs import ham as spam"
3519
self.check_both(b, a)
3521
def test_import(self):
3523
a = "from . import foo"
3524
self.check_both(b, a)
3526
b = "import foo, bar"
3527
a = "from . import foo, bar"
3528
self.check_both(b, a)
3530
b = "import foo, bar, x"
3531
a = "from . import foo, bar, x"
3532
self.check_both(b, a)
3534
b = "import x, y, z"
3535
a = "from . import x, y, z"
3536
self.check_both(b, a)
3538
def test_import_as(self):
3539
b = "import foo as x"
3540
a = "from . import foo as x"
3541
self.check_both(b, a)
3543
b = "import a as b, b as c, c as d"
3544
a = "from . import a as b, b as c, c as d"
3545
self.check_both(b, a)
3547
def test_local_and_absolute(self):
3548
self.always_exists = False
3549
self.present_files = set(["foo.py", "__init__.py"])
3551
s = "import foo, bar"
3552
self.warns_unchanged(s, "absolute and local imports together")
3554
def test_dotted_import(self):
3555
b = "import foo.bar"
3556
a = "from . import foo.bar"
3557
self.check_both(b, a)
3559
def test_dotted_import_as(self):
3560
b = "import foo.bar as bang"
3561
a = "from . import foo.bar as bang"
3562
self.check_both(b, a)
3564
def test_prefix(self):
3571
from . import foo.bar
3573
self.check_both(b, a)
3576
class Test_set_literal(FixerTestCase):
3578
fixer = "set_literal"
3580
def test_basic(self):
3581
b = """set([1, 2, 3])"""
3585
b = """set((1, 2, 3))"""
3596
b = """set((a, b))"""
3600
b = """set([a, b])"""
3603
b = """set((a*234, f(args=23)))"""
3604
a = """{a*234, f(args=23)}"""
3607
b = """set([a*23, f(23)])"""
3608
a = """{a*23, f(23)}"""
3611
b = """set([a-234**23])"""
3612
a = """{a-234**23}"""
3615
def test_listcomps(self):
3616
b = """set([x for x in y])"""
3617
a = """{x for x in y}"""
3620
b = """set([x for x in y if x == m])"""
3621
a = """{x for x in y if x == m}"""
3624
b = """set([x for x in y for a in b])"""
3625
a = """{x for x in y for a in b}"""
3628
b = """set([f(x) - 23 for x in y])"""
3629
a = """{f(x) - 23 for x in y}"""
3632
def test_whitespace(self):
3633
b = """set( [1, 2])"""
3637
b = """set([1 , 2])"""
3641
b = """set([ 1 ])"""
3645
b = """set( [1] )"""
3649
b = """set([ 1, 2 ])"""
3653
b = """set([x for x in y ])"""
3654
a = """{x for x in y }"""
3664
def test_comments(self):
3665
b = """set((1, 2)) # Hi"""
3666
a = """{1, 2} # Hi"""
3669
# This isn't optimal behavior, but the fixer is optional.
3682
def test_unchanged(self):
3689
s = """set(a, b, c)"""
3692
# Don't transform generators because they might have to be lazy.
3693
s = """set(x for x in y)"""
3696
s = """set(x for x in y if z)"""
3699
s = """set(a*823-23**2 + f(23))"""
3703
class Test_sys_exc(FixerTestCase):
3708
a = "sys.exc_info()[0]"
3713
a = "sys.exc_info()[1]"
3717
b = "sys.exc_traceback"
3718
a = "sys.exc_info()[2]"
3722
b = "sys.exc_type # Foo"
3723
a = "sys.exc_info()[0] # Foo"
3728
a = "sys. exc_info()[0]"
3733
a = "sys .exc_info()[0]"
3737
class Test_paren(FixerTestCase):
3741
b = """[i for i in 1, 2 ]"""
3742
a = """[i for i in (1, 2) ]"""
3746
b = """[i for i in 1, 2, ]"""
3747
a = """[i for i in (1, 2,) ]"""
3751
b = """[i for i in 1, 2 ]"""
3752
a = """[i for i in (1, 2) ]"""
3756
b = """[i for i in 1, 2 if i]"""
3757
a = """[i for i in (1, 2) if i]"""
3761
b = """[i for i in 1, 2 ]"""
3762
a = """[i for i in (1, 2) ]"""
3766
b = """(i for i in 1, 2)"""
3767
a = """(i for i in (1, 2))"""
3771
b = """(i for i in 1 ,2 if i)"""
3772
a = """(i for i in (1 ,2) if i)"""
3775
def test_unchanged_0(self):
3776
s = """[i for i in (1, 2)]"""
3779
def test_unchanged_1(self):
3780
s = """[i for i in foo()]"""
3783
def test_unchanged_2(self):
3784
s = """[i for i in (1, 2) if nothing]"""
3787
def test_unchanged_3(self):
3788
s = """(i for i in (1, 2))"""
3791
def test_unchanged_4(self):
3792
s = """[i for i in m]"""
3795
class Test_metaclass(FixerTestCase):
3799
def test_unchanged(self):
3800
self.unchanged("class X(): pass")
3801
self.unchanged("class X(object): pass")
3802
self.unchanged("class X(object1, object2): pass")
3803
self.unchanged("class X(object1, object2, object3): pass")
3804
self.unchanged("class X(metaclass=Meta): pass")
3805
self.unchanged("class X(b, arg=23, metclass=Meta): pass")
3806
self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
3810
def __metaclass__(self): pass
3820
def test_comments(self):
3824
__metaclass__ = AppleMeta
3827
class X(metaclass=AppleMeta):
3835
__metaclass__ = Meta
3839
class X(metaclass=Meta):
3845
def test_meta(self):
3846
# no-parent class, odd body
3853
class X(metaclass=Q):
3858
# one parent class, no body
3859
b = """class X(object): __metaclass__ = Q"""
3860
a = """class X(object, metaclass=Q): pass"""
3864
# one parent, simple body
3867
__metaclass__ = Meta
3871
class X(object, metaclass=Meta):
3878
__metaclass__ = Meta; x = 4; g = 23
3881
class X(metaclass=Meta):
3886
# one parent, simple body, __metaclass__ last
3890
__metaclass__ = Meta
3893
class X(object, metaclass=Meta):
3898
# redefining __metaclass__
3906
class X(metaclass=B):
3911
# multiple inheritance, simple body
3913
class X(clsA, clsB):
3914
__metaclass__ = Meta
3918
class X(clsA, clsB, metaclass=Meta):
3923
# keywords in the class statement
3924
b = """class m(a, arg=23): __metaclass__ = Meta"""
3925
a = """class m(a, arg=23, metaclass=Meta): pass"""
3929
class X(expression(2 + 4)):
3930
__metaclass__ = Meta
3933
class X(expression(2 + 4), metaclass=Meta):
3939
class X(expression(2 + 4), x**4):
3940
__metaclass__ = Meta
3943
class X(expression(2 + 4), x**4, metaclass=Meta):
3950
__metaclass__ = Meta
3954
class X(metaclass=Meta):
3960
class Test_getcwdu(FixerTestCase):
3964
def test_basic(self):
3965
b = """os.getcwdu"""
3969
b = """os.getcwdu()"""
3970
a = """os.getcwd()"""
3973
b = """meth = os.getcwdu"""
3974
a = """meth = os.getcwd"""
3977
b = """os.getcwdu(args)"""
3978
a = """os.getcwd(args)"""
3981
def test_comment(self):
3982
b = """os.getcwdu() # Foo"""
3983
a = """os.getcwd() # Foo"""
3986
def test_unchanged(self):
3987
s = """os.getcwd()"""
3993
s = """os.getcwdb()"""
3996
def test_indentation(self):
4007
def test_multilation(self):
4008
b = """os .getcwdu()"""
4009
a = """os .getcwd()"""
4012
b = """os. getcwdu"""
4013
a = """os. getcwd"""
4016
b = """os.getcwdu ( )"""
4017
a = """os.getcwd ( )"""
4021
if __name__ == "__main__":
4023
support.run_all_tests(__main__)