Debian Codegraph @

lib-python/3/lib2to3/tests/test_fixers.py

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