Debian Codegraph @

cpython27/Lib/lib2to3/tests/test_fixers.py

raw symbols SCIP
1 """ Test suite for the fixer modules """
2
3 # Python imports
4 import os
5 import unittest
6 from itertools import chain
7 from operator import itemgetter
8
9 # Local imports
10 from lib2to3 import pygram, pytree, refactor, fixer_util
11 from lib2to3.tests import support
12
13
14 class FixerTestCase(support.TestCase):
15
16 # Other test cases can subclass this class and replace "fixer_pkg" with
17 # their own.
18 def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None):
19 if fix_list is None:
20 fix_list = [self.fixer]
21 self.refactor = support.get_refactorer(fixer_pkg, fix_list, options)
22 self.fixer_log = []
23 self.filename = u"<string>"
24
25 for fixer in chain(self.refactor.pre_order,
26 self.refactor.post_order):
27 fixer.log = self.fixer_log
28
29 def _check(self, before, after):
30 before = support.reformat(before)
31 after = support.reformat(after)
32 tree = self.refactor.refactor_string(before, self.filename)
33 self.assertEqual(after, unicode(tree))
34 return tree
35
36 def check(self, before, after, ignore_warnings=False):
37 tree = self._check(before, after)
38 self.assertTrue(tree.was_changed)
39 if not ignore_warnings:
40 self.assertEqual(self.fixer_log, [])
41
42 def warns(self, before, after, message, unchanged=False):
43 tree = self._check(before, after)
44 self.assertIn(message, "".join(self.fixer_log))
45 if not unchanged:
46 self.assertTrue(tree.was_changed)
47
48 def warns_unchanged(self, before, message):
49 self.warns(before, before, message, unchanged=True)
50
51 def unchanged(self, before, ignore_warnings=False):
52 self._check(before, before)
53 if not ignore_warnings:
54 self.assertEqual(self.fixer_log, [])
55
56 def assert_runs_after(self, *names):
57 fixes = [self.fixer]
58 fixes.extend(names)
59 r = support.get_refactorer("lib2to3", fixes)
60 (pre, post) = r.get_fixers()
61 n = "fix_" + self.fixer
62 if post and post[-1].__class__.__module__.endswith(n):
63 # We're the last fixer to run
64 return
65 if pre and pre[-1].__class__.__module__.endswith(n) and not post:
66 # We're the last in pre and post is empty
67 return
68 self.fail("Fixer run order (%s) is incorrect; %s should be last."\
69 %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
70
71 class Test_ne(FixerTestCase):
72 fixer = "ne"
73
74 def test_basic(self):
75 b = """if x <> y:
76 pass"""
77
78 a = """if x != y:
79 pass"""
80 self.check(b, a)
81
82 def test_no_spaces(self):
83 b = """if x<>y:
84 pass"""
85
86 a = """if x!=y:
87 pass"""
88 self.check(b, a)
89
90 def test_chained(self):
91 b = """if x<>y<>z:
92 pass"""
93
94 a = """if x!=y!=z:
95 pass"""
96 self.check(b, a)
97
98 class Test_has_key(FixerTestCase):
99 fixer = "has_key"
100
101 def test_1(self):
102 b = """x = d.has_key("x") or d.has_key("y")"""
103 a = """x = "x" in d or "y" in d"""
104 self.check(b, a)
105
106 def test_2(self):
107 b = """x = a.b.c.d.has_key("x") ** 3"""
108 a = """x = ("x" in a.b.c.d) ** 3"""
109 self.check(b, a)
110
111 def test_3(self):
112 b = """x = a.b.has_key(1 + 2).__repr__()"""
113 a = """x = (1 + 2 in a.b).__repr__()"""
114 self.check(b, a)
115
116 def test_4(self):
117 b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
118 a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
119 self.check(b, a)
120
121 def test_5(self):
122 b = """x = a.has_key(f or g)"""
123 a = """x = (f or g) in a"""
124 self.check(b, a)
125
126 def test_6(self):
127 b = """x = a + b.has_key(c)"""
128 a = """x = a + (c in b)"""
129 self.check(b, a)
130
131 def test_7(self):
132 b = """x = a.has_key(lambda: 12)"""
133 a = """x = (lambda: 12) in a"""
134 self.check(b, a)
135
136 def test_8(self):
137 b = """x = a.has_key(a for a in b)"""
138 a = """x = (a for a in b) in a"""
139 self.check(b, a)
140
141 def test_9(self):
142 b = """if not a.has_key(b): pass"""
143 a = """if b not in a: pass"""
144 self.check(b, a)
145
146 def test_10(self):
147 b = """if not a.has_key(b).__repr__(): pass"""
148 a = """if not (b in a).__repr__(): pass"""
149 self.check(b, a)
150
151 def test_11(self):
152 b = """if not a.has_key(b) ** 2: pass"""
153 a = """if not (b in a) ** 2: pass"""
154 self.check(b, a)
155
156 class Test_apply(FixerTestCase):
157 fixer = "apply"
158
159 def test_1(self):
160 b = """x = apply(f, g + h)"""
161 a = """x = f(*g + h)"""
162 self.check(b, a)
163
164 def test_2(self):
165 b = """y = apply(f, g, h)"""
166 a = """y = f(*g, **h)"""
167 self.check(b, a)
168
169 def test_3(self):
170 b = """z = apply(fs[0], g or h, h or g)"""
171 a = """z = fs[0](*g or h, **h or g)"""
172 self.check(b, a)
173
174 def test_4(self):
175 b = """apply(f, (x, y) + t)"""
176 a = """f(*(x, y) + t)"""
177 self.check(b, a)
178
179 def test_5(self):
180 b = """apply(f, args,)"""
181 a = """f(*args)"""
182 self.check(b, a)
183
184 def test_6(self):
185 b = """apply(f, args, kwds,)"""
186 a = """f(*args, **kwds)"""
187 self.check(b, a)
188
189 # Test that complex functions are parenthesized
190
191 def test_complex_1(self):
192 b = """x = apply(f+g, args)"""
193 a = """x = (f+g)(*args)"""
194 self.check(b, a)
195
196 def test_complex_2(self):
197 b = """x = apply(f*g, args)"""
198 a = """x = (f*g)(*args)"""
199 self.check(b, a)
200
201 def test_complex_3(self):
202 b = """x = apply(f**g, args)"""
203 a = """x = (f**g)(*args)"""
204 self.check(b, a)
205
206 # But dotted names etc. not
207
208 def test_dotted_name(self):
209 b = """x = apply(f.g, args)"""
210 a = """x = f.g(*args)"""
211 self.check(b, a)
212
213 def test_subscript(self):
214 b = """x = apply(f[x], args)"""
215 a = """x = f[x](*args)"""
216 self.check(b, a)
217
218 def test_call(self):
219 b = """x = apply(f(), args)"""
220 a = """x = f()(*args)"""
221 self.check(b, a)
222
223 # Extreme case
224 def test_extreme(self):
225 b = """x = apply(a.b.c.d.e.f, args, kwds)"""
226 a = """x = a.b.c.d.e.f(*args, **kwds)"""
227 self.check(b, a)
228
229 # XXX Comments in weird places still get lost
230 def test_weird_comments(self):
231 b = """apply( # foo
232 f, # bar
233 args)"""
234 a = """f(*args)"""
235 self.check(b, a)
236
237 # These should *not* be touched
238
239 def test_unchanged_1(self):
240 s = """apply()"""
241 self.unchanged(s)
242
243 def test_unchanged_2(self):
244 s = """apply(f)"""
245 self.unchanged(s)
246
247 def test_unchanged_3(self):
248 s = """apply(f,)"""
249 self.unchanged(s)
250
251 def test_unchanged_4(self):
252 s = """apply(f, args, kwds, extras)"""
253 self.unchanged(s)
254
255 def test_unchanged_5(self):
256 s = """apply(f, *args, **kwds)"""
257 self.unchanged(s)
258
259 def test_unchanged_6(self):
260 s = """apply(f, *args)"""
261 self.unchanged(s)
262
263 def test_unchanged_6b(self):
264 s = """apply(f, **kwds)"""
265 self.unchanged(s)
266
267 def test_unchanged_7(self):
268 s = """apply(func=f, args=args, kwds=kwds)"""
269 self.unchanged(s)
270
271 def test_unchanged_8(self):
272 s = """apply(f, args=args, kwds=kwds)"""
273 self.unchanged(s)
274
275 def test_unchanged_9(self):
276 s = """apply(f, args, kwds=kwds)"""
277 self.unchanged(s)
278
279 def test_space_1(self):
280 a = """apply( f, args, kwds)"""
281 b = """f(*args, **kwds)"""
282 self.check(a, b)
283
284 def test_space_2(self):
285 a = """apply( f ,args,kwds )"""
286 b = """f(*args, **kwds)"""
287 self.check(a, b)
288
289 class Test_intern(FixerTestCase):
290 fixer = "intern"
291
292 def test_prefix_preservation(self):
293 b = """x = intern( a )"""
294 a = """import sys\nx = sys.intern( a )"""
295 self.check(b, a)
296
297 b = """y = intern("b" # test
298 )"""
299 a = """import sys\ny = sys.intern("b" # test
300 )"""
301 self.check(b, a)
302
303 b = """z = intern(a+b+c.d, )"""
304 a = """import sys\nz = sys.intern(a+b+c.d, )"""
305 self.check(b, a)
306
307 def test(self):
308 b = """x = intern(a)"""
309 a = """import sys\nx = sys.intern(a)"""
310 self.check(b, a)
311
312 b = """z = intern(a+b+c.d,)"""
313 a = """import sys\nz = sys.intern(a+b+c.d,)"""
314 self.check(b, a)
315
316 b = """intern("y%s" % 5).replace("y", "")"""
317 a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
318 self.check(b, a)
319
320 # These should not be refactored
321
322 def test_unchanged(self):
323 s = """intern(a=1)"""
324 self.unchanged(s)
325
326 s = """intern(f, g)"""
327 self.unchanged(s)
328
329 s = """intern(*h)"""
330 self.unchanged(s)
331
332 s = """intern(**i)"""
333 self.unchanged(s)
334
335 s = """intern()"""
336 self.unchanged(s)
337
338 class Test_reduce(FixerTestCase):
339 fixer = "reduce"
340
341 def test_simple_call(self):
342 b = "reduce(a, b, c)"
343 a = "from functools import reduce\nreduce(a, b, c)"
344 self.check(b, a)
345
346 def test_bug_7253(self):
347 # fix_tuple_params was being bad and orphaning nodes in the tree.
348 b = "def x(arg): reduce(sum, [])"
349 a = "from functools import reduce\ndef x(arg): reduce(sum, [])"
350 self.check(b, a)
351
352 def test_call_with_lambda(self):
353 b = "reduce(lambda x, y: x + y, seq)"
354 a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)"
355 self.check(b, a)
356
357 def test_unchanged(self):
358 s = "reduce(a)"
359 self.unchanged(s)
360
361 s = "reduce(a, b=42)"
362 self.unchanged(s)
363
364 s = "reduce(a, b, c, d)"
365 self.unchanged(s)
366
367 s = "reduce(**c)"
368 self.unchanged(s)
369
370 s = "reduce()"
371 self.unchanged(s)
372
373 class Test_print(FixerTestCase):
374 fixer = "print"
375
376 def test_prefix_preservation(self):
377 b = """print 1, 1+1, 1+1+1"""
378 a = """print(1, 1+1, 1+1+1)"""
379 self.check(b, a)
380
381 def test_idempotency(self):
382 s = """print()"""
383 self.unchanged(s)
384
385 s = """print('')"""
386 self.unchanged(s)
387
388 def test_idempotency_print_as_function(self):
389 self.refactor.driver.grammar = pygram.python_grammar_no_print_statement
390 s = """print(1, 1+1, 1+1+1)"""
391 self.unchanged(s)
392
393 s = """print()"""
394 self.unchanged(s)
395
396 s = """print('')"""
397 self.unchanged(s)
398
399 def test_1(self):
400 b = """print 1, 1+1, 1+1+1"""
401 a = """print(1, 1+1, 1+1+1)"""
402 self.check(b, a)
403
404 def test_2(self):
405 b = """print 1, 2"""
406 a = """print(1, 2)"""
407 self.check(b, a)
408
409 def test_3(self):
410 b = """print"""
411 a = """print()"""
412 self.check(b, a)
413
414 def test_4(self):
415 # from bug 3000
416 b = """print whatever; print"""
417 a = """print(whatever); print()"""
418 self.check(b, a)
419
420 def test_5(self):
421 b = """print; print whatever;"""
422 a = """print(); print(whatever);"""
423 self.check(b, a)
424
425 def test_tuple(self):
426 b = """print (a, b, c)"""
427 a = """print((a, b, c))"""
428 self.check(b, a)
429
430 # trailing commas
431
432 def test_trailing_comma_1(self):
433 b = """print 1, 2, 3,"""
434 a = """print(1, 2, 3, end=' ')"""
435 self.check(b, a)
436
437 def test_trailing_comma_2(self):
438 b = """print 1, 2,"""
439 a = """print(1, 2, end=' ')"""
440 self.check(b, a)
441
442 def test_trailing_comma_3(self):
443 b = """print 1,"""
444 a = """print(1, end=' ')"""
445 self.check(b, a)
446
447 # >> stuff
448
449 def test_vargs_without_trailing_comma(self):
450 b = """print >>sys.stderr, 1, 2, 3"""
451 a = """print(1, 2, 3, file=sys.stderr)"""
452 self.check(b, a)
453
454 def test_with_trailing_comma(self):
455 b = """print >>sys.stderr, 1, 2,"""
456 a = """print(1, 2, end=' ', file=sys.stderr)"""
457 self.check(b, a)
458
459 def test_no_trailing_comma(self):
460 b = """print >>sys.stderr, 1+1"""
461 a = """print(1+1, file=sys.stderr)"""
462 self.check(b, a)
463
464 def test_spaces_before_file(self):
465 b = """print >> sys.stderr"""
466 a = """print(file=sys.stderr)"""
467 self.check(b, a)
468
469 def test_with_future_print_function(self):
470 s = "from __future__ import print_function\n" \
471 "print('Hai!', end=' ')"
472 self.unchanged(s)
473
474 b = "print 'Hello, world!'"
475 a = "print('Hello, world!')"
476 self.check(b, a)
477
478
479 class Test_exec(FixerTestCase):
480 fixer = "exec"
481
482 def test_prefix_preservation(self):
483 b = """ exec code in ns1, ns2"""
484 a = """ exec(code, ns1, ns2)"""
485 self.check(b, a)
486
487 def test_basic(self):
488 b = """exec code"""
489 a = """exec(code)"""
490 self.check(b, a)
491
492 def test_with_globals(self):
493 b = """exec code in ns"""
494 a = """exec(code, ns)"""
495 self.check(b, a)
496
497 def test_with_globals_locals(self):
498 b = """exec code in ns1, ns2"""
499 a = """exec(code, ns1, ns2)"""
500 self.check(b, a)
501
502 def test_complex_1(self):
503 b = """exec (a.b()) in ns"""
504 a = """exec((a.b()), ns)"""
505 self.check(b, a)
506
507 def test_complex_2(self):
508 b = """exec a.b() + c in ns"""
509 a = """exec(a.b() + c, ns)"""
510 self.check(b, a)
511
512 # These should not be touched
513
514 def test_unchanged_1(self):
515 s = """exec(code)"""
516 self.unchanged(s)
517
518 def test_unchanged_2(self):
519 s = """exec (code)"""
520 self.unchanged(s)
521
522 def test_unchanged_3(self):
523 s = """exec(code, ns)"""
524 self.unchanged(s)
525
526 def test_unchanged_4(self):
527 s = """exec(code, ns1, ns2)"""
528 self.unchanged(s)
529
530 class Test_repr(FixerTestCase):
531 fixer = "repr"
532
533 def test_prefix_preservation(self):
534 b = """x = `1 + 2`"""
535 a = """x = repr(1 + 2)"""
536 self.check(b, a)
537
538 def test_simple_1(self):
539 b = """x = `1 + 2`"""
540 a = """x = repr(1 + 2)"""
541 self.check(b, a)
542
543 def test_simple_2(self):
544 b = """y = `x`"""
545 a = """y = repr(x)"""
546 self.check(b, a)
547
548 def test_complex(self):
549 b = """z = `y`.__repr__()"""
550 a = """z = repr(y).__repr__()"""
551 self.check(b, a)
552
553 def test_tuple(self):
554 b = """x = `1, 2, 3`"""
555 a = """x = repr((1, 2, 3))"""
556 self.check(b, a)
557
558 def test_nested(self):
559 b = """x = `1 + `2``"""
560 a = """x = repr(1 + repr(2))"""
561 self.check(b, a)
562
563 def test_nested_tuples(self):
564 b = """x = `1, 2 + `3, 4``"""
565 a = """x = repr((1, 2 + repr((3, 4))))"""
566 self.check(b, a)
567
568 class Test_except(FixerTestCase):
569 fixer = "except"
570
571 def test_prefix_preservation(self):
572 b = """
573 try:
574 pass
575 except (RuntimeError, ImportError), e:
576 pass"""
577 a = """
578 try:
579 pass
580 except (RuntimeError, ImportError) as e:
581 pass"""
582 self.check(b, a)
583
584 def test_simple(self):
585 b = """
586 try:
587 pass
588 except Foo, e:
589 pass"""
590 a = """
591 try:
592 pass
593 except Foo as e:
594 pass"""
595 self.check(b, a)
596
597 def test_simple_no_space_before_target(self):
598 b = """
599 try:
600 pass
601 except Foo,e:
602 pass"""
603 a = """
604 try:
605 pass
606 except Foo as e:
607 pass"""
608 self.check(b, a)
609
610 def test_tuple_unpack(self):
611 b = """
612 def foo():
613 try:
614 pass
615 except Exception, (f, e):
616 pass
617 except ImportError, e:
618 pass"""
619
620 a = """
621 def foo():
622 try:
623 pass
624 except Exception as xxx_todo_changeme:
625 (f, e) = xxx_todo_changeme.args
626 pass
627 except ImportError as e:
628 pass"""
629 self.check(b, a)
630
631 def test_multi_class(self):
632 b = """
633 try:
634 pass
635 except (RuntimeError, ImportError), e:
636 pass"""
637
638 a = """
639 try:
640 pass
641 except (RuntimeError, ImportError) as e:
642 pass"""
643 self.check(b, a)
644
645 def test_list_unpack(self):
646 b = """
647 try:
648 pass
649 except Exception, [a, b]:
650 pass"""
651
652 a = """
653 try:
654 pass
655 except Exception as xxx_todo_changeme:
656 [a, b] = xxx_todo_changeme.args
657 pass"""
658 self.check(b, a)
659
660 def test_weird_target_1(self):
661 b = """
662 try:
663 pass
664 except Exception, d[5]:
665 pass"""
666
667 a = """
668 try:
669 pass
670 except Exception as xxx_todo_changeme:
671 d[5] = xxx_todo_changeme
672 pass"""
673 self.check(b, a)
674
675 def test_weird_target_2(self):
676 b = """
677 try:
678 pass
679 except Exception, a.foo:
680 pass"""
681
682 a = """
683 try:
684 pass
685 except Exception as xxx_todo_changeme:
686 a.foo = xxx_todo_changeme
687 pass"""
688 self.check(b, a)
689
690 def test_weird_target_3(self):
691 b = """
692 try:
693 pass
694 except Exception, a().foo:
695 pass"""
696
697 a = """
698 try:
699 pass
700 except Exception as xxx_todo_changeme:
701 a().foo = xxx_todo_changeme
702 pass"""
703 self.check(b, a)
704
705 def test_bare_except(self):
706 b = """
707 try:
708 pass
709 except Exception, a:
710 pass
711 except:
712 pass"""
713
714 a = """
715 try:
716 pass
717 except Exception as a:
718 pass
719 except:
720 pass"""
721 self.check(b, a)
722
723 def test_bare_except_and_else_finally(self):
724 b = """
725 try:
726 pass
727 except Exception, a:
728 pass
729 except:
730 pass
731 else:
732 pass
733 finally:
734 pass"""
735
736 a = """
737 try:
738 pass
739 except Exception as a:
740 pass
741 except:
742 pass
743 else:
744 pass
745 finally:
746 pass"""
747 self.check(b, a)
748
749 def test_multi_fixed_excepts_before_bare_except(self):
750 b = """
751 try:
752 pass
753 except TypeError, b:
754 pass
755 except Exception, a:
756 pass
757 except:
758 pass"""
759
760 a = """
761 try:
762 pass
763 except TypeError as b:
764 pass
765 except Exception as a:
766 pass
767 except:
768 pass"""
769 self.check(b, a)
770
771 def test_one_line_suites(self):
772 b = """
773 try: raise TypeError
774 except TypeError, e:
775 pass
776 """
777 a = """
778 try: raise TypeError
779 except TypeError as e:
780 pass
781 """
782 self.check(b, a)
783 b = """
784 try:
785 raise TypeError
786 except TypeError, e: pass
787 """
788 a = """
789 try:
790 raise TypeError
791 except TypeError as e: pass
792 """
793 self.check(b, a)
794 b = """
795 try: raise TypeError
796 except TypeError, e: pass
797 """
798 a = """
799 try: raise TypeError
800 except TypeError as e: pass
801 """
802 self.check(b, a)
803 b = """
804 try: raise TypeError
805 except TypeError, e: pass
806 else: function()
807 finally: done()
808 """
809 a = """
810 try: raise TypeError
811 except TypeError as e: pass
812 else: function()
813 finally: done()
814 """
815 self.check(b, a)
816
817 # These should not be touched:
818
819 def test_unchanged_1(self):
820 s = """
821 try:
822 pass
823 except:
824 pass"""
825 self.unchanged(s)
826
827 def test_unchanged_2(self):
828 s = """
829 try:
830 pass
831 except Exception:
832 pass"""
833 self.unchanged(s)
834
835 def test_unchanged_3(self):
836 s = """
837 try:
838 pass
839 except (Exception, SystemExit):
840 pass"""
841 self.unchanged(s)
842
843 class Test_raise(FixerTestCase):
844 fixer = "raise"
845
846 def test_basic(self):
847 b = """raise Exception, 5"""
848 a = """raise Exception(5)"""
849 self.check(b, a)
850
851 def test_prefix_preservation(self):
852 b = """raise Exception,5"""
853 a = """raise Exception(5)"""
854 self.check(b, a)
855
856 b = """raise Exception, 5"""
857 a = """raise Exception(5)"""
858 self.check(b, a)
859
860 def test_with_comments(self):
861 b = """raise Exception, 5 # foo"""
862 a = """raise Exception(5) # foo"""
863 self.check(b, a)
864
865 b = """raise E, (5, 6) % (a, b) # foo"""
866 a = """raise E((5, 6) % (a, b)) # foo"""
867 self.check(b, a)
868
869 b = """def foo():
870 raise Exception, 5, 6 # foo"""
871 a = """def foo():
872 raise Exception(5).with_traceback(6) # foo"""
873 self.check(b, a)
874
875 def test_None_value(self):
876 b = """raise Exception(5), None, tb"""
877 a = """raise Exception(5).with_traceback(tb)"""
878 self.check(b, a)
879
880 def test_tuple_value(self):
881 b = """raise Exception, (5, 6, 7)"""
882 a = """raise Exception(5, 6, 7)"""
883 self.check(b, a)
884
885 def test_tuple_detection(self):
886 b = """raise E, (5, 6) % (a, b)"""
887 a = """raise E((5, 6) % (a, b))"""
888 self.check(b, a)
889
890 def test_tuple_exc_1(self):
891 b = """raise (((E1, E2), E3), E4), V"""
892 a = """raise E1(V)"""
893 self.check(b, a)
894
895 def test_tuple_exc_2(self):
896 b = """raise (E1, (E2, E3), E4), V"""
897 a = """raise E1(V)"""
898 self.check(b, a)
899
900 # These should produce a warning
901
902 def test_string_exc(self):
903 s = """raise 'foo'"""
904 self.warns_unchanged(s, "Python 3 does not support string exceptions")
905
906 def test_string_exc_val(self):
907 s = """raise "foo", 5"""
908 self.warns_unchanged(s, "Python 3 does not support string exceptions")
909
910 def test_string_exc_val_tb(self):
911 s = """raise "foo", 5, 6"""
912 self.warns_unchanged(s, "Python 3 does not support string exceptions")
913
914 # These should result in traceback-assignment
915
916 def test_tb_1(self):
917 b = """def foo():
918 raise Exception, 5, 6"""
919 a = """def foo():
920 raise Exception(5).with_traceback(6)"""
921 self.check(b, a)
922
923 def test_tb_2(self):
924 b = """def foo():
925 a = 5
926 raise Exception, 5, 6
927 b = 6"""
928 a = """def foo():
929 a = 5
930 raise Exception(5).with_traceback(6)
931 b = 6"""
932 self.check(b, a)
933
934 def test_tb_3(self):
935 b = """def foo():
936 raise Exception,5,6"""
937 a = """def foo():
938 raise Exception(5).with_traceback(6)"""
939 self.check(b, a)
940
941 def test_tb_4(self):
942 b = """def foo():
943 a = 5
944 raise Exception,5,6
945 b = 6"""
946 a = """def foo():
947 a = 5
948 raise Exception(5).with_traceback(6)
949 b = 6"""
950 self.check(b, a)
951
952 def test_tb_5(self):
953 b = """def foo():
954 raise Exception, (5, 6, 7), 6"""
955 a = """def foo():
956 raise Exception(5, 6, 7).with_traceback(6)"""
957 self.check(b, a)
958
959 def test_tb_6(self):
960 b = """def foo():
961 a = 5
962 raise Exception, (5, 6, 7), 6
963 b = 6"""
964 a = """def foo():
965 a = 5
966 raise Exception(5, 6, 7).with_traceback(6)
967 b = 6"""
968 self.check(b, a)
969
970 class Test_throw(FixerTestCase):
971 fixer = "throw"
972
973 def test_1(self):
974 b = """g.throw(Exception, 5)"""
975 a = """g.throw(Exception(5))"""
976 self.check(b, a)
977
978 def test_2(self):
979 b = """g.throw(Exception,5)"""
980 a = """g.throw(Exception(5))"""
981 self.check(b, a)
982
983 def test_3(self):
984 b = """g.throw(Exception, (5, 6, 7))"""
985 a = """g.throw(Exception(5, 6, 7))"""
986 self.check(b, a)
987
988 def test_4(self):
989 b = """5 + g.throw(Exception, 5)"""
990 a = """5 + g.throw(Exception(5))"""
991 self.check(b, a)
992
993 # These should produce warnings
994
995 def test_warn_1(self):
996 s = """g.throw("foo")"""
997 self.warns_unchanged(s, "Python 3 does not support string exceptions")
998
999 def test_warn_2(self):
1000 s = """g.throw("foo", 5)"""
1001 self.warns_unchanged(s, "Python 3 does not support string exceptions")
1002
1003 def test_warn_3(self):
1004 s = """g.throw("foo", 5, 6)"""
1005 self.warns_unchanged(s, "Python 3 does not support string exceptions")
1006
1007 # These should not be touched
1008
1009 def test_untouched_1(self):
1010 s = """g.throw(Exception)"""
1011 self.unchanged(s)
1012
1013 def test_untouched_2(self):
1014 s = """g.throw(Exception(5, 6))"""
1015 self.unchanged(s)
1016
1017 def test_untouched_3(self):
1018 s = """5 + g.throw(Exception(5, 6))"""
1019 self.unchanged(s)
1020
1021 # These should result in traceback-assignment
1022
1023 def test_tb_1(self):
1024 b = """def foo():
1025 g.throw(Exception, 5, 6)"""
1026 a = """def foo():
1027 g.throw(Exception(5).with_traceback(6))"""
1028 self.check(b, a)
1029
1030 def test_tb_2(self):
1031 b = """def foo():
1032 a = 5
1033 g.throw(Exception, 5, 6)
1034 b = 6"""
1035 a = """def foo():
1036 a = 5
1037 g.throw(Exception(5).with_traceback(6))
1038 b = 6"""
1039 self.check(b, a)
1040
1041 def test_tb_3(self):
1042 b = """def foo():
1043 g.throw(Exception,5,6)"""
1044 a = """def foo():
1045 g.throw(Exception(5).with_traceback(6))"""
1046 self.check(b, a)
1047
1048 def test_tb_4(self):
1049 b = """def foo():
1050 a = 5
1051 g.throw(Exception,5,6)
1052 b = 6"""
1053 a = """def foo():
1054 a = 5
1055 g.throw(Exception(5).with_traceback(6))
1056 b = 6"""
1057 self.check(b, a)
1058
1059 def test_tb_5(self):
1060 b = """def foo():
1061 g.throw(Exception, (5, 6, 7), 6)"""
1062 a = """def foo():
1063 g.throw(Exception(5, 6, 7).with_traceback(6))"""
1064 self.check(b, a)
1065
1066 def test_tb_6(self):
1067 b = """def foo():
1068 a = 5
1069 g.throw(Exception, (5, 6, 7), 6)
1070 b = 6"""
1071 a = """def foo():
1072 a = 5
1073 g.throw(Exception(5, 6, 7).with_traceback(6))
1074 b = 6"""
1075 self.check(b, a)
1076
1077 def test_tb_7(self):
1078 b = """def foo():
1079 a + g.throw(Exception, 5, 6)"""
1080 a = """def foo():
1081 a + g.throw(Exception(5).with_traceback(6))"""
1082 self.check(b, a)
1083
1084 def test_tb_8(self):
1085 b = """def foo():
1086 a = 5
1087 a + g.throw(Exception, 5, 6)
1088 b = 6"""
1089 a = """def foo():
1090 a = 5
1091 a + g.throw(Exception(5).with_traceback(6))
1092 b = 6"""
1093 self.check(b, a)
1094
1095 class Test_long(FixerTestCase):
1096 fixer = "long"
1097
1098 def test_1(self):
1099 b = """x = long(x)"""
1100 a = """x = int(x)"""
1101 self.check(b, a)
1102
1103 def test_2(self):
1104 b = """y = isinstance(x, long)"""
1105 a = """y = isinstance(x, int)"""
1106 self.check(b, a)
1107
1108 def test_3(self):
1109 b = """z = type(x) in (int, long)"""
1110 a = """z = type(x) in (int, int)"""
1111 self.check(b, a)
1112
1113 def test_unchanged(self):
1114 s = """long = True"""
1115 self.unchanged(s)
1116
1117 s = """s.long = True"""
1118 self.unchanged(s)
1119
1120 s = """def long(): pass"""
1121 self.unchanged(s)
1122
1123 s = """class long(): pass"""
1124 self.unchanged(s)
1125
1126 s = """def f(long): pass"""
1127 self.unchanged(s)
1128
1129 s = """def f(g, long): pass"""
1130 self.unchanged(s)
1131
1132 s = """def f(x, long=True): pass"""
1133 self.unchanged(s)
1134
1135 def test_prefix_preservation(self):
1136 b = """x = long( x )"""
1137 a = """x = int( x )"""
1138 self.check(b, a)
1139
1140
1141 class Test_execfile(FixerTestCase):
1142 fixer = "execfile"
1143
1144 def test_conversion(self):
1145 b = """execfile("fn")"""
1146 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'))"""
1147 self.check(b, a)
1148
1149 b = """execfile("fn", glob)"""
1150 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob)"""
1151 self.check(b, a)
1152
1153 b = """execfile("fn", glob, loc)"""
1154 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), glob, loc)"""
1155 self.check(b, a)
1156
1157 b = """execfile("fn", globals=glob)"""
1158 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob)"""
1159 self.check(b, a)
1160
1161 b = """execfile("fn", locals=loc)"""
1162 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), locals=loc)"""
1163 self.check(b, a)
1164
1165 b = """execfile("fn", globals=glob, locals=loc)"""
1166 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals=glob, locals=loc)"""
1167 self.check(b, a)
1168
1169 def test_spacing(self):
1170 b = """execfile( "fn" )"""
1171 a = """exec(compile(open( "fn", "rb" ).read(), "fn", 'exec'))"""
1172 self.check(b, a)
1173
1174 b = """execfile("fn", globals = glob)"""
1175 a = """exec(compile(open("fn", "rb").read(), "fn", 'exec'), globals = glob)"""
1176 self.check(b, a)
1177
1178
1179 class Test_isinstance(FixerTestCase):
1180 fixer = "isinstance"
1181
1182 def test_remove_multiple_items(self):
1183 b = """isinstance(x, (int, int, int))"""
1184 a = """isinstance(x, int)"""
1185 self.check(b, a)
1186
1187 b = """isinstance(x, (int, float, int, int, float))"""
1188 a = """isinstance(x, (int, float))"""
1189 self.check(b, a)
1190
1191 b = """isinstance(x, (int, float, int, int, float, str))"""
1192 a = """isinstance(x, (int, float, str))"""
1193 self.check(b, a)
1194
1195 b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
1196 a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
1197 self.check(b, a)
1198
1199 def test_prefix_preservation(self):
1200 b = """if isinstance( foo(), ( bar, bar, baz )) : pass"""
1201 a = """if isinstance( foo(), ( bar, baz )) : pass"""
1202 self.check(b, a)
1203
1204 def test_unchanged(self):
1205 self.unchanged("isinstance(x, (str, int))")
1206
1207 class Test_dict(FixerTestCase):
1208 fixer = "dict"
1209
1210 def test_prefix_preservation(self):
1211 b = "if d. keys ( ) : pass"
1212 a = "if list(d. keys ( )) : pass"
1213 self.check(b, a)
1214
1215 b = "if d. items ( ) : pass"
1216 a = "if list(d. items ( )) : pass"
1217 self.check(b, a)
1218
1219 b = "if d. iterkeys ( ) : pass"
1220 a = "if iter(d. keys ( )) : pass"
1221 self.check(b, a)
1222
1223 b = "[i for i in d. iterkeys( ) ]"
1224 a = "[i for i in d. keys( ) ]"
1225 self.check(b, a)
1226
1227 b = "if d. viewkeys ( ) : pass"
1228 a = "if d. keys ( ) : pass"
1229 self.check(b, a)
1230
1231 b = "[i for i in d. viewkeys( ) ]"
1232 a = "[i for i in d. keys( ) ]"
1233 self.check(b, a)
1234
1235 def test_trailing_comment(self):
1236 b = "d.keys() # foo"
1237 a = "list(d.keys()) # foo"
1238 self.check(b, a)
1239
1240 b = "d.items() # foo"
1241 a = "list(d.items()) # foo"
1242 self.check(b, a)
1243
1244 b = "d.iterkeys() # foo"
1245 a = "iter(d.keys()) # foo"
1246 self.check(b, a)
1247
1248 b = """[i for i in d.iterkeys() # foo
1249 ]"""
1250 a = """[i for i in d.keys() # foo
1251 ]"""
1252 self.check(b, a)
1253
1254 b = """[i for i in d.iterkeys() # foo
1255 ]"""
1256 a = """[i for i in d.keys() # foo
1257 ]"""
1258 self.check(b, a)
1259
1260 b = "d.viewitems() # foo"
1261 a = "d.items() # foo"
1262 self.check(b, a)
1263
1264 def test_unchanged(self):
1265 for wrapper in fixer_util.consuming_calls:
1266 s = "s = %s(d.keys())" % wrapper
1267 self.unchanged(s)
1268
1269 s = "s = %s(d.values())" % wrapper
1270 self.unchanged(s)
1271
1272 s = "s = %s(d.items())" % wrapper
1273 self.unchanged(s)
1274
1275 def test_01(self):
1276 b = "d.keys()"
1277 a = "list(d.keys())"
1278 self.check(b, a)
1279
1280 b = "a[0].foo().keys()"
1281 a = "list(a[0].foo().keys())"
1282 self.check(b, a)
1283
1284 def test_02(self):
1285 b = "d.items()"
1286 a = "list(d.items())"
1287 self.check(b, a)
1288
1289 def test_03(self):
1290 b = "d.values()"
1291 a = "list(d.values())"
1292 self.check(b, a)
1293
1294 def test_04(self):
1295 b = "d.iterkeys()"
1296 a = "iter(d.keys())"
1297 self.check(b, a)
1298
1299 def test_05(self):
1300 b = "d.iteritems()"
1301 a = "iter(d.items())"
1302 self.check(b, a)
1303
1304 def test_06(self):
1305 b = "d.itervalues()"
1306 a = "iter(d.values())"
1307 self.check(b, a)
1308
1309 def test_07(self):
1310 s = "list(d.keys())"
1311 self.unchanged(s)
1312
1313 def test_08(self):
1314 s = "sorted(d.keys())"
1315 self.unchanged(s)
1316
1317 def test_09(self):
1318 b = "iter(d.keys())"
1319 a = "iter(list(d.keys()))"
1320 self.check(b, a)
1321
1322 def test_10(self):
1323 b = "foo(d.keys())"
1324 a = "foo(list(d.keys()))"
1325 self.check(b, a)
1326
1327 def test_11(self):
1328 b = "for i in d.keys(): print i"
1329 a = "for i in list(d.keys()): print i"
1330 self.check(b, a)
1331
1332 def test_12(self):
1333 b = "for i in d.iterkeys(): print i"
1334 a = "for i in d.keys(): print i"
1335 self.check(b, a)
1336
1337 def test_13(self):
1338 b = "[i for i in d.keys()]"
1339 a = "[i for i in list(d.keys())]"
1340 self.check(b, a)
1341
1342 def test_14(self):
1343 b = "[i for i in d.iterkeys()]"
1344 a = "[i for i in d.keys()]"
1345 self.check(b, a)
1346
1347 def test_15(self):
1348 b = "(i for i in d.keys())"
1349 a = "(i for i in list(d.keys()))"
1350 self.check(b, a)
1351
1352 def test_16(self):
1353 b = "(i for i in d.iterkeys())"
1354 a = "(i for i in d.keys())"
1355 self.check(b, a)
1356
1357 def test_17(self):
1358 b = "iter(d.iterkeys())"
1359 a = "iter(d.keys())"
1360 self.check(b, a)
1361
1362 def test_18(self):
1363 b = "list(d.iterkeys())"
1364 a = "list(d.keys())"
1365 self.check(b, a)
1366
1367 def test_19(self):
1368 b = "sorted(d.iterkeys())"
1369 a = "sorted(d.keys())"
1370 self.check(b, a)
1371
1372 def test_20(self):
1373 b = "foo(d.iterkeys())"
1374 a = "foo(iter(d.keys()))"
1375 self.check(b, a)
1376
1377 def test_21(self):
1378 b = "print h.iterkeys().next()"
1379 a = "print iter(h.keys()).next()"
1380 self.check(b, a)
1381
1382 def test_22(self):
1383 b = "print h.keys()[0]"
1384 a = "print list(h.keys())[0]"
1385 self.check(b, a)
1386
1387 def test_23(self):
1388 b = "print list(h.iterkeys().next())"
1389 a = "print list(iter(h.keys()).next())"
1390 self.check(b, a)
1391
1392 def test_24(self):
1393 b = "for x in h.keys()[0]: print x"
1394 a = "for x in list(h.keys())[0]: print x"
1395 self.check(b, a)
1396
1397 def test_25(self):
1398 b = "d.viewkeys()"
1399 a = "d.keys()"
1400 self.check(b, a)
1401
1402 def test_26(self):
1403 b = "d.viewitems()"
1404 a = "d.items()"
1405 self.check(b, a)
1406
1407 def test_27(self):
1408 b = "d.viewvalues()"
1409 a = "d.values()"
1410 self.check(b, a)
1411
1412 def test_28(self):
1413 b = "[i for i in d.viewkeys()]"
1414 a = "[i for i in d.keys()]"
1415 self.check(b, a)
1416
1417 def test_29(self):
1418 b = "(i for i in d.viewkeys())"
1419 a = "(i for i in d.keys())"
1420 self.check(b, a)
1421
1422 def test_30(self):
1423 b = "iter(d.viewkeys())"
1424 a = "iter(d.keys())"
1425 self.check(b, a)
1426
1427 def test_31(self):
1428 b = "list(d.viewkeys())"
1429 a = "list(d.keys())"
1430 self.check(b, a)
1431
1432 def test_32(self):
1433 b = "sorted(d.viewkeys())"
1434 a = "sorted(d.keys())"
1435 self.check(b, a)
1436
1437 class Test_xrange(FixerTestCase):
1438 fixer = "xrange"
1439
1440 def test_prefix_preservation(self):
1441 b = """x = xrange( 10 )"""
1442 a = """x = range( 10 )"""
1443 self.check(b, a)
1444
1445 b = """x = xrange( 1 , 10 )"""
1446 a = """x = range( 1 , 10 )"""
1447 self.check(b, a)
1448
1449 b = """x = xrange( 0 , 10 , 2 )"""
1450 a = """x = range( 0 , 10 , 2 )"""
1451 self.check(b, a)
1452
1453 def test_single_arg(self):
1454 b = """x = xrange(10)"""
1455 a = """x = range(10)"""
1456 self.check(b, a)
1457
1458 def test_two_args(self):
1459 b = """x = xrange(1, 10)"""
1460 a = """x = range(1, 10)"""
1461 self.check(b, a)
1462
1463 def test_three_args(self):
1464 b = """x = xrange(0, 10, 2)"""
1465 a = """x = range(0, 10, 2)"""
1466 self.check(b, a)
1467
1468 def test_wrap_in_list(self):
1469 b = """x = range(10, 3, 9)"""
1470 a = """x = list(range(10, 3, 9))"""
1471 self.check(b, a)
1472
1473 b = """x = foo(range(10, 3, 9))"""
1474 a = """x = foo(list(range(10, 3, 9)))"""
1475 self.check(b, a)
1476
1477 b = """x = range(10, 3, 9) + [4]"""
1478 a = """x = list(range(10, 3, 9)) + [4]"""
1479 self.check(b, a)
1480
1481 b = """x = range(10)[::-1]"""
1482 a = """x = list(range(10))[::-1]"""
1483 self.check(b, a)
1484
1485 b = """x = range(10) [3]"""
1486 a = """x = list(range(10)) [3]"""
1487 self.check(b, a)
1488
1489 def test_xrange_in_for(self):
1490 b = """for i in xrange(10):\n j=i"""
1491 a = """for i in range(10):\n j=i"""
1492 self.check(b, a)
1493
1494 b = """[i for i in xrange(10)]"""
1495 a = """[i for i in range(10)]"""
1496 self.check(b, a)
1497
1498 def test_range_in_for(self):
1499 self.unchanged("for i in range(10): pass")
1500 self.unchanged("[i for i in range(10)]")
1501
1502 def test_in_contains_test(self):
1503 self.unchanged("x in range(10, 3, 9)")
1504
1505 def test_in_consuming_context(self):
1506 for call in fixer_util.consuming_calls:
1507 self.unchanged("a = %s(range(10))" % call)
1508
1509 class Test_xrange_with_reduce(FixerTestCase):
1510
1511 def setUp(self):
1512 super(Test_xrange_with_reduce, self).setUp(["xrange", "reduce"])
1513
1514 def test_double_transform(self):
1515 b = """reduce(x, xrange(5))"""
1516 a = """from functools import reduce
1517 reduce(x, range(5))"""
1518 self.check(b, a)
1519
1520 class Test_raw_input(FixerTestCase):
1521 fixer = "raw_input"
1522
1523 def test_prefix_preservation(self):
1524 b = """x = raw_input( )"""
1525 a = """x = input( )"""
1526 self.check(b, a)
1527
1528 b = """x = raw_input( '' )"""
1529 a = """x = input( '' )"""
1530 self.check(b, a)
1531
1532 def test_1(self):
1533 b = """x = raw_input()"""
1534 a = """x = input()"""
1535 self.check(b, a)
1536
1537 def test_2(self):
1538 b = """x = raw_input('')"""
1539 a = """x = input('')"""
1540 self.check(b, a)
1541
1542 def test_3(self):
1543 b = """x = raw_input('prompt')"""
1544 a = """x = input('prompt')"""
1545 self.check(b, a)
1546
1547 def test_4(self):
1548 b = """x = raw_input(foo(a) + 6)"""
1549 a = """x = input(foo(a) + 6)"""
1550 self.check(b, a)
1551
1552 def test_5(self):
1553 b = """x = raw_input(invite).split()"""
1554 a = """x = input(invite).split()"""
1555 self.check(b, a)
1556
1557 def test_6(self):
1558 b = """x = raw_input(invite) . split ()"""
1559 a = """x = input(invite) . split ()"""
1560 self.check(b, a)
1561
1562 def test_8(self):
1563 b = "x = int(raw_input())"
1564 a = "x = int(input())"
1565 self.check(b, a)
1566
1567 class Test_funcattrs(FixerTestCase):
1568 fixer = "funcattrs"
1569
1570 attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
1571
1572 def test(self):
1573 for attr in self.attrs:
1574 b = "a.func_%s" % attr
1575 a = "a.__%s__" % attr
1576 self.check(b, a)
1577
1578 b = "self.foo.func_%s.foo_bar" % attr
1579 a = "self.foo.__%s__.foo_bar" % attr
1580 self.check(b, a)
1581
1582 def test_unchanged(self):
1583 for attr in self.attrs:
1584 s = "foo(func_%s + 5)" % attr
1585 self.unchanged(s)
1586
1587 s = "f(foo.__%s__)" % attr
1588 self.unchanged(s)
1589
1590 s = "f(foo.__%s__.foo)" % attr
1591 self.unchanged(s)
1592
1593 class Test_xreadlines(FixerTestCase):
1594 fixer = "xreadlines"
1595
1596 def test_call(self):
1597 b = "for x in f.xreadlines(): pass"
1598 a = "for x in f: pass"
1599 self.check(b, a)
1600
1601 b = "for x in foo().xreadlines(): pass"
1602 a = "for x in foo(): pass"
1603 self.check(b, a)
1604
1605 b = "for x in (5 + foo()).xreadlines(): pass"
1606 a = "for x in (5 + foo()): pass"
1607 self.check(b, a)
1608
1609 def test_attr_ref(self):
1610 b = "foo(f.xreadlines + 5)"
1611 a = "foo(f.__iter__ + 5)"
1612 self.check(b, a)
1613
1614 b = "foo(f().xreadlines + 5)"
1615 a = "foo(f().__iter__ + 5)"
1616 self.check(b, a)
1617
1618 b = "foo((5 + f()).xreadlines + 5)"
1619 a = "foo((5 + f()).__iter__ + 5)"
1620 self.check(b, a)
1621
1622 def test_unchanged(self):
1623 s = "for x in f.xreadlines(5): pass"
1624 self.unchanged(s)
1625
1626 s = "for x in f.xreadlines(k=5): pass"
1627 self.unchanged(s)
1628
1629 s = "for x in f.xreadlines(*k, **v): pass"
1630 self.unchanged(s)
1631
1632 s = "foo(xreadlines)"
1633 self.unchanged(s)
1634
1635
1636 class ImportsFixerTests:
1637
1638 def test_import_module(self):
1639 for old, new in self.modules.items():
1640 b = "import %s" % old
1641 a = "import %s" % new
1642 self.check(b, a)
1643
1644 b = "import foo, %s, bar" % old
1645 a = "import foo, %s, bar" % new
1646 self.check(b, a)
1647
1648 def test_import_from(self):
1649 for old, new in self.modules.items():
1650 b = "from %s import foo" % old
1651 a = "from %s import foo" % new
1652 self.check(b, a)
1653
1654 b = "from %s import foo, bar" % old
1655 a = "from %s import foo, bar" % new
1656 self.check(b, a)
1657
1658 b = "from %s import (yes, no)" % old
1659 a = "from %s import (yes, no)" % new
1660 self.check(b, a)
1661
1662 def test_import_module_as(self):
1663 for old, new in self.modules.items():
1664 b = "import %s as foo_bar" % old
1665 a = "import %s as foo_bar" % new
1666 self.check(b, a)
1667
1668 b = "import %s as foo_bar" % old
1669 a = "import %s as foo_bar" % new
1670 self.check(b, a)
1671
1672 def test_import_from_as(self):
1673 for old, new in self.modules.items():
1674 b = "from %s import foo as bar" % old
1675 a = "from %s import foo as bar" % new
1676 self.check(b, a)
1677
1678 def test_star(self):
1679 for old, new in self.modules.items():
1680 b = "from %s import *" % old
1681 a = "from %s import *" % new
1682 self.check(b, a)
1683
1684 def test_import_module_usage(self):
1685 for old, new in self.modules.items():
1686 b = """
1687 import %s
1688 foo(%s.bar)
1689 """ % (old, old)
1690 a = """
1691 import %s
1692 foo(%s.bar)
1693 """ % (new, new)
1694 self.check(b, a)
1695
1696 b = """
1697 from %s import x
1698 %s = 23
1699 """ % (old, old)
1700 a = """
1701 from %s import x
1702 %s = 23
1703 """ % (new, old)
1704 self.check(b, a)
1705
1706 s = """
1707 def f():
1708 %s.method()
1709 """ % (old,)
1710 self.unchanged(s)
1711
1712 # test nested usage
1713 b = """
1714 import %s
1715 %s.bar(%s.foo)
1716 """ % (old, old, old)
1717 a = """
1718 import %s
1719 %s.bar(%s.foo)
1720 """ % (new, new, new)
1721 self.check(b, a)
1722
1723 b = """
1724 import %s
1725 x.%s
1726 """ % (old, old)
1727 a = """
1728 import %s
1729 x.%s
1730 """ % (new, old)
1731 self.check(b, a)
1732
1733
1734 class Test_imports(FixerTestCase, ImportsFixerTests):
1735 fixer = "imports"
1736 from ..fixes.fix_imports import MAPPING as modules
1737
1738 def test_multiple_imports(self):
1739 b = """import urlparse, cStringIO"""
1740 a = """import urllib.parse, io"""
1741 self.check(b, a)
1742
1743 def test_multiple_imports_as(self):
1744 b = """
1745 import copy_reg as bar, HTMLParser as foo, urlparse
1746 s = urlparse.spam(bar.foo())
1747 """
1748 a = """
1749 import copyreg as bar, html.parser as foo, urllib.parse
1750 s = urllib.parse.spam(bar.foo())
1751 """
1752 self.check(b, a)
1753
1754
1755 class Test_imports2(FixerTestCase, ImportsFixerTests):
1756 fixer = "imports2"
1757 from ..fixes.fix_imports2 import MAPPING as modules
1758
1759
1760 class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
1761
1762 def setUp(self):
1763 super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
1764 from ..fixes.fix_imports2 import MAPPING as mapping2
1765 self.modules = mapping2.copy()
1766 from ..fixes.fix_imports import MAPPING as mapping1
1767 for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
1768 self.modules[key] = mapping1[key]
1769
1770 def test_after_local_imports_refactoring(self):
1771 for fix in ("imports", "imports2"):
1772 self.fixer = fix
1773 self.assert_runs_after("import")
1774
1775
1776 class Test_urllib(FixerTestCase):
1777 fixer = "urllib"
1778 from ..fixes.fix_urllib import MAPPING as modules
1779
1780 def test_import_module(self):
1781 for old, changes in self.modules.items():
1782 b = "import %s" % old
1783 a = "import %s" % ", ".join(map(itemgetter(0), changes))
1784 self.check(b, a)
1785
1786 def test_import_from(self):
1787 for old, changes in self.modules.items():
1788 all_members = []
1789 for new, members in changes:
1790 for member in members:
1791 all_members.append(member)
1792 b = "from %s import %s" % (old, member)
1793 a = "from %s import %s" % (new, member)
1794 self.check(b, a)
1795
1796 s = "from foo import %s" % member
1797 self.unchanged(s)
1798
1799 b = "from %s import %s" % (old, ", ".join(members))
1800 a = "from %s import %s" % (new, ", ".join(members))
1801 self.check(b, a)
1802
1803 s = "from foo import %s" % ", ".join(members)
1804 self.unchanged(s)
1805
1806 # test the breaking of a module into multiple replacements
1807 b = "from %s import %s" % (old, ", ".join(all_members))
1808 a = "\n".join(["from %s import %s" % (new, ", ".join(members))
1809 for (new, members) in changes])
1810 self.check(b, a)
1811
1812 def test_import_module_as(self):
1813 for old in self.modules:
1814 s = "import %s as foo" % old
1815 self.warns_unchanged(s, "This module is now multiple modules")
1816
1817 def test_import_from_as(self):
1818 for old, changes in self.modules.items():
1819 for new, members in changes:
1820 for member in members:
1821 b = "from %s import %s as foo_bar" % (old, member)
1822 a = "from %s import %s as foo_bar" % (new, member)
1823 self.check(b, a)
1824 b = "from %s import %s as blah, %s" % (old, member, member)
1825 a = "from %s import %s as blah, %s" % (new, member, member)
1826 self.check(b, a)
1827
1828 def test_star(self):
1829 for old in self.modules:
1830 s = "from %s import *" % old
1831 self.warns_unchanged(s, "Cannot handle star imports")
1832
1833 def test_indented(self):
1834 b = """
1835 def foo():
1836 from urllib import urlencode, urlopen
1837 """
1838 a = """
1839 def foo():
1840 from urllib.parse import urlencode
1841 from urllib.request import urlopen
1842 """
1843 self.check(b, a)
1844
1845 b = """
1846 def foo():
1847 other()
1848 from urllib import urlencode, urlopen
1849 """
1850 a = """
1851 def foo():
1852 other()
1853 from urllib.parse import urlencode
1854 from urllib.request import urlopen
1855 """
1856 self.check(b, a)
1857
1858
1859
1860 def test_import_module_usage(self):
1861 for old, changes in self.modules.items():
1862 for new, members in changes:
1863 for member in members:
1864 new_import = ", ".join([n for (n, mems)
1865 in self.modules[old]])
1866 b = """
1867 import %s
1868 foo(%s.%s)
1869 """ % (old, old, member)
1870 a = """
1871 import %s
1872 foo(%s.%s)
1873 """ % (new_import, new, member)
1874 self.check(b, a)
1875 b = """
1876 import %s
1877 %s.%s(%s.%s)
1878 """ % (old, old, member, old, member)
1879 a = """
1880 import %s
1881 %s.%s(%s.%s)
1882 """ % (new_import, new, member, new, member)
1883 self.check(b, a)
1884
1885
1886 class Test_input(FixerTestCase):
1887 fixer = "input"
1888
1889 def test_prefix_preservation(self):
1890 b = """x = input( )"""
1891 a = """x = eval(input( ))"""
1892 self.check(b, a)
1893
1894 b = """x = input( '' )"""
1895 a = """x = eval(input( '' ))"""
1896 self.check(b, a)
1897
1898 def test_trailing_comment(self):
1899 b = """x = input() # foo"""
1900 a = """x = eval(input()) # foo"""
1901 self.check(b, a)
1902
1903 def test_idempotency(self):
1904 s = """x = eval(input())"""
1905 self.unchanged(s)
1906
1907 s = """x = eval(input(''))"""
1908 self.unchanged(s)
1909
1910 s = """x = eval(input(foo(5) + 9))"""
1911 self.unchanged(s)
1912
1913 def test_1(self):
1914 b = """x = input()"""
1915 a = """x = eval(input())"""
1916 self.check(b, a)
1917
1918 def test_2(self):
1919 b = """x = input('')"""
1920 a = """x = eval(input(''))"""
1921 self.check(b, a)
1922
1923 def test_3(self):
1924 b = """x = input('prompt')"""
1925 a = """x = eval(input('prompt'))"""
1926 self.check(b, a)
1927
1928 def test_4(self):
1929 b = """x = input(foo(5) + 9)"""
1930 a = """x = eval(input(foo(5) + 9))"""
1931 self.check(b, a)
1932
1933 class Test_tuple_params(FixerTestCase):
1934 fixer = "tuple_params"
1935
1936 def test_unchanged_1(self):
1937 s = """def foo(): pass"""
1938 self.unchanged(s)
1939
1940 def test_unchanged_2(self):
1941 s = """def foo(a, b, c): pass"""
1942 self.unchanged(s)
1943
1944 def test_unchanged_3(self):
1945 s = """def foo(a=3, b=4, c=5): pass"""
1946 self.unchanged(s)
1947
1948 def test_1(self):
1949 b = """
1950 def foo(((a, b), c)):
1951 x = 5"""
1952
1953 a = """
1954 def foo(xxx_todo_changeme):
1955 ((a, b), c) = xxx_todo_changeme
1956 x = 5"""
1957 self.check(b, a)
1958
1959 def test_2(self):
1960 b = """
1961 def foo(((a, b), c), d):
1962 x = 5"""
1963
1964 a = """
1965 def foo(xxx_todo_changeme, d):
1966 ((a, b), c) = xxx_todo_changeme
1967 x = 5"""
1968 self.check(b, a)
1969
1970 def test_3(self):
1971 b = """
1972 def foo(((a, b), c), d) -> e:
1973 x = 5"""
1974
1975 a = """
1976 def foo(xxx_todo_changeme, d) -> e:
1977 ((a, b), c) = xxx_todo_changeme
1978 x = 5"""
1979 self.check(b, a)
1980
1981 def test_semicolon(self):
1982 b = """
1983 def foo(((a, b), c)): x = 5; y = 7"""
1984
1985 a = """
1986 def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
1987 self.check(b, a)
1988
1989 def test_keywords(self):
1990 b = """
1991 def foo(((a, b), c), d, e=5) -> z:
1992 x = 5"""
1993
1994 a = """
1995 def foo(xxx_todo_changeme, d, e=5) -> z:
1996 ((a, b), c) = xxx_todo_changeme
1997 x = 5"""
1998 self.check(b, a)
1999
2000 def test_varargs(self):
2001 b = """
2002 def foo(((a, b), c), d, *vargs, **kwargs) -> z:
2003 x = 5"""
2004
2005 a = """
2006 def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z:
2007 ((a, b), c) = xxx_todo_changeme
2008 x = 5"""
2009 self.check(b, a)
2010
2011 def test_multi_1(self):
2012 b = """
2013 def foo(((a, b), c), (d, e, f)) -> z:
2014 x = 5"""
2015
2016 a = """
2017 def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
2018 ((a, b), c) = xxx_todo_changeme
2019 (d, e, f) = xxx_todo_changeme1
2020 x = 5"""
2021 self.check(b, a)
2022
2023 def test_multi_2(self):
2024 b = """
2025 def foo(x, ((a, b), c), d, (e, f, g), y) -> z:
2026 x = 5"""
2027
2028 a = """
2029 def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z:
2030 ((a, b), c) = xxx_todo_changeme
2031 (e, f, g) = xxx_todo_changeme1
2032 x = 5"""
2033 self.check(b, a)
2034
2035 def test_docstring(self):
2036 b = """
2037 def foo(((a, b), c), (d, e, f)) -> z:
2038 "foo foo foo foo"
2039 x = 5"""
2040
2041 a = """
2042 def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
2043 "foo foo foo foo"
2044 ((a, b), c) = xxx_todo_changeme
2045 (d, e, f) = xxx_todo_changeme1
2046 x = 5"""
2047 self.check(b, a)
2048
2049 def test_lambda_no_change(self):
2050 s = """lambda x: x + 5"""
2051 self.unchanged(s)
2052
2053 def test_lambda_parens_single_arg(self):
2054 b = """lambda (x): x + 5"""
2055 a = """lambda x: x + 5"""
2056 self.check(b, a)
2057
2058 b = """lambda(x): x + 5"""
2059 a = """lambda x: x + 5"""
2060 self.check(b, a)
2061
2062 b = """lambda ((((x)))): x + 5"""
2063 a = """lambda x: x + 5"""
2064 self.check(b, a)
2065
2066 b = """lambda((((x)))): x + 5"""
2067 a = """lambda x: x + 5"""
2068 self.check(b, a)
2069
2070 def test_lambda_simple(self):
2071 b = """lambda (x, y): x + f(y)"""
2072 a = """lambda x_y: x_y[0] + f(x_y[1])"""
2073 self.check(b, a)
2074
2075 b = """lambda(x, y): x + f(y)"""
2076 a = """lambda x_y: x_y[0] + f(x_y[1])"""
2077 self.check(b, a)
2078
2079 b = """lambda (((x, y))): x + f(y)"""
2080 a = """lambda x_y: x_y[0] + f(x_y[1])"""
2081 self.check(b, a)
2082
2083 b = """lambda(((x, y))): x + f(y)"""
2084 a = """lambda x_y: x_y[0] + f(x_y[1])"""
2085 self.check(b, a)
2086
2087 def test_lambda_one_tuple(self):
2088 b = """lambda (x,): x + f(x)"""
2089 a = """lambda x1: x1[0] + f(x1[0])"""
2090 self.check(b, a)
2091
2092 b = """lambda (((x,))): x + f(x)"""
2093 a = """lambda x1: x1[0] + f(x1[0])"""
2094 self.check(b, a)
2095
2096 def test_lambda_simple_multi_use(self):
2097 b = """lambda (x, y): x + x + f(x) + x"""
2098 a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]"""
2099 self.check(b, a)
2100
2101 def test_lambda_simple_reverse(self):
2102 b = """lambda (x, y): y + x"""
2103 a = """lambda x_y: x_y[1] + x_y[0]"""
2104 self.check(b, a)
2105
2106 def test_lambda_nested(self):
2107 b = """lambda (x, (y, z)): x + y + z"""
2108 a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
2109 self.check(b, a)
2110
2111 b = """lambda (((x, (y, z)))): x + y + z"""
2112 a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
2113 self.check(b, a)
2114
2115 def test_lambda_nested_multi_use(self):
2116 b = """lambda (x, (y, z)): x + y + f(y)"""
2117 a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])"""
2118 self.check(b, a)
2119
2120 class Test_methodattrs(FixerTestCase):
2121 fixer = "methodattrs"
2122
2123 attrs = ["func", "self", "class"]
2124
2125 def test(self):
2126 for attr in self.attrs:
2127 b = "a.im_%s" % attr
2128 if attr == "class":
2129 a = "a.__self__.__class__"
2130 else:
2131 a = "a.__%s__" % attr
2132 self.check(b, a)
2133
2134 b = "self.foo.im_%s.foo_bar" % attr
2135 if attr == "class":
2136 a = "self.foo.__self__.__class__.foo_bar"
2137 else:
2138 a = "self.foo.__%s__.foo_bar" % attr
2139 self.check(b, a)
2140
2141 def test_unchanged(self):
2142 for attr in self.attrs:
2143 s = "foo(im_%s + 5)" % attr
2144 self.unchanged(s)
2145
2146 s = "f(foo.__%s__)" % attr
2147 self.unchanged(s)
2148
2149 s = "f(foo.__%s__.foo)" % attr
2150 self.unchanged(s)
2151
2152 class Test_next(FixerTestCase):
2153 fixer = "next"
2154
2155 def test_1(self):
2156 b = """it.next()"""
2157 a = """next(it)"""
2158 self.check(b, a)
2159
2160 def test_2(self):
2161 b = """a.b.c.d.next()"""
2162 a = """next(a.b.c.d)"""
2163 self.check(b, a)
2164
2165 def test_3(self):
2166 b = """(a + b).next()"""
2167 a = """next((a + b))"""
2168 self.check(b, a)
2169
2170 def test_4(self):
2171 b = """a().next()"""
2172 a = """next(a())"""
2173 self.check(b, a)
2174
2175 def test_5(self):
2176 b = """a().next() + b"""
2177 a = """next(a()) + b"""
2178 self.check(b, a)
2179
2180 def test_6(self):
2181 b = """c( a().next() + b)"""
2182 a = """c( next(a()) + b)"""
2183 self.check(b, a)
2184
2185 def test_prefix_preservation_1(self):
2186 b = """
2187 for a in b:
2188 foo(a)
2189 a.next()
2190 """
2191 a = """
2192 for a in b:
2193 foo(a)
2194 next(a)
2195 """
2196 self.check(b, a)
2197
2198 def test_prefix_preservation_2(self):
2199 b = """
2200 for a in b:
2201 foo(a) # abc
2202 # def
2203 a.next()
2204 """
2205 a = """
2206 for a in b:
2207 foo(a) # abc
2208 # def
2209 next(a)
2210 """
2211 self.check(b, a)
2212
2213 def test_prefix_preservation_3(self):
2214 b = """
2215 next = 5
2216 for a in b:
2217 foo(a)
2218 a.next()
2219 """
2220 a = """
2221 next = 5
2222 for a in b:
2223 foo(a)
2224 a.__next__()
2225 """
2226 self.check(b, a, ignore_warnings=True)
2227
2228 def test_prefix_preservation_4(self):
2229 b = """
2230 next = 5
2231 for a in b:
2232 foo(a) # abc
2233 # def
2234 a.next()
2235 """
2236 a = """
2237 next = 5
2238 for a in b:
2239 foo(a) # abc
2240 # def
2241 a.__next__()
2242 """
2243 self.check(b, a, ignore_warnings=True)
2244
2245 def test_prefix_preservation_5(self):
2246 b = """
2247 next = 5
2248 for a in b:
2249 foo(foo(a), # abc
2250 a.next())
2251 """
2252 a = """
2253 next = 5
2254 for a in b:
2255 foo(foo(a), # abc
2256 a.__next__())
2257 """
2258 self.check(b, a, ignore_warnings=True)
2259
2260 def test_prefix_preservation_6(self):
2261 b = """
2262 for a in b:
2263 foo(foo(a), # abc
2264 a.next())
2265 """
2266 a = """
2267 for a in b:
2268 foo(foo(a), # abc
2269 next(a))
2270 """
2271 self.check(b, a)
2272
2273 def test_method_1(self):
2274 b = """
2275 class A:
2276 def next(self):
2277 pass
2278 """
2279 a = """
2280 class A:
2281 def __next__(self):
2282 pass
2283 """
2284 self.check(b, a)
2285
2286 def test_method_2(self):
2287 b = """
2288 class A(object):
2289 def next(self):
2290 pass
2291 """
2292 a = """
2293 class A(object):
2294 def __next__(self):
2295 pass
2296 """
2297 self.check(b, a)
2298
2299 def test_method_3(self):
2300 b = """
2301 class A:
2302 def next(x):
2303 pass
2304 """
2305 a = """
2306 class A:
2307 def __next__(x):
2308 pass
2309 """
2310 self.check(b, a)
2311
2312 def test_method_4(self):
2313 b = """
2314 class A:
2315 def __init__(self, foo):
2316 self.foo = foo
2317
2318 def next(self):
2319 pass
2320
2321 def __iter__(self):
2322 return self
2323 """
2324 a = """
2325 class A:
2326 def __init__(self, foo):
2327 self.foo = foo
2328
2329 def __next__(self):
2330 pass
2331
2332 def __iter__(self):
2333 return self
2334 """
2335 self.check(b, a)
2336
2337 def test_method_unchanged(self):
2338 s = """
2339 class A:
2340 def next(self, a, b):
2341 pass
2342 """
2343 self.unchanged(s)
2344
2345 def test_shadowing_assign_simple(self):
2346 s = """
2347 next = foo
2348
2349 class A:
2350 def next(self, a, b):
2351 pass
2352 """
2353 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2354
2355 def test_shadowing_assign_tuple_1(self):
2356 s = """
2357 (next, a) = foo
2358
2359 class A:
2360 def next(self, a, b):
2361 pass
2362 """
2363 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2364
2365 def test_shadowing_assign_tuple_2(self):
2366 s = """
2367 (a, (b, (next, c)), a) = foo
2368
2369 class A:
2370 def next(self, a, b):
2371 pass
2372 """
2373 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2374
2375 def test_shadowing_assign_list_1(self):
2376 s = """
2377 [next, a] = foo
2378
2379 class A:
2380 def next(self, a, b):
2381 pass
2382 """
2383 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2384
2385 def test_shadowing_assign_list_2(self):
2386 s = """
2387 [a, [b, [next, c]], a] = foo
2388
2389 class A:
2390 def next(self, a, b):
2391 pass
2392 """
2393 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2394
2395 def test_builtin_assign(self):
2396 s = """
2397 def foo():
2398 __builtin__.next = foo
2399
2400 class A:
2401 def next(self, a, b):
2402 pass
2403 """
2404 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2405
2406 def test_builtin_assign_in_tuple(self):
2407 s = """
2408 def foo():
2409 (a, __builtin__.next) = foo
2410
2411 class A:
2412 def next(self, a, b):
2413 pass
2414 """
2415 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2416
2417 def test_builtin_assign_in_list(self):
2418 s = """
2419 def foo():
2420 [a, __builtin__.next] = foo
2421
2422 class A:
2423 def next(self, a, b):
2424 pass
2425 """
2426 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2427
2428 def test_assign_to_next(self):
2429 s = """
2430 def foo():
2431 A.next = foo
2432
2433 class A:
2434 def next(self, a, b):
2435 pass
2436 """
2437 self.unchanged(s)
2438
2439 def test_assign_to_next_in_tuple(self):
2440 s = """
2441 def foo():
2442 (a, A.next) = foo
2443
2444 class A:
2445 def next(self, a, b):
2446 pass
2447 """
2448 self.unchanged(s)
2449
2450 def test_assign_to_next_in_list(self):
2451 s = """
2452 def foo():
2453 [a, A.next] = foo
2454
2455 class A:
2456 def next(self, a, b):
2457 pass
2458 """
2459 self.unchanged(s)
2460
2461 def test_shadowing_import_1(self):
2462 s = """
2463 import foo.bar as next
2464
2465 class A:
2466 def next(self, a, b):
2467 pass
2468 """
2469 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2470
2471 def test_shadowing_import_2(self):
2472 s = """
2473 import bar, bar.foo as next
2474
2475 class A:
2476 def next(self, a, b):
2477 pass
2478 """
2479 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2480
2481 def test_shadowing_import_3(self):
2482 s = """
2483 import bar, bar.foo as next, baz
2484
2485 class A:
2486 def next(self, a, b):
2487 pass
2488 """
2489 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2490
2491 def test_shadowing_import_from_1(self):
2492 s = """
2493 from x import next
2494
2495 class A:
2496 def next(self, a, b):
2497 pass
2498 """
2499 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2500
2501 def test_shadowing_import_from_2(self):
2502 s = """
2503 from x.a import next
2504
2505 class A:
2506 def next(self, a, b):
2507 pass
2508 """
2509 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2510
2511 def test_shadowing_import_from_3(self):
2512 s = """
2513 from x import a, next, b
2514
2515 class A:
2516 def next(self, a, b):
2517 pass
2518 """
2519 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2520
2521 def test_shadowing_import_from_4(self):
2522 s = """
2523 from x.a import a, next, b
2524
2525 class A:
2526 def next(self, a, b):
2527 pass
2528 """
2529 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2530
2531 def test_shadowing_funcdef_1(self):
2532 s = """
2533 def next(a):
2534 pass
2535
2536 class A:
2537 def next(self, a, b):
2538 pass
2539 """
2540 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2541
2542 def test_shadowing_funcdef_2(self):
2543 b = """
2544 def next(a):
2545 pass
2546
2547 class A:
2548 def next(self):
2549 pass
2550
2551 it.next()
2552 """
2553 a = """
2554 def next(a):
2555 pass
2556
2557 class A:
2558 def __next__(self):
2559 pass
2560
2561 it.__next__()
2562 """
2563 self.warns(b, a, "Calls to builtin next() possibly shadowed")
2564
2565 def test_shadowing_global_1(self):
2566 s = """
2567 def f():
2568 global next
2569 next = 5
2570 """
2571 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2572
2573 def test_shadowing_global_2(self):
2574 s = """
2575 def f():
2576 global a, next, b
2577 next = 5
2578 """
2579 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2580
2581 def test_shadowing_for_simple(self):
2582 s = """
2583 for next in it():
2584 pass
2585
2586 b = 5
2587 c = 6
2588 """
2589 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2590
2591 def test_shadowing_for_tuple_1(self):
2592 s = """
2593 for next, b in it():
2594 pass
2595
2596 b = 5
2597 c = 6
2598 """
2599 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2600
2601 def test_shadowing_for_tuple_2(self):
2602 s = """
2603 for a, (next, c), b in it():
2604 pass
2605
2606 b = 5
2607 c = 6
2608 """
2609 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
2610
2611 def test_noncall_access_1(self):
2612 b = """gnext = g.next"""
2613 a = """gnext = g.__next__"""
2614 self.check(b, a)
2615
2616 def test_noncall_access_2(self):
2617 b = """f(g.next + 5)"""
2618 a = """f(g.__next__ + 5)"""
2619 self.check(b, a)
2620
2621 def test_noncall_access_3(self):
2622 b = """f(g().next + 5)"""
2623 a = """f(g().__next__ + 5)"""
2624 self.check(b, a)
2625
2626 class Test_nonzero(FixerTestCase):
2627 fixer = "nonzero"
2628
2629 def test_1(self):
2630 b = """
2631 class A:
2632 def __nonzero__(self):
2633 pass
2634 """
2635 a = """
2636 class A:
2637 def __bool__(self):
2638 pass
2639 """
2640 self.check(b, a)
2641
2642 def test_2(self):
2643 b = """
2644 class A(object):
2645 def __nonzero__(self):
2646 pass
2647 """
2648 a = """
2649 class A(object):
2650 def __bool__(self):
2651 pass
2652 """
2653 self.check(b, a)
2654
2655 def test_unchanged_1(self):
2656 s = """
2657 class A(object):
2658 def __bool__(self):
2659 pass
2660 """
2661 self.unchanged(s)
2662
2663 def test_unchanged_2(self):
2664 s = """
2665 class A(object):
2666 def __nonzero__(self, a):
2667 pass
2668 """
2669 self.unchanged(s)
2670
2671 def test_unchanged_func(self):
2672 s = """
2673 def __nonzero__(self):
2674 pass
2675 """
2676 self.unchanged(s)
2677
2678 class Test_numliterals(FixerTestCase):
2679 fixer = "numliterals"
2680
2681 def test_octal_1(self):
2682 b = """0755"""
2683 a = """0o755"""
2684 self.check(b, a)
2685
2686 def test_long_int_1(self):
2687 b = """a = 12L"""
2688 a = """a = 12"""
2689 self.check(b, a)
2690
2691 def test_long_int_2(self):
2692 b = """a = 12l"""
2693 a = """a = 12"""
2694 self.check(b, a)
2695
2696 def test_long_hex(self):
2697 b = """b = 0x12l"""
2698 a = """b = 0x12"""
2699 self.check(b, a)
2700
2701 def test_comments_and_spacing(self):
2702 b = """b = 0x12L"""
2703 a = """b = 0x12"""
2704 self.check(b, a)
2705
2706 b = """b = 0755 # spam"""
2707 a = """b = 0o755 # spam"""
2708 self.check(b, a)
2709
2710 def test_unchanged_int(self):
2711 s = """5"""
2712 self.unchanged(s)
2713
2714 def test_unchanged_float(self):
2715 s = """5.0"""
2716 self.unchanged(s)
2717
2718 def test_unchanged_octal(self):
2719 s = """0o755"""
2720 self.unchanged(s)
2721
2722 def test_unchanged_hex(self):
2723 s = """0xABC"""
2724 self.unchanged(s)
2725
2726 def test_unchanged_exp(self):
2727 s = """5.0e10"""
2728 self.unchanged(s)
2729
2730 def test_unchanged_complex_int(self):
2731 s = """5 + 4j"""
2732 self.unchanged(s)
2733
2734 def test_unchanged_complex_float(self):
2735 s = """5.4 + 4.9j"""
2736 self.unchanged(s)
2737
2738 def test_unchanged_complex_bare(self):
2739 s = """4j"""
2740 self.unchanged(s)
2741 s = """4.4j"""
2742 self.unchanged(s)
2743
2744 class Test_renames(FixerTestCase):
2745 fixer = "renames"
2746
2747 modules = {"sys": ("maxint", "maxsize"),
2748 }
2749
2750 def test_import_from(self):
2751 for mod, (old, new) in self.modules.items():
2752 b = "from %s import %s" % (mod, old)
2753 a = "from %s import %s" % (mod, new)
2754 self.check(b, a)
2755
2756 s = "from foo import %s" % old
2757 self.unchanged(s)
2758
2759 def test_import_from_as(self):
2760 for mod, (old, new) in self.modules.items():
2761 b = "from %s import %s as foo_bar" % (mod, old)
2762 a = "from %s import %s as foo_bar" % (mod, new)
2763 self.check(b, a)
2764
2765 def test_import_module_usage(self):
2766 for mod, (old, new) in self.modules.items():
2767 b = """
2768 import %s
2769 foo(%s, %s.%s)
2770 """ % (mod, mod, mod, old)
2771 a = """
2772 import %s
2773 foo(%s, %s.%s)
2774 """ % (mod, mod, mod, new)
2775 self.check(b, a)
2776
2777 def XXX_test_from_import_usage(self):
2778 # not implemented yet
2779 for mod, (old, new) in self.modules.items():
2780 b = """
2781 from %s import %s
2782 foo(%s, %s)
2783 """ % (mod, old, mod, old)
2784 a = """
2785 from %s import %s
2786 foo(%s, %s)
2787 """ % (mod, new, mod, new)
2788 self.check(b, a)
2789
2790 class Test_unicode(FixerTestCase):
2791 fixer = "unicode"
2792
2793 def test_whitespace(self):
2794 b = """unicode( x)"""
2795 a = """str( x)"""
2796 self.check(b, a)
2797
2798 b = """ unicode(x )"""
2799 a = """ str(x )"""
2800 self.check(b, a)
2801
2802 b = """ u'h'"""
2803 a = """ 'h'"""
2804 self.check(b, a)
2805
2806 def test_unicode_call(self):
2807 b = """unicode(x, y, z)"""
2808 a = """str(x, y, z)"""
2809 self.check(b, a)
2810
2811 def test_unichr(self):
2812 b = """unichr(u'h')"""
2813 a = """chr('h')"""
2814 self.check(b, a)
2815
2816 def test_unicode_literal_1(self):
2817 b = '''u"x"'''
2818 a = '''"x"'''
2819 self.check(b, a)
2820
2821 def test_unicode_literal_2(self):
2822 b = """ur'x'"""
2823 a = """r'x'"""
2824 self.check(b, a)
2825
2826 def test_unicode_literal_3(self):
2827 b = """UR'''x''' """
2828 a = """R'''x''' """
2829 self.check(b, a)
2830
2831 def test_native_literal_escape_u(self):
2832 b = """'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2833 a = """'\\\\\\\\u20ac\\\\U0001d121\\\\u20ac'"""
2834 self.check(b, a)
2835
2836 b = """r'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2837 a = """r'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2838 self.check(b, a)
2839
2840 def test_bytes_literal_escape_u(self):
2841 b = """b'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2842 a = """b'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2843 self.check(b, a)
2844
2845 b = """br'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2846 a = """br'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2847 self.check(b, a)
2848
2849 def test_unicode_literal_escape_u(self):
2850 b = """u'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2851 a = """'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2852 self.check(b, a)
2853
2854 b = """ur'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2855 a = """r'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2856 self.check(b, a)
2857
2858 def test_native_unicode_literal_escape_u(self):
2859 f = 'from __future__ import unicode_literals\n'
2860 b = f + """'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2861 a = f + """'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2862 self.check(b, a)
2863
2864 b = f + """r'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2865 a = f + """r'\\\\\\u20ac\\U0001d121\\\\u20ac'"""
2866 self.check(b, a)
2867
2868
2869 class Test_filter(FixerTestCase):
2870 fixer = "filter"
2871
2872 def test_prefix_preservation(self):
2873 b = """x = filter( foo, 'abc' )"""
2874 a = """x = list(filter( foo, 'abc' ))"""
2875 self.check(b, a)
2876
2877 b = """x = filter( None , 'abc' )"""
2878 a = """x = [_f for _f in 'abc' if _f]"""
2879 self.check(b, a)
2880
2881 def test_filter_basic(self):
2882 b = """x = filter(None, 'abc')"""
2883 a = """x = [_f for _f in 'abc' if _f]"""
2884 self.check(b, a)
2885
2886 b = """x = len(filter(f, 'abc'))"""
2887 a = """x = len(list(filter(f, 'abc')))"""
2888 self.check(b, a)
2889
2890 b = """x = filter(lambda x: x%2 == 0, range(10))"""
2891 a = """x = [x for x in range(10) if x%2 == 0]"""
2892 self.check(b, a)
2893
2894 # Note the parens around x
2895 b = """x = filter(lambda (x): x%2 == 0, range(10))"""
2896 a = """x = [x for x in range(10) if x%2 == 0]"""
2897 self.check(b, a)
2898
2899 # XXX This (rare) case is not supported
2900 ## b = """x = filter(f, 'abc')[0]"""
2901 ## a = """x = list(filter(f, 'abc'))[0]"""
2902 ## self.check(b, a)
2903
2904 def test_filter_nochange(self):
2905 a = """b.join(filter(f, 'abc'))"""
2906 self.unchanged(a)
2907 a = """(a + foo(5)).join(filter(f, 'abc'))"""
2908 self.unchanged(a)
2909 a = """iter(filter(f, 'abc'))"""
2910 self.unchanged(a)
2911 a = """list(filter(f, 'abc'))"""
2912 self.unchanged(a)
2913 a = """list(filter(f, 'abc'))[0]"""
2914 self.unchanged(a)
2915 a = """set(filter(f, 'abc'))"""
2916 self.unchanged(a)
2917 a = """set(filter(f, 'abc')).pop()"""
2918 self.unchanged(a)
2919 a = """tuple(filter(f, 'abc'))"""
2920 self.unchanged(a)
2921 a = """any(filter(f, 'abc'))"""
2922 self.unchanged(a)
2923 a = """all(filter(f, 'abc'))"""
2924 self.unchanged(a)
2925 a = """sum(filter(f, 'abc'))"""
2926 self.unchanged(a)
2927 a = """sorted(filter(f, 'abc'))"""
2928 self.unchanged(a)
2929 a = """sorted(filter(f, 'abc'), key=blah)"""
2930 self.unchanged(a)
2931 a = """sorted(filter(f, 'abc'), key=blah)[0]"""
2932 self.unchanged(a)
2933 a = """enumerate(filter(f, 'abc'))"""
2934 self.unchanged(a)
2935 a = """enumerate(filter(f, 'abc'), start=1)"""
2936 self.unchanged(a)
2937 a = """for i in filter(f, 'abc'): pass"""
2938 self.unchanged(a)
2939 a = """[x for x in filter(f, 'abc')]"""
2940 self.unchanged(a)
2941 a = """(x for x in filter(f, 'abc'))"""
2942 self.unchanged(a)
2943
2944 def test_future_builtins(self):
2945 a = "from future_builtins import spam, filter; filter(f, 'ham')"
2946 self.unchanged(a)
2947
2948 b = """from future_builtins import spam; x = filter(f, 'abc')"""
2949 a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
2950 self.check(b, a)
2951
2952 a = "from future_builtins import *; filter(f, 'ham')"
2953 self.unchanged(a)
2954
2955 class Test_map(FixerTestCase):
2956 fixer = "map"
2957
2958 def check(self, b, a):
2959 self.unchanged("from future_builtins import map; " + b, a)
2960 super(Test_map, self).check(b, a)
2961
2962 def test_prefix_preservation(self):
2963 b = """x = map( f, 'abc' )"""
2964 a = """x = list(map( f, 'abc' ))"""
2965 self.check(b, a)
2966
2967 def test_trailing_comment(self):
2968 b = """x = map(f, 'abc') # foo"""
2969 a = """x = list(map(f, 'abc')) # foo"""
2970 self.check(b, a)
2971
2972 def test_None_with_multiple_arguments(self):
2973 s = """x = map(None, a, b, c)"""
2974 self.warns_unchanged(s, "cannot convert map(None, ...) with "
2975 "multiple arguments")
2976
2977 def test_map_basic(self):
2978 b = """x = map(f, 'abc')"""
2979 a = """x = list(map(f, 'abc'))"""
2980 self.check(b, a)
2981
2982 b = """x = len(map(f, 'abc', 'def'))"""
2983 a = """x = len(list(map(f, 'abc', 'def')))"""
2984 self.check(b, a)
2985
2986 b = """x = map(None, 'abc')"""
2987 a = """x = list('abc')"""
2988 self.check(b, a)
2989
2990 b = """x = map(lambda x: x+1, range(4))"""
2991 a = """x = [x+1 for x in range(4)]"""
2992 self.check(b, a)
2993
2994 # Note the parens around x
2995 b = """x = map(lambda (x): x+1, range(4))"""
2996 a = """x = [x+1 for x in range(4)]"""
2997 self.check(b, a)
2998
2999 b = """
3000 foo()
3001 # foo
3002 map(f, x)
3003 """
3004 a = """
3005 foo()
3006 # foo
3007 list(map(f, x))
3008 """
3009 self.warns(b, a, "You should use a for loop here")
3010
3011 # XXX This (rare) case is not supported
3012 ## b = """x = map(f, 'abc')[0]"""
3013 ## a = """x = list(map(f, 'abc'))[0]"""
3014 ## self.check(b, a)
3015
3016 def test_map_nochange(self):
3017 a = """b.join(map(f, 'abc'))"""
3018 self.unchanged(a)
3019 a = """(a + foo(5)).join(map(f, 'abc'))"""
3020 self.unchanged(a)
3021 a = """iter(map(f, 'abc'))"""
3022 self.unchanged(a)
3023 a = """list(map(f, 'abc'))"""
3024 self.unchanged(a)
3025 a = """list(map(f, 'abc'))[0]"""
3026 self.unchanged(a)
3027 a = """set(map(f, 'abc'))"""
3028 self.unchanged(a)
3029 a = """set(map(f, 'abc')).pop()"""
3030 self.unchanged(a)
3031 a = """tuple(map(f, 'abc'))"""
3032 self.unchanged(a)
3033 a = """any(map(f, 'abc'))"""
3034 self.unchanged(a)
3035 a = """all(map(f, 'abc'))"""
3036 self.unchanged(a)
3037 a = """sum(map(f, 'abc'))"""
3038 self.unchanged(a)
3039 a = """sorted(map(f, 'abc'))"""
3040 self.unchanged(a)
3041 a = """sorted(map(f, 'abc'), key=blah)"""
3042 self.unchanged(a)
3043 a = """sorted(map(f, 'abc'), key=blah)[0]"""
3044 self.unchanged(a)
3045 a = """enumerate(map(f, 'abc'))"""
3046 self.unchanged(a)
3047 a = """enumerate(map(f, 'abc'), start=1)"""
3048 self.unchanged(a)
3049 a = """for i in map(f, 'abc'): pass"""
3050 self.unchanged(a)
3051 a = """[x for x in map(f, 'abc')]"""
3052 self.unchanged(a)
3053 a = """(x for x in map(f, 'abc'))"""
3054 self.unchanged(a)
3055
3056 def test_future_builtins(self):
3057 a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
3058 self.unchanged(a)
3059
3060 b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
3061 a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
3062 self.check(b, a)
3063
3064 a = "from future_builtins import *; map(f, 'ham')"
3065 self.unchanged(a)
3066
3067 class Test_zip(FixerTestCase):
3068 fixer = "zip"
3069
3070 def check(self, b, a):
3071 self.unchanged("from future_builtins import zip; " + b, a)
3072 super(Test_zip, self).check(b, a)
3073
3074 def test_zip_basic(self):
3075 b = """x = zip(a, b, c)"""
3076 a = """x = list(zip(a, b, c))"""
3077 self.check(b, a)
3078
3079 b = """x = len(zip(a, b))"""
3080 a = """x = len(list(zip(a, b)))"""
3081 self.check(b, a)
3082
3083 def test_zip_nochange(self):
3084 a = """b.join(zip(a, b))"""
3085 self.unchanged(a)
3086 a = """(a + foo(5)).join(zip(a, b))"""
3087 self.unchanged(a)
3088 a = """iter(zip(a, b))"""
3089 self.unchanged(a)
3090 a = """list(zip(a, b))"""
3091 self.unchanged(a)
3092 a = """list(zip(a, b))[0]"""
3093 self.unchanged(a)
3094 a = """set(zip(a, b))"""
3095 self.unchanged(a)
3096 a = """set(zip(a, b)).pop()"""
3097 self.unchanged(a)
3098 a = """tuple(zip(a, b))"""
3099 self.unchanged(a)
3100 a = """any(zip(a, b))"""
3101 self.unchanged(a)
3102 a = """all(zip(a, b))"""
3103 self.unchanged(a)
3104 a = """sum(zip(a, b))"""
3105 self.unchanged(a)
3106 a = """sorted(zip(a, b))"""
3107 self.unchanged(a)
3108 a = """sorted(zip(a, b), key=blah)"""
3109 self.unchanged(a)
3110 a = """sorted(zip(a, b), key=blah)[0]"""
3111 self.unchanged(a)
3112 a = """enumerate(zip(a, b))"""
3113 self.unchanged(a)
3114 a = """enumerate(zip(a, b), start=1)"""
3115 self.unchanged(a)
3116 a = """for i in zip(a, b): pass"""
3117 self.unchanged(a)
3118 a = """[x for x in zip(a, b)]"""
3119 self.unchanged(a)
3120 a = """(x for x in zip(a, b))"""
3121 self.unchanged(a)
3122
3123 def test_future_builtins(self):
3124 a = "from future_builtins import spam, zip, eggs; zip(a, b)"
3125 self.unchanged(a)
3126
3127 b = """from future_builtins import spam, eggs; x = zip(a, b)"""
3128 a = """from future_builtins import spam, eggs; x = list(zip(a, b))"""
3129 self.check(b, a)
3130
3131 a = "from future_builtins import *; zip(a, b)"
3132 self.unchanged(a)
3133
3134 class Test_standarderror(FixerTestCase):
3135 fixer = "standarderror"
3136
3137 def test(self):
3138 b = """x = StandardError()"""
3139 a = """x = Exception()"""
3140 self.check(b, a)
3141
3142 b = """x = StandardError(a, b, c)"""
3143 a = """x = Exception(a, b, c)"""
3144 self.check(b, a)
3145
3146 b = """f(2 + StandardError(a, b, c))"""
3147 a = """f(2 + Exception(a, b, c))"""
3148 self.check(b, a)
3149
3150 class Test_types(FixerTestCase):
3151 fixer = "types"
3152
3153 def test_basic_types_convert(self):
3154 b = """types.StringType"""
3155 a = """bytes"""
3156 self.check(b, a)
3157
3158 b = """types.DictType"""
3159 a = """dict"""
3160 self.check(b, a)
3161
3162 b = """types . IntType"""
3163 a = """int"""
3164 self.check(b, a)
3165
3166 b = """types.ListType"""
3167 a = """list"""
3168 self.check(b, a)
3169
3170 b = """types.LongType"""
3171 a = """int"""
3172 self.check(b, a)
3173
3174 b = """types.NoneType"""
3175 a = """type(None)"""
3176 self.check(b, a)
3177
3178 b = "types.StringTypes"
3179 a = "(str,)"
3180 self.check(b, a)
3181
3182 class Test_idioms(FixerTestCase):
3183 fixer = "idioms"
3184
3185 def test_while(self):
3186 b = """while 1: foo()"""
3187 a = """while True: foo()"""
3188 self.check(b, a)
3189
3190 b = """while 1: foo()"""
3191 a = """while True: foo()"""
3192 self.check(b, a)
3193
3194 b = """
3195 while 1:
3196 foo()
3197 """
3198 a = """
3199 while True:
3200 foo()
3201 """
3202 self.check(b, a)
3203
3204 def test_while_unchanged(self):
3205 s = """while 11: foo()"""
3206 self.unchanged(s)
3207
3208 s = """while 0: foo()"""
3209 self.unchanged(s)
3210
3211 s = """while foo(): foo()"""
3212 self.unchanged(s)
3213
3214 s = """while []: foo()"""
3215 self.unchanged(s)
3216
3217 def test_eq_simple(self):
3218 b = """type(x) == T"""
3219 a = """isinstance(x, T)"""
3220 self.check(b, a)
3221
3222 b = """if type(x) == T: pass"""
3223 a = """if isinstance(x, T): pass"""
3224 self.check(b, a)
3225
3226 def test_eq_reverse(self):
3227 b = """T == type(x)"""
3228 a = """isinstance(x, T)"""
3229 self.check(b, a)
3230
3231 b = """if T == type(x): pass"""
3232 a = """if isinstance(x, T): pass"""
3233 self.check(b, a)
3234
3235 def test_eq_expression(self):
3236 b = """type(x+y) == d.get('T')"""
3237 a = """isinstance(x+y, d.get('T'))"""
3238 self.check(b, a)
3239
3240 b = """type( x + y) == d.get('T')"""
3241 a = """isinstance(x + y, d.get('T'))"""
3242 self.check(b, a)
3243
3244 def test_is_simple(self):
3245 b = """type(x) is T"""
3246 a = """isinstance(x, T)"""
3247 self.check(b, a)
3248
3249 b = """if type(x) is T: pass"""
3250 a = """if isinstance(x, T): pass"""
3251 self.check(b, a)
3252
3253 def test_is_reverse(self):
3254 b = """T is type(x)"""
3255 a = """isinstance(x, T)"""
3256 self.check(b, a)
3257
3258 b = """if T is type(x): pass"""
3259 a = """if isinstance(x, T): pass"""
3260 self.check(b, a)
3261
3262 def test_is_expression(self):
3263 b = """type(x+y) is d.get('T')"""
3264 a = """isinstance(x+y, d.get('T'))"""
3265 self.check(b, a)
3266
3267 b = """type( x + y) is d.get('T')"""
3268 a = """isinstance(x + y, d.get('T'))"""
3269 self.check(b, a)
3270
3271 def test_is_not_simple(self):
3272 b = """type(x) is not T"""
3273 a = """not isinstance(x, T)"""
3274 self.check(b, a)
3275
3276 b = """if type(x) is not T: pass"""
3277 a = """if not isinstance(x, T): pass"""
3278 self.check(b, a)
3279
3280 def test_is_not_reverse(self):
3281 b = """T is not type(x)"""
3282 a = """not isinstance(x, T)"""
3283 self.check(b, a)
3284
3285 b = """if T is not type(x): pass"""
3286 a = """if not isinstance(x, T): pass"""
3287 self.check(b, a)
3288
3289 def test_is_not_expression(self):
3290 b = """type(x+y) is not d.get('T')"""
3291 a = """not isinstance(x+y, d.get('T'))"""
3292 self.check(b, a)
3293
3294 b = """type( x + y) is not d.get('T')"""
3295 a = """not isinstance(x + y, d.get('T'))"""
3296 self.check(b, a)
3297
3298 def test_ne_simple(self):
3299 b = """type(x) != T"""
3300 a = """not isinstance(x, T)"""
3301 self.check(b, a)
3302
3303 b = """if type(x) != T: pass"""
3304 a = """if not isinstance(x, T): pass"""
3305 self.check(b, a)
3306
3307 def test_ne_reverse(self):
3308 b = """T != type(x)"""
3309 a = """not isinstance(x, T)"""
3310 self.check(b, a)
3311
3312 b = """if T != type(x): pass"""
3313 a = """if not isinstance(x, T): pass"""
3314 self.check(b, a)
3315
3316 def test_ne_expression(self):
3317 b = """type(x+y) != d.get('T')"""
3318 a = """not isinstance(x+y, d.get('T'))"""
3319 self.check(b, a)
3320
3321 b = """type( x + y) != d.get('T')"""
3322 a = """not isinstance(x + y, d.get('T'))"""
3323 self.check(b, a)
3324
3325 def test_type_unchanged(self):
3326 a = """type(x).__name__"""
3327 self.unchanged(a)
3328
3329 def test_sort_list_call(self):
3330 b = """
3331 v = list(t)
3332 v.sort()
3333 foo(v)
3334 """
3335 a = """
3336 v = sorted(t)
3337 foo(v)
3338 """
3339 self.check(b, a)
3340
3341 b = """
3342 v = list(foo(b) + d)
3343 v.sort()
3344 foo(v)
3345 """
3346 a = """
3347 v = sorted(foo(b) + d)
3348 foo(v)
3349 """
3350 self.check(b, a)
3351
3352 b = """
3353 while x:
3354 v = list(t)
3355 v.sort()
3356 foo(v)
3357 """
3358 a = """
3359 while x:
3360 v = sorted(t)
3361 foo(v)
3362 """
3363 self.check(b, a)
3364
3365 b = """
3366 v = list(t)
3367 # foo
3368 v.sort()
3369 foo(v)
3370 """
3371 a = """
3372 v = sorted(t)
3373 # foo
3374 foo(v)
3375 """
3376 self.check(b, a)
3377
3378 b = r"""
3379 v = list( t)
3380 v.sort()
3381 foo(v)
3382 """
3383 a = r"""
3384 v = sorted( t)
3385 foo(v)
3386 """
3387 self.check(b, a)
3388
3389 b = r"""
3390 try:
3391 m = list(s)
3392 m.sort()
3393 except: pass
3394 """
3395
3396 a = r"""
3397 try:
3398 m = sorted(s)
3399 except: pass
3400 """
3401 self.check(b, a)
3402
3403 b = r"""
3404 try:
3405 m = list(s)
3406 # foo
3407 m.sort()
3408 except: pass
3409 """
3410
3411 a = r"""
3412 try:
3413 m = sorted(s)
3414 # foo
3415 except: pass
3416 """
3417 self.check(b, a)
3418
3419 b = r"""
3420 m = list(s)
3421 # more comments
3422 m.sort()"""
3423
3424 a = r"""
3425 m = sorted(s)
3426 # more comments"""
3427 self.check(b, a)
3428
3429 def test_sort_simple_expr(self):
3430 b = """
3431 v = t
3432 v.sort()
3433 foo(v)
3434 """
3435 a = """
3436 v = sorted(t)
3437 foo(v)
3438 """
3439 self.check(b, a)
3440
3441 b = """
3442 v = foo(b)
3443 v.sort()
3444 foo(v)
3445 """
3446 a = """
3447 v = sorted(foo(b))
3448 foo(v)
3449 """
3450 self.check(b, a)
3451
3452 b = """
3453 v = b.keys()
3454 v.sort()
3455 foo(v)
3456 """
3457 a = """
3458 v = sorted(b.keys())
3459 foo(v)
3460 """
3461 self.check(b, a)
3462
3463 b = """
3464 v = foo(b) + d
3465 v.sort()
3466 foo(v)
3467 """
3468 a = """
3469 v = sorted(foo(b) + d)
3470 foo(v)
3471 """
3472 self.check(b, a)
3473
3474 b = """
3475 while x:
3476 v = t
3477 v.sort()
3478 foo(v)
3479 """
3480 a = """
3481 while x:
3482 v = sorted(t)
3483 foo(v)
3484 """
3485 self.check(b, a)
3486
3487 b = """
3488 v = t
3489 # foo
3490 v.sort()
3491 foo(v)
3492 """
3493 a = """
3494 v = sorted(t)
3495 # foo
3496 foo(v)
3497 """
3498 self.check(b, a)
3499
3500 b = r"""
3501 v = t
3502 v.sort()
3503 foo(v)
3504 """
3505 a = r"""
3506 v = sorted(t)
3507 foo(v)
3508 """
3509 self.check(b, a)
3510
3511 def test_sort_unchanged(self):
3512 s = """
3513 v = list(t)
3514 w.sort()
3515 foo(w)
3516 """
3517 self.unchanged(s)
3518
3519 s = """
3520 v = list(t)
3521 v.sort(u)
3522 foo(v)
3523 """
3524 self.unchanged(s)
3525
3526 class Test_basestring(FixerTestCase):
3527 fixer = "basestring"
3528
3529 def test_basestring(self):
3530 b = """isinstance(x, basestring)"""
3531 a = """isinstance(x, str)"""
3532 self.check(b, a)
3533
3534 class Test_buffer(FixerTestCase):
3535 fixer = "buffer"
3536
3537 def test_buffer(self):
3538 b = """x = buffer(y)"""
3539 a = """x = memoryview(y)"""
3540 self.check(b, a)
3541
3542 def test_slicing(self):
3543 b = """buffer(y)[4:5]"""
3544 a = """memoryview(y)[4:5]"""
3545 self.check(b, a)
3546
3547 class Test_future(FixerTestCase):
3548 fixer = "future"
3549
3550 def test_future(self):
3551 b = """from __future__ import braces"""
3552 a = """"""
3553 self.check(b, a)
3554
3555 b = """# comment\nfrom __future__ import braces"""
3556 a = """# comment\n"""
3557 self.check(b, a)
3558
3559 b = """from __future__ import braces\n# comment"""
3560 a = """\n# comment"""
3561 self.check(b, a)
3562
3563 def test_run_order(self):
3564 self.assert_runs_after('print')
3565
3566 class Test_itertools(FixerTestCase):
3567 fixer = "itertools"
3568
3569 def checkall(self, before, after):
3570 # Because we need to check with and without the itertools prefix
3571 # and on each of the three functions, these loops make it all
3572 # much easier
3573 for i in ('itertools.', ''):
3574 for f in ('map', 'filter', 'zip'):
3575 b = before %(i+'i'+f)
3576 a = after %(f)
3577 self.check(b, a)
3578
3579 def test_0(self):
3580 # A simple example -- test_1 covers exactly the same thing,
3581 # but it's not quite as clear.
3582 b = "itertools.izip(a, b)"
3583 a = "zip(a, b)"
3584 self.check(b, a)
3585
3586 def test_1(self):
3587 b = """%s(f, a)"""
3588 a = """%s(f, a)"""
3589 self.checkall(b, a)
3590
3591 def test_qualified(self):
3592 b = """itertools.ifilterfalse(a, b)"""
3593 a = """itertools.filterfalse(a, b)"""
3594 self.check(b, a)
3595
3596 b = """itertools.izip_longest(a, b)"""
3597 a = """itertools.zip_longest(a, b)"""
3598 self.check(b, a)
3599
3600 def test_2(self):
3601 b = """ifilterfalse(a, b)"""
3602 a = """filterfalse(a, b)"""
3603 self.check(b, a)
3604
3605 b = """izip_longest(a, b)"""
3606 a = """zip_longest(a, b)"""
3607 self.check(b, a)
3608
3609 def test_space_1(self):
3610 b = """ %s(f, a)"""
3611 a = """ %s(f, a)"""
3612 self.checkall(b, a)
3613
3614 def test_space_2(self):
3615 b = """ itertools.ifilterfalse(a, b)"""
3616 a = """ itertools.filterfalse(a, b)"""
3617 self.check(b, a)
3618
3619 b = """ itertools.izip_longest(a, b)"""
3620 a = """ itertools.zip_longest(a, b)"""
3621 self.check(b, a)
3622
3623 def test_run_order(self):
3624 self.assert_runs_after('map', 'zip', 'filter')
3625
3626
3627 class Test_itertools_imports(FixerTestCase):
3628 fixer = 'itertools_imports'
3629
3630 def test_reduced(self):
3631 b = "from itertools import imap, izip, foo"
3632 a = "from itertools import foo"
3633 self.check(b, a)
3634
3635 b = "from itertools import bar, imap, izip, foo"
3636 a = "from itertools import bar, foo"
3637 self.check(b, a)
3638
3639 b = "from itertools import chain, imap, izip"
3640 a = "from itertools import chain"
3641 self.check(b, a)
3642
3643 def test_comments(self):
3644 b = "#foo\nfrom itertools import imap, izip"
3645 a = "#foo\n"
3646 self.check(b, a)
3647
3648 def test_none(self):
3649 b = "from itertools import imap, izip"
3650 a = ""
3651 self.check(b, a)
3652
3653 b = "from itertools import izip"
3654 a = ""
3655 self.check(b, a)
3656
3657 def test_import_as(self):
3658 b = "from itertools import izip, bar as bang, imap"
3659 a = "from itertools import bar as bang"
3660 self.check(b, a)
3661
3662 b = "from itertools import izip as _zip, imap, bar"
3663 a = "from itertools import bar"
3664 self.check(b, a)
3665
3666 b = "from itertools import imap as _map"
3667 a = ""
3668 self.check(b, a)
3669
3670 b = "from itertools import imap as _map, izip as _zip"
3671 a = ""
3672 self.check(b, a)
3673
3674 s = "from itertools import bar as bang"
3675 self.unchanged(s)
3676
3677 def test_ifilter_and_zip_longest(self):
3678 for name in "filterfalse", "zip_longest":
3679 b = "from itertools import i%s" % (name,)
3680 a = "from itertools import %s" % (name,)
3681 self.check(b, a)
3682
3683 b = "from itertools import imap, i%s, foo" % (name,)
3684 a = "from itertools import %s, foo" % (name,)
3685 self.check(b, a)
3686
3687 b = "from itertools import bar, i%s, foo" % (name,)
3688 a = "from itertools import bar, %s, foo" % (name,)
3689 self.check(b, a)
3690
3691 def test_import_star(self):
3692 s = "from itertools import *"
3693 self.unchanged(s)
3694
3695
3696 def test_unchanged(self):
3697 s = "from itertools import foo"
3698 self.unchanged(s)
3699
3700
3701 class Test_import(FixerTestCase):
3702 fixer = "import"
3703
3704 def setUp(self):
3705 super(Test_import, self).setUp()
3706 # Need to replace fix_import's exists method
3707 # so we can check that it's doing the right thing
3708 self.files_checked = []
3709 self.present_files = set()
3710 self.always_exists = True
3711 def fake_exists(name):
3712 self.files_checked.append(name)
3713 return self.always_exists or (name in self.present_files)
3714
3715 from lib2to3.fixes import fix_import
3716 fix_import.exists = fake_exists
3717
3718 def tearDown(self):
3719 from lib2to3.fixes import fix_import
3720 fix_import.exists = os.path.exists
3721
3722 def check_both(self, b, a):
3723 self.always_exists = True
3724 super(Test_import, self).check(b, a)
3725 self.always_exists = False
3726 super(Test_import, self).unchanged(b)
3727
3728 def test_files_checked(self):
3729 def p(path):
3730 # Takes a unix path and returns a path with correct separators
3731 return os.path.pathsep.join(path.split("/"))
3732
3733 self.always_exists = False
3734 self.present_files = set(['__init__.py'])
3735 expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd')
3736 names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
3737
3738 for name in names_to_test:
3739 self.files_checked = []
3740 self.filename = name
3741 self.unchanged("import jam")
3742
3743 if os.path.dirname(name):
3744 name = os.path.dirname(name) + '/jam'
3745 else:
3746 name = 'jam'
3747 expected_checks = set(name + ext for ext in expected_extensions)
3748 expected_checks.add("__init__.py")
3749
3750 self.assertEqual(set(self.files_checked), expected_checks)
3751
3752 def test_not_in_package(self):
3753 s = "import bar"
3754 self.always_exists = False
3755 self.present_files = set(["bar.py"])
3756 self.unchanged(s)
3757
3758 def test_with_absolute_import_enabled(self):
3759 s = "from __future__ import absolute_import\nimport bar"
3760 self.always_exists = False
3761 self.present_files = set(["__init__.py", "bar.py"])
3762 self.unchanged(s)
3763
3764 def test_in_package(self):
3765 b = "import bar"
3766 a = "from . import bar"
3767 self.always_exists = False
3768 self.present_files = set(["__init__.py", "bar.py"])
3769 self.check(b, a)
3770
3771 def test_import_from_package(self):
3772 b = "import bar"
3773 a = "from . import bar"
3774 self.always_exists = False
3775 self.present_files = set(["__init__.py", "bar" + os.path.sep])
3776 self.check(b, a)
3777
3778 def test_already_relative_import(self):
3779 s = "from . import bar"
3780 self.unchanged(s)
3781
3782 def test_comments_and_indent(self):
3783 b = "import bar # Foo"
3784 a = "from . import bar # Foo"
3785 self.check(b, a)
3786
3787 def test_from(self):
3788 b = "from foo import bar, baz"
3789 a = "from .foo import bar, baz"
3790 self.check_both(b, a)
3791
3792 b = "from foo import bar"
3793 a = "from .foo import bar"
3794 self.check_both(b, a)
3795
3796 b = "from foo import (bar, baz)"
3797 a = "from .foo import (bar, baz)"
3798 self.check_both(b, a)
3799
3800 def test_dotted_from(self):
3801 b = "from green.eggs import ham"
3802 a = "from .green.eggs import ham"
3803 self.check_both(b, a)
3804
3805 def test_from_as(self):
3806 b = "from green.eggs import ham as spam"
3807 a = "from .green.eggs import ham as spam"
3808 self.check_both(b, a)
3809
3810 def test_import(self):
3811 b = "import foo"
3812 a = "from . import foo"
3813 self.check_both(b, a)
3814
3815 b = "import foo, bar"
3816 a = "from . import foo, bar"
3817 self.check_both(b, a)
3818
3819 b = "import foo, bar, x"
3820 a = "from . import foo, bar, x"
3821 self.check_both(b, a)
3822
3823 b = "import x, y, z"
3824 a = "from . import x, y, z"
3825 self.check_both(b, a)
3826
3827 def test_import_as(self):
3828 b = "import foo as x"
3829 a = "from . import foo as x"
3830 self.check_both(b, a)
3831
3832 b = "import a as b, b as c, c as d"
3833 a = "from . import a as b, b as c, c as d"
3834 self.check_both(b, a)
3835
3836 def test_local_and_absolute(self):
3837 self.always_exists = False
3838 self.present_files = set(["foo.py", "__init__.py"])
3839
3840 s = "import foo, bar"
3841 self.warns_unchanged(s, "absolute and local imports together")
3842
3843 def test_dotted_import(self):
3844 b = "import foo.bar"
3845 a = "from . import foo.bar"
3846 self.check_both(b, a)
3847
3848 def test_dotted_import_as(self):
3849 b = "import foo.bar as bang"
3850 a = "from . import foo.bar as bang"
3851 self.check_both(b, a)
3852
3853 def test_prefix(self):
3854 b = """
3855 # prefix
3856 import foo.bar
3857 """
3858 a = """
3859 # prefix
3860 from . import foo.bar
3861 """
3862 self.check_both(b, a)
3863
3864
3865 class Test_set_literal(FixerTestCase):
3866
3867 fixer = "set_literal"
3868
3869 def test_basic(self):
3870 b = """set([1, 2, 3])"""
3871 a = """{1, 2, 3}"""
3872 self.check(b, a)
3873
3874 b = """set((1, 2, 3))"""
3875 a = """{1, 2, 3}"""
3876 self.check(b, a)
3877
3878 b = """set((1,))"""
3879 a = """{1}"""
3880 self.check(b, a)
3881
3882 b = """set([1])"""
3883 self.check(b, a)
3884
3885 b = """set((a, b))"""
3886 a = """{a, b}"""
3887 self.check(b, a)
3888
3889 b = """set([a, b])"""
3890 self.check(b, a)
3891
3892 b = """set((a*234, f(args=23)))"""
3893 a = """{a*234, f(args=23)}"""
3894 self.check(b, a)
3895
3896 b = """set([a*23, f(23)])"""
3897 a = """{a*23, f(23)}"""
3898 self.check(b, a)
3899
3900 b = """set([a-234**23])"""
3901 a = """{a-234**23}"""
3902 self.check(b, a)
3903
3904 def test_listcomps(self):
3905 b = """set([x for x in y])"""
3906 a = """{x for x in y}"""
3907 self.check(b, a)
3908
3909 b = """set([x for x in y if x == m])"""
3910 a = """{x for x in y if x == m}"""
3911 self.check(b, a)
3912
3913 b = """set([x for x in y for a in b])"""
3914 a = """{x for x in y for a in b}"""
3915 self.check(b, a)
3916
3917 b = """set([f(x) - 23 for x in y])"""
3918 a = """{f(x) - 23 for x in y}"""
3919 self.check(b, a)
3920
3921 def test_whitespace(self):
3922 b = """set( [1, 2])"""
3923 a = """{1, 2}"""
3924 self.check(b, a)
3925
3926 b = """set([1 , 2])"""
3927 a = """{1 , 2}"""
3928 self.check(b, a)
3929
3930 b = """set([ 1 ])"""
3931 a = """{ 1 }"""
3932 self.check(b, a)
3933
3934 b = """set( [1] )"""
3935 a = """{1}"""
3936 self.check(b, a)
3937
3938 b = """set([ 1, 2 ])"""
3939 a = """{ 1, 2 }"""
3940 self.check(b, a)
3941
3942 b = """set([x for x in y ])"""
3943 a = """{x for x in y }"""
3944 self.check(b, a)
3945
3946 b = """set(
3947 [1, 2]
3948 )
3949 """
3950 a = """{1, 2}\n"""
3951 self.check(b, a)
3952
3953 def test_comments(self):
3954 b = """set((1, 2)) # Hi"""
3955 a = """{1, 2} # Hi"""
3956 self.check(b, a)
3957
3958 # This isn't optimal behavior, but the fixer is optional.
3959 b = """
3960 # Foo
3961 set( # Bar
3962 (1, 2)
3963 )
3964 """
3965 a = """
3966 # Foo
3967 {1, 2}
3968 """
3969 self.check(b, a)
3970
3971 def test_unchanged(self):
3972 s = """set()"""
3973 self.unchanged(s)
3974
3975 s = """set(a)"""
3976 self.unchanged(s)
3977
3978 s = """set(a, b, c)"""
3979 self.unchanged(s)
3980
3981 # Don't transform generators because they might have to be lazy.
3982 s = """set(x for x in y)"""
3983 self.unchanged(s)
3984
3985 s = """set(x for x in y if z)"""
3986 self.unchanged(s)
3987
3988 s = """set(a*823-23**2 + f(23))"""
3989 self.unchanged(s)
3990
3991
3992 class Test_sys_exc(FixerTestCase):
3993 fixer = "sys_exc"
3994
3995 def test_0(self):
3996 b = "sys.exc_type"
3997 a = "sys.exc_info()[0]"
3998 self.check(b, a)
3999
4000 def test_1(self):
4001 b = "sys.exc_value"
4002 a = "sys.exc_info()[1]"
4003 self.check(b, a)
4004
4005 def test_2(self):
4006 b = "sys.exc_traceback"
4007 a = "sys.exc_info()[2]"
4008 self.check(b, a)
4009
4010 def test_3(self):
4011 b = "sys.exc_type # Foo"
4012 a = "sys.exc_info()[0] # Foo"
4013 self.check(b, a)
4014
4015 def test_4(self):
4016 b = "sys. exc_type"
4017 a = "sys. exc_info()[0]"
4018 self.check(b, a)
4019
4020 def test_5(self):
4021 b = "sys .exc_type"
4022 a = "sys .exc_info()[0]"
4023 self.check(b, a)
4024
4025
4026 class Test_paren(FixerTestCase):
4027 fixer = "paren"
4028
4029 def test_0(self):
4030 b = """[i for i in 1, 2 ]"""
4031 a = """[i for i in (1, 2) ]"""
4032 self.check(b, a)
4033
4034 def test_1(self):
4035 b = """[i for i in 1, 2, ]"""
4036 a = """[i for i in (1, 2,) ]"""
4037 self.check(b, a)
4038
4039 def test_2(self):
4040 b = """[i for i in 1, 2 ]"""
4041 a = """[i for i in (1, 2) ]"""
4042 self.check(b, a)
4043
4044 def test_3(self):
4045 b = """[i for i in 1, 2 if i]"""
4046 a = """[i for i in (1, 2) if i]"""
4047 self.check(b, a)
4048
4049 def test_4(self):
4050 b = """[i for i in 1, 2 ]"""
4051 a = """[i for i in (1, 2) ]"""
4052 self.check(b, a)
4053
4054 def test_5(self):
4055 b = """(i for i in 1, 2)"""
4056 a = """(i for i in (1, 2))"""
4057 self.check(b, a)
4058
4059 def test_6(self):
4060 b = """(i for i in 1 ,2 if i)"""
4061 a = """(i for i in (1 ,2) if i)"""
4062 self.check(b, a)
4063
4064 def test_unchanged_0(self):
4065 s = """[i for i in (1, 2)]"""
4066 self.unchanged(s)
4067
4068 def test_unchanged_1(self):
4069 s = """[i for i in foo()]"""
4070 self.unchanged(s)
4071
4072 def test_unchanged_2(self):
4073 s = """[i for i in (1, 2) if nothing]"""
4074 self.unchanged(s)
4075
4076 def test_unchanged_3(self):
4077 s = """(i for i in (1, 2))"""
4078 self.unchanged(s)
4079
4080 def test_unchanged_4(self):
4081 s = """[i for i in m]"""
4082 self.unchanged(s)
4083
4084 class Test_metaclass(FixerTestCase):
4085
4086 fixer = 'metaclass'
4087
4088 def test_unchanged(self):
4089 self.unchanged("class X(): pass")
4090 self.unchanged("class X(object): pass")
4091 self.unchanged("class X(object1, object2): pass")
4092 self.unchanged("class X(object1, object2, object3): pass")
4093 self.unchanged("class X(metaclass=Meta): pass")
4094 self.unchanged("class X(b, arg=23, metclass=Meta): pass")
4095 self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
4096
4097 s = """
4098 class X:
4099 def __metaclass__(self): pass
4100 """
4101 self.unchanged(s)
4102
4103 s = """
4104 class X:
4105 a[23] = 74
4106 """
4107 self.unchanged(s)
4108
4109 def test_comments(self):
4110 b = """
4111 class X:
4112 # hi
4113 __metaclass__ = AppleMeta
4114 """
4115 a = """
4116 class X(metaclass=AppleMeta):
4117 # hi
4118 pass
4119 """
4120 self.check(b, a)
4121
4122 b = """
4123 class X:
4124 __metaclass__ = Meta
4125 # Bedtime!
4126 """
4127 a = """
4128 class X(metaclass=Meta):
4129 pass
4130 # Bedtime!
4131 """
4132 self.check(b, a)
4133
4134 def test_meta(self):
4135 # no-parent class, odd body
4136 b = """
4137 class X():
4138 __metaclass__ = Q
4139 pass
4140 """
4141 a = """
4142 class X(metaclass=Q):
4143 pass
4144 """
4145 self.check(b, a)
4146
4147 # one parent class, no body
4148 b = """class X(object): __metaclass__ = Q"""
4149 a = """class X(object, metaclass=Q): pass"""
4150 self.check(b, a)
4151
4152
4153 # one parent, simple body
4154 b = """
4155 class X(object):
4156 __metaclass__ = Meta
4157 bar = 7
4158 """
4159 a = """
4160 class X(object, metaclass=Meta):
4161 bar = 7
4162 """
4163 self.check(b, a)
4164
4165 b = """
4166 class X:
4167 __metaclass__ = Meta; x = 4; g = 23
4168 """
4169 a = """
4170 class X(metaclass=Meta):
4171 x = 4; g = 23
4172 """
4173 self.check(b, a)
4174
4175 # one parent, simple body, __metaclass__ last
4176 b = """
4177 class X(object):
4178 bar = 7
4179 __metaclass__ = Meta
4180 """
4181 a = """
4182 class X(object, metaclass=Meta):
4183 bar = 7
4184 """
4185 self.check(b, a)
4186
4187 # redefining __metaclass__
4188 b = """
4189 class X():
4190 __metaclass__ = A
4191 __metaclass__ = B
4192 bar = 7
4193 """
4194 a = """
4195 class X(metaclass=B):
4196 bar = 7
4197 """
4198 self.check(b, a)
4199
4200 # multiple inheritance, simple body
4201 b = """
4202 class X(clsA, clsB):
4203 __metaclass__ = Meta
4204 bar = 7
4205 """
4206 a = """
4207 class X(clsA, clsB, metaclass=Meta):
4208 bar = 7
4209 """
4210 self.check(b, a)
4211
4212 # keywords in the class statement
4213 b = """class m(a, arg=23): __metaclass__ = Meta"""
4214 a = """class m(a, arg=23, metaclass=Meta): pass"""
4215 self.check(b, a)
4216
4217 b = """
4218 class X(expression(2 + 4)):
4219 __metaclass__ = Meta
4220 """
4221 a = """
4222 class X(expression(2 + 4), metaclass=Meta):
4223 pass
4224 """
4225 self.check(b, a)
4226
4227 b = """
4228 class X(expression(2 + 4), x**4):
4229 __metaclass__ = Meta
4230 """
4231 a = """
4232 class X(expression(2 + 4), x**4, metaclass=Meta):
4233 pass
4234 """
4235 self.check(b, a)
4236
4237 b = """
4238 class X:
4239 __metaclass__ = Meta
4240 save.py = 23
4241 """
4242 a = """
4243 class X(metaclass=Meta):
4244 save.py = 23
4245 """
4246 self.check(b, a)
4247
4248
4249 class Test_getcwdu(FixerTestCase):
4250
4251 fixer = 'getcwdu'
4252
4253 def test_basic(self):
4254 b = """os.getcwdu"""
4255 a = """os.getcwd"""
4256 self.check(b, a)
4257
4258 b = """os.getcwdu()"""
4259 a = """os.getcwd()"""
4260 self.check(b, a)
4261
4262 b = """meth = os.getcwdu"""
4263 a = """meth = os.getcwd"""
4264 self.check(b, a)
4265
4266 b = """os.getcwdu(args)"""
4267 a = """os.getcwd(args)"""
4268 self.check(b, a)
4269
4270 def test_comment(self):
4271 b = """os.getcwdu() # Foo"""
4272 a = """os.getcwd() # Foo"""
4273 self.check(b, a)
4274
4275 def test_unchanged(self):
4276 s = """os.getcwd()"""
4277 self.unchanged(s)
4278
4279 s = """getcwdu()"""
4280 self.unchanged(s)
4281
4282 s = """os.getcwdb()"""
4283 self.unchanged(s)
4284
4285 def test_indentation(self):
4286 b = """
4287 if 1:
4288 os.getcwdu()
4289 """
4290 a = """
4291 if 1:
4292 os.getcwd()
4293 """
4294 self.check(b, a)
4295
4296 def test_multilation(self):
4297 b = """os .getcwdu()"""
4298 a = """os .getcwd()"""
4299 self.check(b, a)
4300
4301 b = """os. getcwdu"""
4302 a = """os. getcwd"""
4303 self.check(b, a)
4304
4305 b = """os.getcwdu ( )"""
4306 a = """os.getcwd ( )"""
4307 self.check(b, a)
4308
4309
4310 class Test_operator(FixerTestCase):
4311
4312 fixer = "operator"
4313
4314 def test_operator_isCallable(self):
4315 b = "operator.isCallable(x)"
4316 a = "hasattr(x, '__call__')"
4317 self.check(b, a)
4318
4319 def test_operator_sequenceIncludes(self):
4320 b = "operator.sequenceIncludes(x, y)"
4321 a = "operator.contains(x, y)"
4322 self.check(b, a)
4323
4324 b = "operator .sequenceIncludes(x, y)"
4325 a = "operator .contains(x, y)"
4326 self.check(b, a)
4327
4328 b = "operator. sequenceIncludes(x, y)"
4329 a = "operator. contains(x, y)"
4330 self.check(b, a)
4331
4332 def test_operator_isSequenceType(self):
4333 b = "operator.isSequenceType(x)"
4334 a = "import collections\nisinstance(x, collections.Sequence)"
4335 self.check(b, a)
4336
4337 def test_operator_isMappingType(self):
4338 b = "operator.isMappingType(x)"
4339 a = "import collections\nisinstance(x, collections.Mapping)"
4340 self.check(b, a)
4341
4342 def test_operator_isNumberType(self):
4343 b = "operator.isNumberType(x)"
4344 a = "import numbers\nisinstance(x, numbers.Number)"
4345 self.check(b, a)
4346
4347 def test_operator_repeat(self):
4348 b = "operator.repeat(x, n)"
4349 a = "operator.mul(x, n)"
4350 self.check(b, a)
4351
4352 b = "operator .repeat(x, n)"
4353 a = "operator .mul(x, n)"
4354 self.check(b, a)
4355
4356 b = "operator. repeat(x, n)"
4357 a = "operator. mul(x, n)"
4358 self.check(b, a)
4359
4360 def test_operator_irepeat(self):
4361 b = "operator.irepeat(x, n)"
4362 a = "operator.imul(x, n)"
4363 self.check(b, a)
4364
4365 b = "operator .irepeat(x, n)"
4366 a = "operator .imul(x, n)"
4367 self.check(b, a)
4368
4369 b = "operator. irepeat(x, n)"
4370 a = "operator. imul(x, n)"
4371 self.check(b, a)
4372
4373 def test_bare_isCallable(self):
4374 s = "isCallable(x)"
4375 t = "You should use 'hasattr(x, '__call__')' here."
4376 self.warns_unchanged(s, t)
4377
4378 def test_bare_sequenceIncludes(self):
4379 s = "sequenceIncludes(x, y)"
4380 t = "You should use 'operator.contains(x, y)' here."
4381 self.warns_unchanged(s, t)
4382
4383 def test_bare_operator_isSequenceType(self):
4384 s = "isSequenceType(z)"
4385 t = "You should use 'isinstance(z, collections.Sequence)' here."
4386 self.warns_unchanged(s, t)
4387
4388 def test_bare_operator_isMappingType(self):
4389 s = "isMappingType(x)"
4390 t = "You should use 'isinstance(x, collections.Mapping)' here."
4391 self.warns_unchanged(s, t)
4392
4393 def test_bare_operator_isNumberType(self):
4394 s = "isNumberType(y)"
4395 t = "You should use 'isinstance(y, numbers.Number)' here."
4396 self.warns_unchanged(s, t)
4397
4398 def test_bare_operator_repeat(self):
4399 s = "repeat(x, n)"
4400 t = "You should use 'operator.mul(x, n)' here."
4401 self.warns_unchanged(s, t)
4402
4403 def test_bare_operator_irepeat(self):
4404 s = "irepeat(y, 187)"
4405 t = "You should use 'operator.imul(y, 187)' here."
4406 self.warns_unchanged(s, t)
4407
4408
4409 class Test_exitfunc(FixerTestCase):
4410
4411 fixer = "exitfunc"
4412
4413 def test_simple(self):
4414 b = """
4415 import sys
4416 sys.exitfunc = my_atexit
4417 """
4418 a = """
4419 import sys
4420 import atexit
4421 atexit.register(my_atexit)
4422 """
4423 self.check(b, a)
4424
4425 def test_names_import(self):
4426 b = """
4427 import sys, crumbs
4428 sys.exitfunc = my_func
4429 """
4430 a = """
4431 import sys, crumbs, atexit
4432 atexit.register(my_func)
4433 """
4434 self.check(b, a)
4435
4436 def test_complex_expression(self):
4437 b = """
4438 import sys
4439 sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression
4440 """
4441 a = """
4442 import sys
4443 import atexit
4444 atexit.register(do(d)/a()+complex(f=23, g=23)*expression)
4445 """
4446 self.check(b, a)
4447
4448 def test_comments(self):
4449 b = """
4450 import sys # Foo
4451 sys.exitfunc = f # Blah
4452 """
4453 a = """
4454 import sys
4455 import atexit # Foo
4456 atexit.register(f) # Blah
4457 """
4458 self.check(b, a)
4459
4460 b = """
4461 import apples, sys, crumbs, larry # Pleasant comments
4462 sys.exitfunc = func
4463 """
4464 a = """
4465 import apples, sys, crumbs, larry, atexit # Pleasant comments
4466 atexit.register(func)
4467 """
4468 self.check(b, a)
4469
4470 def test_in_a_function(self):
4471 b = """
4472 import sys
4473 def f():
4474 sys.exitfunc = func
4475 """
4476 a = """
4477 import sys
4478 import atexit
4479 def f():
4480 atexit.register(func)
4481 """
4482 self.check(b, a)
4483
4484 def test_no_sys_import(self):
4485 b = """sys.exitfunc = f"""
4486 a = """atexit.register(f)"""
4487 msg = ("Can't find sys import; Please add an atexit import at the "
4488 "top of your file.")
4489 self.warns(b, a, msg)
4490
4491
4492 def test_unchanged(self):
4493 s = """f(sys.exitfunc)"""
4494 self.unchanged(s)
4495
4496
4497 class Test_asserts(FixerTestCase):
4498
4499 fixer = "asserts"
4500
4501 def test_deprecated_names(self):
4502 tests = [
4503 ('self.assert_(True)', 'self.assertTrue(True)'),
4504 ('self.assertEquals(2, 2)', 'self.assertEqual(2, 2)'),
4505 ('self.assertNotEquals(2, 3)', 'self.assertNotEqual(2, 3)'),
4506 ('self.assertAlmostEquals(2, 3)', 'self.assertAlmostEqual(2, 3)'),
4507 ('self.assertNotAlmostEquals(2, 8)', 'self.assertNotAlmostEqual(2, 8)'),
4508 ('self.failUnlessEqual(2, 2)', 'self.assertEqual(2, 2)'),
4509 ('self.failIfEqual(2, 3)', 'self.assertNotEqual(2, 3)'),
4510 ('self.failUnlessAlmostEqual(2, 3)', 'self.assertAlmostEqual(2, 3)'),
4511 ('self.failIfAlmostEqual(2, 8)', 'self.assertNotAlmostEqual(2, 8)'),
4512 ('self.failUnless(True)', 'self.assertTrue(True)'),
4513 ('self.failUnlessRaises(foo)', 'self.assertRaises(foo)'),
4514 ('self.failIf(False)', 'self.assertFalse(False)'),
4515 ]
4516 for b, a in tests:
4517 self.check(b, a)
4518
4519 def test_variants(self):
4520 b = 'eq = self.assertEquals'
4521 a = 'eq = self.assertEqual'
4522 self.check(b, a)
4523 b = 'self.assertEquals(2, 3, msg="fail")'
4524 a = 'self.assertEqual(2, 3, msg="fail")'
4525 self.check(b, a)
4526 b = 'self.assertEquals(2, 3, msg="fail") # foo'
4527 a = 'self.assertEqual(2, 3, msg="fail") # foo'
4528 self.check(b, a)
4529 b = 'self.assertEquals (2, 3)'
4530 a = 'self.assertEqual (2, 3)'
4531 self.check(b, a)
4532 b = ' self.assertEquals (2, 3)'
4533 a = ' self.assertEqual (2, 3)'
4534 self.check(b, a)
4535 b = 'with self.failUnlessRaises(Explosion): explode()'
4536 a = 'with self.assertRaises(Explosion): explode()'
4537 self.check(b, a)
4538 b = 'with self.failUnlessRaises(Explosion) as cm: explode()'
4539 a = 'with self.assertRaises(Explosion) as cm: explode()'
4540 self.check(b, a)
4541
4542 def test_unchanged(self):
4543 self.unchanged('self.assertEqualsOnSaturday')
4544 self.unchanged('self.assertEqualsOnSaturday(3, 5)')