1""" Test suite for the fixer modules """ 2 3# Python imports 4import os 5from itertools import chain 6from operator import itemgetter 7 8# Local imports 9from lib2to3 import pygram, fixer_util 10from lib2to3.tests import support 11 12 13class 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 70class 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 97class 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 155class 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 288class 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 347class 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 396class 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 431class 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 537class 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 588class 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 626class 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 901class 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 1028class 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 1153class 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 1199class 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 1237class 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 1265class 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 1495class 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 1567class 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 1575reduce(x, range(5))""" 1576 self.check(b, a) 1577 1578class 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 1625class 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 1651class 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 1694class 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 1792class 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 1813class Test_imports2(FixerTestCase, ImportsFixerTests): 1814 fixer = "imports2" 1815 from ..fixes.fix_imports2 import MAPPING as modules 1816 1817 1818class 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 1834class 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 = """ 1893def foo(): 1894 from urllib import urlencode, urlopen 1895""" 1896 a = """ 1897def foo(): 1898 from urllib.parse import urlencode 1899 from urllib.request import urlopen 1900""" 1901 self.check(b, a) 1902 1903 b = """ 1904def foo(): 1905 other() 1906 from urllib import urlencode, urlopen 1907""" 1908 a = """ 1909def foo(): 1910 other() 1911 from urllib.parse import urlencode 1912 from urllib.request import urlopen 1913""" 1914 self.check(b, a) 1915 1916 1917 1918 def test_import_module_usage(self): 1919 for old, changes in self.modules.items(): 1920 for new, members in changes: 1921 for member in members: 1922 new_import = ", ".join([n for (n, mems) 1923 in self.modules[old]]) 1924 b = """ 1925 import %s 1926 foo(%s.%s) 1927 """ % (old, old, member) 1928 a = """ 1929 import %s 1930 foo(%s.%s) 1931 """ % (new_import, new, member) 1932 self.check(b, a) 1933 b = """ 1934 import %s 1935 %s.%s(%s.%s) 1936 """ % (old, old, member, old, member) 1937 a = """ 1938 import %s 1939 %s.%s(%s.%s) 1940 """ % (new_import, new, member, new, member) 1941 self.check(b, a) 1942 1943 1944class Test_input(FixerTestCase): 1945 fixer = "input" 1946 1947 def test_prefix_preservation(self): 1948 b = """x = input( )""" 1949 a = """x = eval(input( ))""" 1950 self.check(b, a) 1951 1952 b = """x = input( '' )""" 1953 a = """x = eval(input( '' ))""" 1954 self.check(b, a) 1955 1956 def test_trailing_comment(self): 1957 b = """x = input() # foo""" 1958 a = """x = eval(input()) # foo""" 1959 self.check(b, a) 1960 1961 def test_idempotency(self): 1962 s = """x = eval(input())""" 1963 self.unchanged(s) 1964 1965 s = """x = eval(input(''))""" 1966 self.unchanged(s) 1967 1968 s = """x = eval(input(foo(5) + 9))""" 1969 self.unchanged(s) 1970 1971 def test_1(self): 1972 b = """x = input()""" 1973 a = """x = eval(input())""" 1974 self.check(b, a) 1975 1976 def test_2(self): 1977 b = """x = input('')""" 1978 a = """x = eval(input(''))""" 1979 self.check(b, a) 1980 1981 def test_3(self): 1982 b = """x = input('prompt')""" 1983 a = """x = eval(input('prompt'))""" 1984 self.check(b, a) 1985 1986 def test_4(self): 1987 b = """x = input(foo(5) + 9)""" 1988 a = """x = eval(input(foo(5) + 9))""" 1989 self.check(b, a) 1990 1991class Test_tuple_params(FixerTestCase): 1992 fixer = "tuple_params" 1993 1994 def test_unchanged_1(self): 1995 s = """def foo(): pass""" 1996 self.unchanged(s) 1997 1998 def test_unchanged_2(self): 1999 s = """def foo(a, b, c): pass""" 2000 self.unchanged(s) 2001 2002 def test_unchanged_3(self): 2003 s = """def foo(a=3, b=4, c=5): pass""" 2004 self.unchanged(s) 2005 2006 def test_1(self): 2007 b = """ 2008 def foo(((a, b), c)): 2009 x = 5""" 2010 2011 a = """ 2012 def foo(xxx_todo_changeme): 2013 ((a, b), c) = xxx_todo_changeme 2014 x = 5""" 2015 self.check(b, a) 2016 2017 def test_2(self): 2018 b = """ 2019 def foo(((a, b), c), d): 2020 x = 5""" 2021 2022 a = """ 2023 def foo(xxx_todo_changeme, d): 2024 ((a, b), c) = xxx_todo_changeme 2025 x = 5""" 2026 self.check(b, a) 2027 2028 def test_3(self): 2029 b = """ 2030 def foo(((a, b), c), d) -> e: 2031 x = 5""" 2032 2033 a = """ 2034 def foo(xxx_todo_changeme, d) -> e: 2035 ((a, b), c) = xxx_todo_changeme 2036 x = 5""" 2037 self.check(b, a) 2038 2039 def test_semicolon(self): 2040 b = """ 2041 def foo(((a, b), c)): x = 5; y = 7""" 2042 2043 a = """ 2044 def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7""" 2045 self.check(b, a) 2046 2047 def test_keywords(self): 2048 b = """ 2049 def foo(((a, b), c), d, e=5) -> z: 2050 x = 5""" 2051 2052 a = """ 2053 def foo(xxx_todo_changeme, d, e=5) -> z: 2054 ((a, b), c) = xxx_todo_changeme 2055 x = 5""" 2056 self.check(b, a) 2057 2058 def test_varargs(self): 2059 b = """ 2060 def foo(((a, b), c), d, *vargs, **kwargs) -> z: 2061 x = 5""" 2062 2063 a = """ 2064 def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z: 2065 ((a, b), c) = xxx_todo_changeme 2066 x = 5""" 2067 self.check(b, a) 2068 2069 def test_multi_1(self): 2070 b = """ 2071 def foo(((a, b), c), (d, e, f)) -> z: 2072 x = 5""" 2073 2074 a = """ 2075 def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z: 2076 ((a, b), c) = xxx_todo_changeme 2077 (d, e, f) = xxx_todo_changeme1 2078 x = 5""" 2079 self.check(b, a) 2080 2081 def test_multi_2(self): 2082 b = """ 2083 def foo(x, ((a, b), c), d, (e, f, g), y) -> z: 2084 x = 5""" 2085 2086 a = """ 2087 def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z: 2088 ((a, b), c) = xxx_todo_changeme 2089 (e, f, g) = xxx_todo_changeme1 2090 x = 5""" 2091 self.check(b, a) 2092 2093 def test_docstring(self): 2094 b = """ 2095 def foo(((a, b), c), (d, e, f)) -> z: 2096 "foo foo foo foo" 2097 x = 5""" 2098 2099 a = """ 2100 def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z: 2101 "foo foo foo foo" 2102 ((a, b), c) = xxx_todo_changeme 2103 (d, e, f) = xxx_todo_changeme1 2104 x = 5""" 2105 self.check(b, a) 2106 2107 def test_lambda_no_change(self): 2108 s = """lambda x: x + 5""" 2109 self.unchanged(s) 2110 2111 def test_lambda_parens_single_arg(self): 2112 b = """lambda (x): x + 5""" 2113 a = """lambda x: x + 5""" 2114 self.check(b, a) 2115 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 def test_lambda_simple(self): 2129 b = """lambda (x, y): x + f(y)""" 2130 a = """lambda x_y: x_y[0] + f(x_y[1])""" 2131 self.check(b, a) 2132 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 def test_lambda_one_tuple(self): 2146 b = """lambda (x,): x + f(x)""" 2147 a = """lambda x1: x1[0] + f(x1[0])""" 2148 self.check(b, a) 2149 2150 b = """lambda (((x,))): x + f(x)""" 2151 a = """lambda x1: x1[0] + f(x1[0])""" 2152 self.check(b, a) 2153 2154 def test_lambda_simple_multi_use(self): 2155 b = """lambda (x, y): x + x + f(x) + x""" 2156 a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]""" 2157 self.check(b, a) 2158 2159 def test_lambda_simple_reverse(self): 2160 b = """lambda (x, y): y + x""" 2161 a = """lambda x_y: x_y[1] + x_y[0]""" 2162 self.check(b, a) 2163 2164 def test_lambda_nested(self): 2165 b = """lambda (x, (y, z)): x + y + z""" 2166 a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]""" 2167 self.check(b, a) 2168 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 def test_lambda_nested_multi_use(self): 2174 b = """lambda (x, (y, z)): x + y + f(y)""" 2175 a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])""" 2176 self.check(b, a) 2177 2178class Test_methodattrs(FixerTestCase): 2179 fixer = "methodattrs" 2180 2181 attrs = ["func", "self", "class"] 2182 2183 def test(self): 2184 for attr in self.attrs: 2185 b = "a.im_%s" % attr 2186 if attr == "class": 2187 a = "a.__self__.__class__" 2188 else: 2189 a = "a.__%s__" % attr 2190 self.check(b, a) 2191 2192 b = "self.foo.im_%s.foo_bar" % attr 2193 if attr == "class": 2194 a = "self.foo.__self__.__class__.foo_bar" 2195 else: 2196 a = "self.foo.__%s__.foo_bar" % attr 2197 self.check(b, a) 2198 2199 def test_unchanged(self): 2200 for attr in self.attrs: 2201 s = "foo(im_%s + 5)" % attr 2202 self.unchanged(s) 2203 2204 s = "f(foo.__%s__)" % attr 2205 self.unchanged(s) 2206 2207 s = "f(foo.__%s__.foo)" % attr 2208 self.unchanged(s) 2209 2210class Test_next(FixerTestCase): 2211 fixer = "next" 2212 2213 def test_1(self): 2214 b = """it.next()""" 2215 a = """next(it)""" 2216 self.check(b, a) 2217 2218 def test_2(self): 2219 b = """a.b.c.d.next()""" 2220 a = """next(a.b.c.d)""" 2221 self.check(b, a) 2222 2223 def test_3(self): 2224 b = """(a + b).next()""" 2225 a = """next((a + b))""" 2226 self.check(b, a) 2227 2228 def test_4(self): 2229 b = """a().next()""" 2230 a = """next(a())""" 2231 self.check(b, a) 2232 2233 def test_5(self): 2234 b = """a().next() + b""" 2235 a = """next(a()) + b""" 2236 self.check(b, a) 2237 2238 def test_6(self): 2239 b = """c( a().next() + b)""" 2240 a = """c( next(a()) + b)""" 2241 self.check(b, a) 2242 2243 def test_prefix_preservation_1(self): 2244 b = """ 2245 for a in b: 2246 foo(a) 2247 a.next() 2248 """ 2249 a = """ 2250 for a in b: 2251 foo(a) 2252 next(a) 2253 """ 2254 self.check(b, a) 2255 2256 def test_prefix_preservation_2(self): 2257 b = """ 2258 for a in b: 2259 foo(a) # abc 2260 # def 2261 a.next() 2262 """ 2263 a = """ 2264 for a in b: 2265 foo(a) # abc 2266 # def 2267 next(a) 2268 """ 2269 self.check(b, a) 2270 2271 def test_prefix_preservation_3(self): 2272 b = """ 2273 next = 5 2274 for a in b: 2275 foo(a) 2276 a.next() 2277 """ 2278 a = """ 2279 next = 5 2280 for a in b: 2281 foo(a) 2282 a.__next__() 2283 """ 2284 self.check(b, a, ignore_warnings=True) 2285 2286 def test_prefix_preservation_4(self): 2287 b = """ 2288 next = 5 2289 for a in b: 2290 foo(a) # abc 2291 # def 2292 a.next() 2293 """ 2294 a = """ 2295 next = 5 2296 for a in b: 2297 foo(a) # abc 2298 # def 2299 a.__next__() 2300 """ 2301 self.check(b, a, ignore_warnings=True) 2302 2303 def test_prefix_preservation_5(self): 2304 b = """ 2305 next = 5 2306 for a in b: 2307 foo(foo(a), # abc 2308 a.next()) 2309 """ 2310 a = """ 2311 next = 5 2312 for a in b: 2313 foo(foo(a), # abc 2314 a.__next__()) 2315 """ 2316 self.check(b, a, ignore_warnings=True) 2317 2318 def test_prefix_preservation_6(self): 2319 b = """ 2320 for a in b: 2321 foo(foo(a), # abc 2322 a.next()) 2323 """ 2324 a = """ 2325 for a in b: 2326 foo(foo(a), # abc 2327 next(a)) 2328 """ 2329 self.check(b, a) 2330 2331 def test_method_1(self): 2332 b = """ 2333 class A: 2334 def next(self): 2335 pass 2336 """ 2337 a = """ 2338 class A: 2339 def __next__(self): 2340 pass 2341 """ 2342 self.check(b, a) 2343 2344 def test_method_2(self): 2345 b = """ 2346 class A(object): 2347 def next(self): 2348 pass 2349 """ 2350 a = """ 2351 class A(object): 2352 def __next__(self): 2353 pass 2354 """ 2355 self.check(b, a) 2356 2357 def test_method_3(self): 2358 b = """ 2359 class A: 2360 def next(x): 2361 pass 2362 """ 2363 a = """ 2364 class A: 2365 def __next__(x): 2366 pass 2367 """ 2368 self.check(b, a) 2369 2370 def test_method_4(self): 2371 b = """ 2372 class A: 2373 def __init__(self, foo): 2374 self.foo = foo 2375 2376 def next(self): 2377 pass 2378 2379 def __iter__(self): 2380 return self 2381 """ 2382 a = """ 2383 class A: 2384 def __init__(self, foo): 2385 self.foo = foo 2386 2387 def __next__(self): 2388 pass 2389 2390 def __iter__(self): 2391 return self 2392 """ 2393 self.check(b, a) 2394 2395 def test_method_unchanged(self): 2396 s = """ 2397 class A: 2398 def next(self, a, b): 2399 pass 2400 """ 2401 self.unchanged(s) 2402 2403 def test_shadowing_assign_simple(self): 2404 s = """ 2405 next = foo 2406 2407 class A: 2408 def next(self, a, b): 2409 pass 2410 """ 2411 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2412 2413 def test_shadowing_assign_tuple_1(self): 2414 s = """ 2415 (next, a) = foo 2416 2417 class A: 2418 def next(self, a, b): 2419 pass 2420 """ 2421 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2422 2423 def test_shadowing_assign_tuple_2(self): 2424 s = """ 2425 (a, (b, (next, c)), a) = foo 2426 2427 class A: 2428 def next(self, a, b): 2429 pass 2430 """ 2431 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2432 2433 def test_shadowing_assign_list_1(self): 2434 s = """ 2435 [next, a] = foo 2436 2437 class A: 2438 def next(self, a, b): 2439 pass 2440 """ 2441 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2442 2443 def test_shadowing_assign_list_2(self): 2444 s = """ 2445 [a, [b, [next, c]], a] = foo 2446 2447 class A: 2448 def next(self, a, b): 2449 pass 2450 """ 2451 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2452 2453 def test_builtin_assign(self): 2454 s = """ 2455 def foo(): 2456 __builtin__.next = foo 2457 2458 class A: 2459 def next(self, a, b): 2460 pass 2461 """ 2462 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2463 2464 def test_builtin_assign_in_tuple(self): 2465 s = """ 2466 def foo(): 2467 (a, __builtin__.next) = foo 2468 2469 class A: 2470 def next(self, a, b): 2471 pass 2472 """ 2473 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2474 2475 def test_builtin_assign_in_list(self): 2476 s = """ 2477 def foo(): 2478 [a, __builtin__.next] = foo 2479 2480 class A: 2481 def next(self, a, b): 2482 pass 2483 """ 2484 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2485 2486 def test_assign_to_next(self): 2487 s = """ 2488 def foo(): 2489 A.next = foo 2490 2491 class A: 2492 def next(self, a, b): 2493 pass 2494 """ 2495 self.unchanged(s) 2496 2497 def test_assign_to_next_in_tuple(self): 2498 s = """ 2499 def foo(): 2500 (a, A.next) = foo 2501 2502 class A: 2503 def next(self, a, b): 2504 pass 2505 """ 2506 self.unchanged(s) 2507 2508 def test_assign_to_next_in_list(self): 2509 s = """ 2510 def foo(): 2511 [a, A.next] = foo 2512 2513 class A: 2514 def next(self, a, b): 2515 pass 2516 """ 2517 self.unchanged(s) 2518 2519 def test_shadowing_import_1(self): 2520 s = """ 2521 import foo.bar as next 2522 2523 class A: 2524 def next(self, a, b): 2525 pass 2526 """ 2527 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2528 2529 def test_shadowing_import_2(self): 2530 s = """ 2531 import bar, bar.foo as next 2532 2533 class A: 2534 def next(self, a, b): 2535 pass 2536 """ 2537 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2538 2539 def test_shadowing_import_3(self): 2540 s = """ 2541 import bar, bar.foo as next, baz 2542 2543 class A: 2544 def next(self, a, b): 2545 pass 2546 """ 2547 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2548 2549 def test_shadowing_import_from_1(self): 2550 s = """ 2551 from x import next 2552 2553 class A: 2554 def next(self, a, b): 2555 pass 2556 """ 2557 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2558 2559 def test_shadowing_import_from_2(self): 2560 s = """ 2561 from x.a import next 2562 2563 class A: 2564 def next(self, a, b): 2565 pass 2566 """ 2567 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2568 2569 def test_shadowing_import_from_3(self): 2570 s = """ 2571 from x import a, next, b 2572 2573 class A: 2574 def next(self, a, b): 2575 pass 2576 """ 2577 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2578 2579 def test_shadowing_import_from_4(self): 2580 s = """ 2581 from x.a import a, next, b 2582 2583 class A: 2584 def next(self, a, b): 2585 pass 2586 """ 2587 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2588 2589 def test_shadowing_funcdef_1(self): 2590 s = """ 2591 def next(a): 2592 pass 2593 2594 class A: 2595 def next(self, a, b): 2596 pass 2597 """ 2598 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2599 2600 def test_shadowing_funcdef_2(self): 2601 b = """ 2602 def next(a): 2603 pass 2604 2605 class A: 2606 def next(self): 2607 pass 2608 2609 it.next() 2610 """ 2611 a = """ 2612 def next(a): 2613 pass 2614 2615 class A: 2616 def __next__(self): 2617 pass 2618 2619 it.__next__() 2620 """ 2621 self.warns(b, a, "Calls to builtin next() possibly shadowed") 2622 2623 def test_shadowing_global_1(self): 2624 s = """ 2625 def f(): 2626 global next 2627 next = 5 2628 """ 2629 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2630 2631 def test_shadowing_global_2(self): 2632 s = """ 2633 def f(): 2634 global a, next, b 2635 next = 5 2636 """ 2637 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2638 2639 def test_shadowing_for_simple(self): 2640 s = """ 2641 for next in it(): 2642 pass 2643 2644 b = 5 2645 c = 6 2646 """ 2647 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2648 2649 def test_shadowing_for_tuple_1(self): 2650 s = """ 2651 for next, b in it(): 2652 pass 2653 2654 b = 5 2655 c = 6 2656 """ 2657 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2658 2659 def test_shadowing_for_tuple_2(self): 2660 s = """ 2661 for a, (next, c), b in it(): 2662 pass 2663 2664 b = 5 2665 c = 6 2666 """ 2667 self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") 2668 2669 def test_noncall_access_1(self): 2670 b = """gnext = g.next""" 2671 a = """gnext = g.__next__""" 2672 self.check(b, a) 2673 2674 def test_noncall_access_2(self): 2675 b = """f(g.next + 5)""" 2676 a = """f(g.__next__ + 5)""" 2677 self.check(b, a) 2678 2679 def test_noncall_access_3(self): 2680 b = """f(g().next + 5)""" 2681 a = """f(g().__next__ + 5)""" 2682 self.check(b, a) 2683 2684class Test_nonzero(FixerTestCase): 2685 fixer = "nonzero" 2686 2687 def test_1(self): 2688 b = """ 2689 class A: 2690 def __nonzero__(self): 2691 pass 2692 """ 2693 a = """ 2694 class A: 2695 def __bool__(self): 2696 pass 2697 """ 2698 self.check(b, a) 2699 2700 def test_2(self): 2701 b = """ 2702 class A(object): 2703 def __nonzero__(self): 2704 pass 2705 """ 2706 a = """ 2707 class A(object): 2708 def __bool__(self): 2709 pass 2710 """ 2711 self.check(b, a) 2712 2713 def test_unchanged_1(self): 2714 s = """ 2715 class A(object): 2716 def __bool__(self): 2717 pass 2718 """ 2719 self.unchanged(s) 2720 2721 def test_unchanged_2(self): 2722 s = """ 2723 class A(object): 2724 def __nonzero__(self, a): 2725 pass 2726 """ 2727 self.unchanged(s) 2728 2729 def test_unchanged_func(self): 2730 s = """ 2731 def __nonzero__(self): 2732 pass 2733 """ 2734 self.unchanged(s) 2735 2736class Test_numliterals(FixerTestCase): 2737 fixer = "numliterals" 2738 2739 def test_octal_1(self): 2740 b = """0755""" 2741 a = """0o755""" 2742 self.check(b, a) 2743 2744 def test_long_int_1(self): 2745 b = """a = 12L""" 2746 a = """a = 12""" 2747 self.check(b, a) 2748 2749 def test_long_int_2(self): 2750 b = """a = 12l""" 2751 a = """a = 12""" 2752 self.check(b, a) 2753 2754 def test_long_hex(self): 2755 b = """b = 0x12l""" 2756 a = """b = 0x12""" 2757 self.check(b, a) 2758 2759 def test_comments_and_spacing(self): 2760 b = """b = 0x12L""" 2761 a = """b = 0x12""" 2762 self.check(b, a) 2763 2764 b = """b = 0755 # spam""" 2765 a = """b = 0o755 # spam""" 2766 self.check(b, a) 2767 2768 def test_unchanged_int(self): 2769 s = """5""" 2770 self.unchanged(s) 2771 2772 def test_unchanged_float(self): 2773 s = """5.0""" 2774 self.unchanged(s) 2775 2776 def test_unchanged_octal(self): 2777 s = """0o755""" 2778 self.unchanged(s) 2779 2780 def test_unchanged_hex(self): 2781 s = """0xABC""" 2782 self.unchanged(s) 2783 2784 def test_unchanged_exp(self): 2785 s = """5.0e10""" 2786 self.unchanged(s) 2787 2788 def test_unchanged_complex_int(self): 2789 s = """5 + 4j""" 2790 self.unchanged(s) 2791 2792 def test_unchanged_complex_float(self): 2793 s = """5.4 + 4.9j""" 2794 self.unchanged(s) 2795 2796 def test_unchanged_complex_bare(self): 2797 s = """4j""" 2798 self.unchanged(s) 2799 s = """4.4j""" 2800 self.unchanged(s) 2801 2802class Test_renames(FixerTestCase): 2803 fixer = "renames" 2804 2805 modules = {"sys": ("maxint", "maxsize"), 2806 } 2807 2808 def test_import_from(self): 2809 for mod, (old, new) in list(self.modules.items()): 2810 b = "from %s import %s" % (mod, old) 2811 a = "from %s import %s" % (mod, new) 2812 self.check(b, a) 2813 2814 s = "from foo import %s" % old 2815 self.unchanged(s) 2816 2817 def test_import_from_as(self): 2818 for mod, (old, new) in list(self.modules.items()): 2819 b = "from %s import %s as foo_bar" % (mod, old) 2820 a = "from %s import %s as foo_bar" % (mod, new) 2821 self.check(b, a) 2822 2823 def test_import_module_usage(self): 2824 for mod, (old, new) in list(self.modules.items()): 2825 b = """ 2826 import %s 2827 foo(%s, %s.%s) 2828 """ % (mod, mod, mod, old) 2829 a = """ 2830 import %s 2831 foo(%s, %s.%s) 2832 """ % (mod, mod, mod, new) 2833 self.check(b, a) 2834 2835 def XXX_test_from_import_usage(self): 2836 # not implemented yet 2837 for mod, (old, new) in list(self.modules.items()): 2838 b = """ 2839 from %s import %s 2840 foo(%s, %s) 2841 """ % (mod, old, mod, old) 2842 a = """ 2843 from %s import %s 2844 foo(%s, %s) 2845 """ % (mod, new, mod, new) 2846 self.check(b, a) 2847 2848class Test_unicode(FixerTestCase): 2849 fixer = "unicode" 2850 2851 def test_whitespace(self): 2852 b = """unicode( x)""" 2853 a = """str( x)""" 2854 self.check(b, a) 2855 2856 b = """ unicode(x )""" 2857 a = """ str(x )""" 2858 self.check(b, a) 2859 2860 b = """ u'h'""" 2861 a = """ 'h'""" 2862 self.check(b, a) 2863 2864 def test_unicode_call(self): 2865 b = """unicode(x, y, z)""" 2866 a = """str(x, y, z)""" 2867 self.check(b, a) 2868 2869 def test_unichr(self): 2870 b = """unichr(u'h')""" 2871 a = """chr('h')""" 2872 self.check(b, a) 2873 2874 def test_unicode_literal_1(self): 2875 b = '''u"x"''' 2876 a = '''"x"''' 2877 self.check(b, a) 2878 2879 def test_unicode_literal_2(self): 2880 b = """ur'x'""" 2881 a = """r'x'""" 2882 self.check(b, a) 2883 2884 def test_unicode_literal_3(self): 2885 b = """UR'''x''' """ 2886 a = """R'''x''' """ 2887 self.check(b, a) 2888 2889 def test_native_literal_escape_u(self): 2890 b = r"""'\\\u20ac\U0001d121\\u20ac'""" 2891 a = r"""'\\\\u20ac\\U0001d121\\u20ac'""" 2892 self.check(b, a) 2893 2894 b = r"""r'\\\u20ac\U0001d121\\u20ac'""" 2895 a = r"""r'\\\u20ac\U0001d121\\u20ac'""" 2896 self.check(b, a) 2897 2898 def test_bytes_literal_escape_u(self): 2899 b = r"""b'\\\u20ac\U0001d121\\u20ac'""" 2900 a = r"""b'\\\u20ac\U0001d121\\u20ac'""" 2901 self.check(b, a) 2902 2903 b = r"""br'\\\u20ac\U0001d121\\u20ac'""" 2904 a = r"""br'\\\u20ac\U0001d121\\u20ac'""" 2905 self.check(b, a) 2906 2907 def test_unicode_literal_escape_u(self): 2908 b = r"""u'\\\u20ac\U0001d121\\u20ac'""" 2909 a = r"""'\\\u20ac\U0001d121\\u20ac'""" 2910 self.check(b, a) 2911 2912 b = r"""ur'\\\u20ac\U0001d121\\u20ac'""" 2913 a = r"""r'\\\u20ac\U0001d121\\u20ac'""" 2914 self.check(b, a) 2915 2916 def test_native_unicode_literal_escape_u(self): 2917 f = 'from __future__ import unicode_literals\n' 2918 b = f + r"""'\\\u20ac\U0001d121\\u20ac'""" 2919 a = f + r"""'\\\u20ac\U0001d121\\u20ac'""" 2920 self.check(b, a) 2921 2922 b = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" 2923 a = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" 2924 self.check(b, a) 2925 2926 2927class Test_filter(FixerTestCase): 2928 fixer = "filter" 2929 2930 def test_prefix_preservation(self): 2931 b = """x = filter( foo, 'abc' )""" 2932 a = """x = list(filter( foo, 'abc' ))""" 2933 self.check(b, a) 2934 2935 b = """x = filter( None , 'abc' )""" 2936 a = """x = [_f for _f in 'abc' if _f]""" 2937 self.check(b, a) 2938 2939 def test_filter_basic(self): 2940 b = """x = filter(None, 'abc')""" 2941 a = """x = [_f for _f in 'abc' if _f]""" 2942 self.check(b, a) 2943 2944 b = """x = len(filter(f, 'abc'))""" 2945 a = """x = len(list(filter(f, 'abc')))""" 2946 self.check(b, a) 2947 2948 b = """x = filter(lambda x: x%2 == 0, range(10))""" 2949 a = """x = [x for x in range(10) if x%2 == 0]""" 2950 self.check(b, a) 2951 2952 # Note the parens around x 2953 b = """x = filter(lambda (x): x%2 == 0, range(10))""" 2954 a = """x = [x for x in range(10) if x%2 == 0]""" 2955 self.check(b, a) 2956 2957 def test_filter_trailers(self): 2958 b = """x = filter(None, 'abc')[0]""" 2959 a = """x = [_f for _f in 'abc' if _f][0]""" 2960 self.check(b, a) 2961 2962 b = """x = len(filter(f, 'abc')[0])""" 2963 a = """x = len(list(filter(f, 'abc'))[0])""" 2964 self.check(b, a) 2965 2966 b = """x = filter(lambda x: x%2 == 0, range(10))[0]""" 2967 a = """x = [x for x in range(10) if x%2 == 0][0]""" 2968 self.check(b, a) 2969 2970 # Note the parens around x 2971 b = """x = filter(lambda (x): x%2 == 0, range(10))[0]""" 2972 a = """x = [x for x in range(10) if x%2 == 0][0]""" 2973 self.check(b, a) 2974 2975 def test_filter_nochange(self): 2976 a = """b.join(filter(f, 'abc'))""" 2977 self.unchanged(a) 2978 a = """(a + foo(5)).join(filter(f, 'abc'))""" 2979 self.unchanged(a) 2980 a = """iter(filter(f, 'abc'))""" 2981 self.unchanged(a) 2982 a = """list(filter(f, 'abc'))""" 2983 self.unchanged(a) 2984 a = """list(filter(f, 'abc'))[0]""" 2985 self.unchanged(a) 2986 a = """set(filter(f, 'abc'))""" 2987 self.unchanged(a) 2988 a = """set(filter(f, 'abc')).pop()""" 2989 self.unchanged(a) 2990 a = """tuple(filter(f, 'abc'))""" 2991 self.unchanged(a) 2992 a = """any(filter(f, 'abc'))""" 2993 self.unchanged(a) 2994 a = """all(filter(f, 'abc'))""" 2995 self.unchanged(a) 2996 a = """sum(filter(f, 'abc'))""" 2997 self.unchanged(a) 2998 a = """sorted(filter(f, 'abc'))""" 2999 self.unchanged(a) 3000 a = """sorted(filter(f, 'abc'), key=blah)""" 3001 self.unchanged(a) 3002 a = """sorted(filter(f, 'abc'), key=blah)[0]""" 3003 self.unchanged(a) 3004 a = """enumerate(filter(f, 'abc'))""" 3005 self.unchanged(a) 3006 a = """enumerate(filter(f, 'abc'), start=1)""" 3007 self.unchanged(a) 3008 a = """for i in filter(f, 'abc'): pass""" 3009 self.unchanged(a) 3010 a = """[x for x in filter(f, 'abc')]""" 3011 self.unchanged(a) 3012 a = """(x for x in filter(f, 'abc'))""" 3013 self.unchanged(a) 3014 3015 def test_future_builtins(self): 3016 a = "from future_builtins import spam, filter; filter(f, 'ham')" 3017 self.unchanged(a) 3018 3019 b = """from future_builtins import spam; x = filter(f, 'abc')""" 3020 a = """from future_builtins import spam; x = list(filter(f, 'abc'))""" 3021 self.check(b, a) 3022 3023 a = "from future_builtins import *; filter(f, 'ham')" 3024 self.unchanged(a) 3025 3026class Test_map(FixerTestCase): 3027 fixer = "map" 3028 3029 def check(self, b, a): 3030 self.unchanged("from future_builtins import map; " + b, a) 3031 super(Test_map, self).check(b, a) 3032 3033 def test_prefix_preservation(self): 3034 b = """x = map( f, 'abc' )""" 3035 a = """x = list(map( f, 'abc' ))""" 3036 self.check(b, a) 3037 3038 def test_map_trailers(self): 3039 b = """x = map(f, 'abc')[0]""" 3040 a = """x = list(map(f, 'abc'))[0]""" 3041 self.check(b, a) 3042 3043 b = """x = map(None, l)[0]""" 3044 a = """x = list(l)[0]""" 3045 self.check(b, a) 3046 3047 b = """x = map(lambda x:x, l)[0]""" 3048 a = """x = [x for x in l][0]""" 3049 self.check(b, a) 3050 3051 b = """x = map(f, 'abc')[0][1]""" 3052 a = """x = list(map(f, 'abc'))[0][1]""" 3053 self.check(b, a) 3054 3055 def test_trailing_comment(self): 3056 b = """x = map(f, 'abc') # foo""" 3057 a = """x = list(map(f, 'abc')) # foo""" 3058 self.check(b, a) 3059 3060 def test_None_with_multiple_arguments(self): 3061 s = """x = map(None, a, b, c)""" 3062 self.warns_unchanged(s, "cannot convert map(None, ...) with " 3063 "multiple arguments") 3064 3065 def test_map_basic(self): 3066 b = """x = map(f, 'abc')""" 3067 a = """x = list(map(f, 'abc'))""" 3068 self.check(b, a) 3069 3070 b = """x = len(map(f, 'abc', 'def'))""" 3071 a = """x = len(list(map(f, 'abc', 'def')))""" 3072 self.check(b, a) 3073 3074 b = """x = map(None, 'abc')""" 3075 a = """x = list('abc')""" 3076 self.check(b, a) 3077 3078 b = """x = map(lambda x: x+1, range(4))""" 3079 a = """x = [x+1 for x in range(4)]""" 3080 self.check(b, a) 3081 3082 # Note the parens around x 3083 b = """x = map(lambda (x): x+1, range(4))""" 3084 a = """x = [x+1 for x in range(4)]""" 3085 self.check(b, a) 3086 3087 b = """ 3088 foo() 3089 # foo 3090 map(f, x) 3091 """ 3092 a = """ 3093 foo() 3094 # foo 3095 list(map(f, x)) 3096 """ 3097 self.warns(b, a, "You should use a for loop here") 3098 3099 def test_map_nochange(self): 3100 a = """b.join(map(f, 'abc'))""" 3101 self.unchanged(a) 3102 a = """(a + foo(5)).join(map(f, 'abc'))""" 3103 self.unchanged(a) 3104 a = """iter(map(f, 'abc'))""" 3105 self.unchanged(a) 3106 a = """list(map(f, 'abc'))""" 3107 self.unchanged(a) 3108 a = """list(map(f, 'abc'))[0]""" 3109 self.unchanged(a) 3110 a = """set(map(f, 'abc'))""" 3111 self.unchanged(a) 3112 a = """set(map(f, 'abc')).pop()""" 3113 self.unchanged(a) 3114 a = """tuple(map(f, 'abc'))""" 3115 self.unchanged(a) 3116 a = """any(map(f, 'abc'))""" 3117 self.unchanged(a) 3118 a = """all(map(f, 'abc'))""" 3119 self.unchanged(a) 3120 a = """sum(map(f, 'abc'))""" 3121 self.unchanged(a) 3122 a = """sorted(map(f, 'abc'))""" 3123 self.unchanged(a) 3124 a = """sorted(map(f, 'abc'), key=blah)""" 3125 self.unchanged(a) 3126 a = """sorted(map(f, 'abc'), key=blah)[0]""" 3127 self.unchanged(a) 3128 a = """enumerate(map(f, 'abc'))""" 3129 self.unchanged(a) 3130 a = """enumerate(map(f, 'abc'), start=1)""" 3131 self.unchanged(a) 3132 a = """for i in map(f, 'abc'): pass""" 3133 self.unchanged(a) 3134 a = """[x for x in map(f, 'abc')]""" 3135 self.unchanged(a) 3136 a = """(x for x in map(f, 'abc'))""" 3137 self.unchanged(a) 3138 3139 def test_future_builtins(self): 3140 a = "from future_builtins import spam, map, eggs; map(f, 'ham')" 3141 self.unchanged(a) 3142 3143 b = """from future_builtins import spam, eggs; x = map(f, 'abc')""" 3144 a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))""" 3145 self.check(b, a) 3146 3147 a = "from future_builtins import *; map(f, 'ham')" 3148 self.unchanged(a) 3149 3150class Test_zip(FixerTestCase): 3151 fixer = "zip" 3152 3153 def check(self, b, a): 3154 self.unchanged("from future_builtins import zip; " + b, a) 3155 super(Test_zip, self).check(b, a) 3156 3157 def test_zip_basic(self): 3158 b = """x = zip()""" 3159 a = """x = list(zip())""" 3160 self.check(b, a) 3161 3162 b = """x = zip(a, b, c)""" 3163 a = """x = list(zip(a, b, c))""" 3164 self.check(b, a) 3165 3166 b = """x = len(zip(a, b))""" 3167 a = """x = len(list(zip(a, b)))""" 3168 self.check(b, a) 3169 3170 def test_zip_trailers(self): 3171 b = """x = zip(a, b, c)[0]""" 3172 a = """x = list(zip(a, b, c))[0]""" 3173 self.check(b, a) 3174 3175 b = """x = zip(a, b, c)[0][1]""" 3176 a = """x = list(zip(a, b, c))[0][1]""" 3177 self.check(b, a) 3178 3179 def test_zip_nochange(self): 3180 a = """b.join(zip(a, b))""" 3181 self.unchanged(a) 3182 a = """(a + foo(5)).join(zip(a, b))""" 3183 self.unchanged(a) 3184 a = """iter(zip(a, b))""" 3185 self.unchanged(a) 3186 a = """list(zip(a, b))""" 3187 self.unchanged(a) 3188 a = """list(zip(a, b))[0]""" 3189 self.unchanged(a) 3190 a = """set(zip(a, b))""" 3191 self.unchanged(a) 3192 a = """set(zip(a, b)).pop()""" 3193 self.unchanged(a) 3194 a = """tuple(zip(a, b))""" 3195 self.unchanged(a) 3196 a = """any(zip(a, b))""" 3197 self.unchanged(a) 3198 a = """all(zip(a, b))""" 3199 self.unchanged(a) 3200 a = """sum(zip(a, b))""" 3201 self.unchanged(a) 3202 a = """sorted(zip(a, b))""" 3203 self.unchanged(a) 3204 a = """sorted(zip(a, b), key=blah)""" 3205 self.unchanged(a) 3206 a = """sorted(zip(a, b), key=blah)[0]""" 3207 self.unchanged(a) 3208 a = """enumerate(zip(a, b))""" 3209 self.unchanged(a) 3210 a = """enumerate(zip(a, b), start=1)""" 3211 self.unchanged(a) 3212 a = """for i in zip(a, b): pass""" 3213 self.unchanged(a) 3214 a = """[x for x in zip(a, b)]""" 3215 self.unchanged(a) 3216 a = """(x for x in zip(a, b))""" 3217 self.unchanged(a) 3218 3219 def test_future_builtins(self): 3220 a = "from future_builtins import spam, zip, eggs; zip(a, b)" 3221 self.unchanged(a) 3222 3223 b = """from future_builtins import spam, eggs; x = zip(a, b)""" 3224 a = """from future_builtins import spam, eggs; x = list(zip(a, b))""" 3225 self.check(b, a) 3226 3227 a = "from future_builtins import *; zip(a, b)" 3228 self.unchanged(a) 3229 3230class Test_standarderror(FixerTestCase): 3231 fixer = "standarderror" 3232 3233 def test(self): 3234 b = """x = StandardError()""" 3235 a = """x = Exception()""" 3236 self.check(b, a) 3237 3238 b = """x = StandardError(a, b, c)""" 3239 a = """x = Exception(a, b, c)""" 3240 self.check(b, a) 3241 3242 b = """f(2 + StandardError(a, b, c))""" 3243 a = """f(2 + Exception(a, b, c))""" 3244 self.check(b, a) 3245 3246class Test_types(FixerTestCase): 3247 fixer = "types" 3248 3249 def test_basic_types_convert(self): 3250 b = """types.StringType""" 3251 a = """bytes""" 3252 self.check(b, a) 3253 3254 b = """types.DictType""" 3255 a = """dict""" 3256 self.check(b, a) 3257 3258 b = """types . IntType""" 3259 a = """int""" 3260 self.check(b, a) 3261 3262 b = """types.ListType""" 3263 a = """list""" 3264 self.check(b, a) 3265 3266 b = """types.LongType""" 3267 a = """int""" 3268 self.check(b, a) 3269 3270 b = """types.NoneType""" 3271 a = """type(None)""" 3272 self.check(b, a) 3273 3274 b = "types.StringTypes" 3275 a = "(str,)" 3276 self.check(b, a) 3277 3278class Test_idioms(FixerTestCase): 3279 fixer = "idioms" 3280 3281 def test_while(self): 3282 b = """while 1: foo()""" 3283 a = """while True: foo()""" 3284 self.check(b, a) 3285 3286 b = """while 1: foo()""" 3287 a = """while True: foo()""" 3288 self.check(b, a) 3289 3290 b = """ 3291 while 1: 3292 foo() 3293 """ 3294 a = """ 3295 while True: 3296 foo() 3297 """ 3298 self.check(b, a) 3299 3300 def test_while_unchanged(self): 3301 s = """while 11: foo()""" 3302 self.unchanged(s) 3303 3304 s = """while 0: foo()""" 3305 self.unchanged(s) 3306 3307 s = """while foo(): foo()""" 3308 self.unchanged(s) 3309 3310 s = """while []: foo()""" 3311 self.unchanged(s) 3312 3313 def test_eq_simple(self): 3314 b = """type(x) == T""" 3315 a = """isinstance(x, T)""" 3316 self.check(b, a) 3317 3318 b = """if type(x) == T: pass""" 3319 a = """if isinstance(x, T): pass""" 3320 self.check(b, a) 3321 3322 def test_eq_reverse(self): 3323 b = """T == type(x)""" 3324 a = """isinstance(x, T)""" 3325 self.check(b, a) 3326 3327 b = """if T == type(x): pass""" 3328 a = """if isinstance(x, T): pass""" 3329 self.check(b, a) 3330 3331 def test_eq_expression(self): 3332 b = """type(x+y) == d.get('T')""" 3333 a = """isinstance(x+y, d.get('T'))""" 3334 self.check(b, a) 3335 3336 b = """type( x + y) == d.get('T')""" 3337 a = """isinstance(x + y, d.get('T'))""" 3338 self.check(b, a) 3339 3340 def test_is_simple(self): 3341 b = """type(x) is T""" 3342 a = """isinstance(x, T)""" 3343 self.check(b, a) 3344 3345 b = """if type(x) is T: pass""" 3346 a = """if isinstance(x, T): pass""" 3347 self.check(b, a) 3348 3349 def test_is_reverse(self): 3350 b = """T is type(x)""" 3351 a = """isinstance(x, T)""" 3352 self.check(b, a) 3353 3354 b = """if T is type(x): pass""" 3355 a = """if isinstance(x, T): pass""" 3356 self.check(b, a) 3357 3358 def test_is_expression(self): 3359 b = """type(x+y) is d.get('T')""" 3360 a = """isinstance(x+y, d.get('T'))""" 3361 self.check(b, a) 3362 3363 b = """type( x + y) is d.get('T')""" 3364 a = """isinstance(x + y, d.get('T'))""" 3365 self.check(b, a) 3366 3367 def test_is_not_simple(self): 3368 b = """type(x) is not T""" 3369 a = """not isinstance(x, T)""" 3370 self.check(b, a) 3371 3372 b = """if type(x) is not T: pass""" 3373 a = """if not isinstance(x, T): pass""" 3374 self.check(b, a) 3375 3376 def test_is_not_reverse(self): 3377 b = """T is not type(x)""" 3378 a = """not isinstance(x, T)""" 3379 self.check(b, a) 3380 3381 b = """if T is not type(x): pass""" 3382 a = """if not isinstance(x, T): pass""" 3383 self.check(b, a) 3384 3385 def test_is_not_expression(self): 3386 b = """type(x+y) is not d.get('T')""" 3387 a = """not isinstance(x+y, d.get('T'))""" 3388 self.check(b, a) 3389 3390 b = """type( x + y) is not d.get('T')""" 3391 a = """not isinstance(x + y, d.get('T'))""" 3392 self.check(b, a) 3393 3394 def test_ne_simple(self): 3395 b = """type(x) != T""" 3396 a = """not isinstance(x, T)""" 3397 self.check(b, a) 3398 3399 b = """if type(x) != T: pass""" 3400 a = """if not isinstance(x, T): pass""" 3401 self.check(b, a) 3402 3403 def test_ne_reverse(self): 3404 b = """T != type(x)""" 3405 a = """not isinstance(x, T)""" 3406 self.check(b, a) 3407 3408 b = """if T != type(x): pass""" 3409 a = """if not isinstance(x, T): pass""" 3410 self.check(b, a) 3411 3412 def test_ne_expression(self): 3413 b = """type(x+y) != d.get('T')""" 3414 a = """not isinstance(x+y, d.get('T'))""" 3415 self.check(b, a) 3416 3417 b = """type( x + y) != d.get('T')""" 3418 a = """not isinstance(x + y, d.get('T'))""" 3419 self.check(b, a) 3420 3421 def test_type_unchanged(self): 3422 a = """type(x).__name__""" 3423 self.unchanged(a) 3424 3425 def test_sort_list_call(self): 3426 b = """ 3427 v = list(t) 3428 v.sort() 3429 foo(v) 3430 """ 3431 a = """ 3432 v = sorted(t) 3433 foo(v) 3434 """ 3435 self.check(b, a) 3436 3437 b = """ 3438 v = list(foo(b) + d) 3439 v.sort() 3440 foo(v) 3441 """ 3442 a = """ 3443 v = sorted(foo(b) + d) 3444 foo(v) 3445 """ 3446 self.check(b, a) 3447 3448 b = """ 3449 while x: 3450 v = list(t) 3451 v.sort() 3452 foo(v) 3453 """ 3454 a = """ 3455 while x: 3456 v = sorted(t) 3457 foo(v) 3458 """ 3459 self.check(b, a) 3460 3461 b = """ 3462 v = list(t) 3463 # foo 3464 v.sort() 3465 foo(v) 3466 """ 3467 a = """ 3468 v = sorted(t) 3469 # foo 3470 foo(v) 3471 """ 3472 self.check(b, a) 3473 3474 b = r""" 3475 v = list( t) 3476 v.sort() 3477 foo(v) 3478 """ 3479 a = r""" 3480 v = sorted( t) 3481 foo(v) 3482 """ 3483 self.check(b, a) 3484 3485 b = r""" 3486 try: 3487 m = list(s) 3488 m.sort() 3489 except: pass 3490 """ 3491 3492 a = r""" 3493 try: 3494 m = sorted(s) 3495 except: pass 3496 """ 3497 self.check(b, a) 3498 3499 b = r""" 3500 try: 3501 m = list(s) 3502 # foo 3503 m.sort() 3504 except: pass 3505 """ 3506 3507 a = r""" 3508 try: 3509 m = sorted(s) 3510 # foo 3511 except: pass 3512 """ 3513 self.check(b, a) 3514 3515 b = r""" 3516 m = list(s) 3517 # more comments 3518 m.sort()""" 3519 3520 a = r""" 3521 m = sorted(s) 3522 # more comments""" 3523 self.check(b, a) 3524 3525 def test_sort_simple_expr(self): 3526 b = """ 3527 v = t 3528 v.sort() 3529 foo(v) 3530 """ 3531 a = """ 3532 v = sorted(t) 3533 foo(v) 3534 """ 3535 self.check(b, a) 3536 3537 b = """ 3538 v = foo(b) 3539 v.sort() 3540 foo(v) 3541 """ 3542 a = """ 3543 v = sorted(foo(b)) 3544 foo(v) 3545 """ 3546 self.check(b, a) 3547 3548 b = """ 3549 v = b.keys() 3550 v.sort() 3551 foo(v) 3552 """ 3553 a = """ 3554 v = sorted(b.keys()) 3555 foo(v) 3556 """ 3557 self.check(b, a) 3558 3559 b = """ 3560 v = foo(b) + d 3561 v.sort() 3562 foo(v) 3563 """ 3564 a = """ 3565 v = sorted(foo(b) + d) 3566 foo(v) 3567 """ 3568 self.check(b, a) 3569 3570 b = """ 3571 while x: 3572 v = t 3573 v.sort() 3574 foo(v) 3575 """ 3576 a = """ 3577 while x: 3578 v = sorted(t) 3579 foo(v) 3580 """ 3581 self.check(b, a) 3582 3583 b = """ 3584 v = t 3585 # foo 3586 v.sort() 3587 foo(v) 3588 """ 3589 a = """ 3590 v = sorted(t) 3591 # foo 3592 foo(v) 3593 """ 3594 self.check(b, a) 3595 3596 b = r""" 3597 v = t 3598 v.sort() 3599 foo(v) 3600 """ 3601 a = r""" 3602 v = sorted(t) 3603 foo(v) 3604 """ 3605 self.check(b, a) 3606 3607 def test_sort_unchanged(self): 3608 s = """ 3609 v = list(t) 3610 w.sort() 3611 foo(w) 3612 """ 3613 self.unchanged(s) 3614 3615 s = """ 3616 v = list(t) 3617 v.sort(u) 3618 foo(v) 3619 """ 3620 self.unchanged(s) 3621 3622class Test_basestring(FixerTestCase): 3623 fixer = "basestring" 3624 3625 def test_basestring(self): 3626 b = """isinstance(x, basestring)""" 3627 a = """isinstance(x, str)""" 3628 self.check(b, a) 3629 3630class Test_buffer(FixerTestCase): 3631 fixer = "buffer" 3632 3633 def test_buffer(self): 3634 b = """x = buffer(y)""" 3635 a = """x = memoryview(y)""" 3636 self.check(b, a) 3637 3638 def test_slicing(self): 3639 b = """buffer(y)[4:5]""" 3640 a = """memoryview(y)[4:5]""" 3641 self.check(b, a) 3642 3643class Test_future(FixerTestCase): 3644 fixer = "future" 3645 3646 def test_future(self): 3647 b = """from __future__ import braces""" 3648 a = """""" 3649 self.check(b, a) 3650 3651 b = """# comment\nfrom __future__ import braces""" 3652 a = """# comment\n""" 3653 self.check(b, a) 3654 3655 b = """from __future__ import braces\n# comment""" 3656 a = """\n# comment""" 3657 self.check(b, a) 3658 3659 def test_run_order(self): 3660 self.assert_runs_after('print') 3661 3662class Test_itertools(FixerTestCase): 3663 fixer = "itertools" 3664 3665 def checkall(self, before, after): 3666 # Because we need to check with and without the itertools prefix 3667 # and on each of the three functions, these loops make it all 3668 # much easier 3669 for i in ('itertools.', ''): 3670 for f in ('map', 'filter', 'zip'): 3671 b = before %(i+'i'+f) 3672 a = after %(f) 3673 self.check(b, a) 3674 3675 def test_0(self): 3676 # A simple example -- test_1 covers exactly the same thing, 3677 # but it's not quite as clear. 3678 b = "itertools.izip(a, b)" 3679 a = "zip(a, b)" 3680 self.check(b, a) 3681 3682 def test_1(self): 3683 b = """%s(f, a)""" 3684 a = """%s(f, a)""" 3685 self.checkall(b, a) 3686 3687 def test_qualified(self): 3688 b = """itertools.ifilterfalse(a, b)""" 3689 a = """itertools.filterfalse(a, b)""" 3690 self.check(b, a) 3691 3692 b = """itertools.izip_longest(a, b)""" 3693 a = """itertools.zip_longest(a, b)""" 3694 self.check(b, a) 3695 3696 def test_2(self): 3697 b = """ifilterfalse(a, b)""" 3698 a = """filterfalse(a, b)""" 3699 self.check(b, a) 3700 3701 b = """izip_longest(a, b)""" 3702 a = """zip_longest(a, b)""" 3703 self.check(b, a) 3704 3705 def test_space_1(self): 3706 b = """ %s(f, a)""" 3707 a = """ %s(f, a)""" 3708 self.checkall(b, a) 3709 3710 def test_space_2(self): 3711 b = """ itertools.ifilterfalse(a, b)""" 3712 a = """ itertools.filterfalse(a, b)""" 3713 self.check(b, a) 3714 3715 b = """ itertools.izip_longest(a, b)""" 3716 a = """ itertools.zip_longest(a, b)""" 3717 self.check(b, a) 3718 3719 def test_run_order(self): 3720 self.assert_runs_after('map', 'zip', 'filter') 3721 3722 3723class Test_itertools_imports(FixerTestCase): 3724 fixer = 'itertools_imports' 3725 3726 def test_reduced(self): 3727 b = "from itertools import imap, izip, foo" 3728 a = "from itertools import foo" 3729 self.check(b, a) 3730 3731 b = "from itertools import bar, imap, izip, foo" 3732 a = "from itertools import bar, foo" 3733 self.check(b, a) 3734 3735 b = "from itertools import chain, imap, izip" 3736 a = "from itertools import chain" 3737 self.check(b, a) 3738 3739 def test_comments(self): 3740 b = "#foo\nfrom itertools import imap, izip" 3741 a = "#foo\n" 3742 self.check(b, a) 3743 3744 def test_none(self): 3745 b = "from itertools import imap, izip" 3746 a = "" 3747 self.check(b, a) 3748 3749 b = "from itertools import izip" 3750 a = "" 3751 self.check(b, a) 3752 3753 def test_import_as(self): 3754 b = "from itertools import izip, bar as bang, imap" 3755 a = "from itertools import bar as bang" 3756 self.check(b, a) 3757 3758 b = "from itertools import izip as _zip, imap, bar" 3759 a = "from itertools import bar" 3760 self.check(b, a) 3761 3762 b = "from itertools import imap as _map" 3763 a = "" 3764 self.check(b, a) 3765 3766 b = "from itertools import imap as _map, izip as _zip" 3767 a = "" 3768 self.check(b, a) 3769 3770 s = "from itertools import bar as bang" 3771 self.unchanged(s) 3772 3773 def test_ifilter_and_zip_longest(self): 3774 for name in "filterfalse", "zip_longest": 3775 b = "from itertools import i%s" % (name,) 3776 a = "from itertools import %s" % (name,) 3777 self.check(b, a) 3778 3779 b = "from itertools import imap, i%s, foo" % (name,) 3780 a = "from itertools import %s, foo" % (name,) 3781 self.check(b, a) 3782 3783 b = "from itertools import bar, i%s, foo" % (name,) 3784 a = "from itertools import bar, %s, foo" % (name,) 3785 self.check(b, a) 3786 3787 def test_import_star(self): 3788 s = "from itertools import *" 3789 self.unchanged(s) 3790 3791 3792 def test_unchanged(self): 3793 s = "from itertools import foo" 3794 self.unchanged(s) 3795 3796 3797class Test_import(FixerTestCase): 3798 fixer = "import" 3799 3800 def setUp(self): 3801 super(Test_import, self).setUp() 3802 # Need to replace fix_import's exists method 3803 # so we can check that it's doing the right thing 3804 self.files_checked = [] 3805 self.present_files = set() 3806 self.always_exists = True 3807 def fake_exists(name): 3808 self.files_checked.append(name) 3809 return self.always_exists or (name in self.present_files) 3810 3811 from lib2to3.fixes import fix_import 3812 fix_import.exists = fake_exists 3813 3814 def tearDown(self): 3815 from lib2to3.fixes import fix_import 3816 fix_import.exists = os.path.exists 3817 3818 def check_both(self, b, a): 3819 self.always_exists = True 3820 super(Test_import, self).check(b, a) 3821 self.always_exists = False 3822 super(Test_import, self).unchanged(b) 3823 3824 def test_files_checked(self): 3825 def p(path): 3826 # Takes a unix path and returns a path with correct separators 3827 return os.path.pathsep.join(path.split("/")) 3828 3829 self.always_exists = False 3830 self.present_files = set(['__init__.py']) 3831 expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd') 3832 names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py")) 3833 3834 for name in names_to_test: 3835 self.files_checked = [] 3836 self.filename = name 3837 self.unchanged("import jam") 3838 3839 if os.path.dirname(name): 3840 name = os.path.dirname(name) + '/jam' 3841 else: 3842 name = 'jam' 3843 expected_checks = set(name + ext for ext in expected_extensions) 3844 expected_checks.add("__init__.py") 3845 3846 self.assertEqual(set(self.files_checked), expected_checks) 3847 3848 def test_not_in_package(self): 3849 s = "import bar" 3850 self.always_exists = False 3851 self.present_files = set(["bar.py"]) 3852 self.unchanged(s) 3853 3854 def test_with_absolute_import_enabled(self): 3855 s = "from __future__ import absolute_import\nimport bar" 3856 self.always_exists = False 3857 self.present_files = set(["__init__.py", "bar.py"]) 3858 self.unchanged(s) 3859 3860 def test_in_package(self): 3861 b = "import bar" 3862 a = "from . import bar" 3863 self.always_exists = False 3864 self.present_files = set(["__init__.py", "bar.py"]) 3865 self.check(b, a) 3866 3867 def test_import_from_package(self): 3868 b = "import bar" 3869 a = "from . import bar" 3870 self.always_exists = False 3871 self.present_files = set(["__init__.py", "bar" + os.path.sep]) 3872 self.check(b, a) 3873 3874 def test_already_relative_import(self): 3875 s = "from . import bar" 3876 self.unchanged(s) 3877 3878 def test_comments_and_indent(self): 3879 b = "import bar # Foo" 3880 a = "from . import bar # Foo" 3881 self.check(b, a) 3882 3883 def test_from(self): 3884 b = "from foo import bar, baz" 3885 a = "from .foo import bar, baz" 3886 self.check_both(b, a) 3887 3888 b = "from foo import bar" 3889 a = "from .foo import bar" 3890 self.check_both(b, a) 3891 3892 b = "from foo import (bar, baz)" 3893 a = "from .foo import (bar, baz)" 3894 self.check_both(b, a) 3895 3896 def test_dotted_from(self): 3897 b = "from green.eggs import ham" 3898 a = "from .green.eggs import ham" 3899 self.check_both(b, a) 3900 3901 def test_from_as(self): 3902 b = "from green.eggs import ham as spam" 3903 a = "from .green.eggs import ham as spam" 3904 self.check_both(b, a) 3905 3906 def test_import(self): 3907 b = "import foo" 3908 a = "from . import foo" 3909 self.check_both(b, a) 3910 3911 b = "import foo, bar" 3912 a = "from . import foo, bar" 3913 self.check_both(b, a) 3914 3915 b = "import foo, bar, x" 3916 a = "from . import foo, bar, x" 3917 self.check_both(b, a) 3918 3919 b = "import x, y, z" 3920 a = "from . import x, y, z" 3921 self.check_both(b, a) 3922 3923 def test_import_as(self): 3924 b = "import foo as x" 3925 a = "from . import foo as x" 3926 self.check_both(b, a) 3927 3928 b = "import a as b, b as c, c as d" 3929 a = "from . import a as b, b as c, c as d" 3930 self.check_both(b, a) 3931 3932 def test_local_and_absolute(self): 3933 self.always_exists = False 3934 self.present_files = set(["foo.py", "__init__.py"]) 3935 3936 s = "import foo, bar" 3937 self.warns_unchanged(s, "absolute and local imports together") 3938 3939 def test_dotted_import(self): 3940 b = "import foo.bar" 3941 a = "from . import foo.bar" 3942 self.check_both(b, a) 3943 3944 def test_dotted_import_as(self): 3945 b = "import foo.bar as bang" 3946 a = "from . import foo.bar as bang" 3947 self.check_both(b, a) 3948 3949 def test_prefix(self): 3950 b = """ 3951 # prefix 3952 import foo.bar 3953 """ 3954 a = """ 3955 # prefix 3956 from . import foo.bar 3957 """ 3958 self.check_both(b, a) 3959 3960 3961class Test_set_literal(FixerTestCase): 3962 3963 fixer = "set_literal" 3964 3965 def test_basic(self): 3966 b = """set([1, 2, 3])""" 3967 a = """{1, 2, 3}""" 3968 self.check(b, a) 3969 3970 b = """set((1, 2, 3))""" 3971 a = """{1, 2, 3}""" 3972 self.check(b, a) 3973 3974 b = """set((1,))""" 3975 a = """{1}""" 3976 self.check(b, a) 3977 3978 b = """set([1])""" 3979 self.check(b, a) 3980 3981 b = """set((a, b))""" 3982 a = """{a, b}""" 3983 self.check(b, a) 3984 3985 b = """set([a, b])""" 3986 self.check(b, a) 3987 3988 b = """set((a*234, f(args=23)))""" 3989 a = """{a*234, f(args=23)}""" 3990 self.check(b, a) 3991 3992 b = """set([a*23, f(23)])""" 3993 a = """{a*23, f(23)}""" 3994 self.check(b, a) 3995 3996 b = """set([a-234**23])""" 3997 a = """{a-234**23}""" 3998 self.check(b, a) 3999 4000 def test_listcomps(self): 4001 b = """set([x for x in y])""" 4002 a = """{x for x in y}""" 4003 self.check(b, a) 4004 4005 b = """set([x for x in y if x == m])""" 4006 a = """{x for x in y if x == m}""" 4007 self.check(b, a) 4008 4009 b = """set([x for x in y for a in b])""" 4010 a = """{x for x in y for a in b}""" 4011 self.check(b, a) 4012 4013 b = """set([f(x) - 23 for x in y])""" 4014 a = """{f(x) - 23 for x in y}""" 4015 self.check(b, a) 4016 4017 def test_whitespace(self): 4018 b = """set( [1, 2])""" 4019 a = """{1, 2}""" 4020 self.check(b, a) 4021 4022 b = """set([1 , 2])""" 4023 a = """{1 , 2}""" 4024 self.check(b, a) 4025 4026 b = """set([ 1 ])""" 4027 a = """{ 1 }""" 4028 self.check(b, a) 4029 4030 b = """set( [1] )""" 4031 a = """{1}""" 4032 self.check(b, a) 4033 4034 b = """set([ 1, 2 ])""" 4035 a = """{ 1, 2 }""" 4036 self.check(b, a) 4037 4038 b = """set([x for x in y ])""" 4039 a = """{x for x in y }""" 4040 self.check(b, a) 4041 4042 b = """set( 4043 [1, 2] 4044 ) 4045 """ 4046 a = """{1, 2}\n""" 4047 self.check(b, a) 4048 4049 def test_comments(self): 4050 b = """set((1, 2)) # Hi""" 4051 a = """{1, 2} # Hi""" 4052 self.check(b, a) 4053 4054 # This isn't optimal behavior, but the fixer is optional. 4055 b = """ 4056 # Foo 4057 set( # Bar 4058 (1, 2) 4059 ) 4060 """ 4061 a = """ 4062 # Foo 4063 {1, 2} 4064 """ 4065 self.check(b, a) 4066 4067 def test_unchanged(self): 4068 s = """set()""" 4069 self.unchanged(s) 4070 4071 s = """set(a)""" 4072 self.unchanged(s) 4073 4074 s = """set(a, b, c)""" 4075 self.unchanged(s) 4076 4077 # Don't transform generators because they might have to be lazy. 4078 s = """set(x for x in y)""" 4079 self.unchanged(s) 4080 4081 s = """set(x for x in y if z)""" 4082 self.unchanged(s) 4083 4084 s = """set(a*823-23**2 + f(23))""" 4085 self.unchanged(s) 4086 4087 4088class Test_sys_exc(FixerTestCase): 4089 fixer = "sys_exc" 4090 4091 def test_0(self): 4092 b = "sys.exc_type" 4093 a = "sys.exc_info()[0]" 4094 self.check(b, a) 4095 4096 def test_1(self): 4097 b = "sys.exc_value" 4098 a = "sys.exc_info()[1]" 4099 self.check(b, a) 4100 4101 def test_2(self): 4102 b = "sys.exc_traceback" 4103 a = "sys.exc_info()[2]" 4104 self.check(b, a) 4105 4106 def test_3(self): 4107 b = "sys.exc_type # Foo" 4108 a = "sys.exc_info()[0] # Foo" 4109 self.check(b, a) 4110 4111 def test_4(self): 4112 b = "sys. exc_type" 4113 a = "sys. exc_info()[0]" 4114 self.check(b, a) 4115 4116 def test_5(self): 4117 b = "sys .exc_type" 4118 a = "sys .exc_info()[0]" 4119 self.check(b, a) 4120 4121 4122class Test_paren(FixerTestCase): 4123 fixer = "paren" 4124 4125 def test_0(self): 4126 b = """[i for i in 1, 2 ]""" 4127 a = """[i for i in (1, 2) ]""" 4128 self.check(b, a) 4129 4130 def test_1(self): 4131 b = """[i for i in 1, 2, ]""" 4132 a = """[i for i in (1, 2,) ]""" 4133 self.check(b, a) 4134 4135 def test_2(self): 4136 b = """[i for i in 1, 2 ]""" 4137 a = """[i for i in (1, 2) ]""" 4138 self.check(b, a) 4139 4140 def test_3(self): 4141 b = """[i for i in 1, 2 if i]""" 4142 a = """[i for i in (1, 2) if i]""" 4143 self.check(b, a) 4144 4145 def test_4(self): 4146 b = """[i for i in 1, 2 ]""" 4147 a = """[i for i in (1, 2) ]""" 4148 self.check(b, a) 4149 4150 def test_5(self): 4151 b = """(i for i in 1, 2)""" 4152 a = """(i for i in (1, 2))""" 4153 self.check(b, a) 4154 4155 def test_6(self): 4156 b = """(i for i in 1 ,2 if i)""" 4157 a = """(i for i in (1 ,2) if i)""" 4158 self.check(b, a) 4159 4160 def test_unchanged_0(self): 4161 s = """[i for i in (1, 2)]""" 4162 self.unchanged(s) 4163 4164 def test_unchanged_1(self): 4165 s = """[i for i in foo()]""" 4166 self.unchanged(s) 4167 4168 def test_unchanged_2(self): 4169 s = """[i for i in (1, 2) if nothing]""" 4170 self.unchanged(s) 4171 4172 def test_unchanged_3(self): 4173 s = """(i for i in (1, 2))""" 4174 self.unchanged(s) 4175 4176 def test_unchanged_4(self): 4177 s = """[i for i in m]""" 4178 self.unchanged(s) 4179 4180class Test_metaclass(FixerTestCase): 4181 4182 fixer = 'metaclass' 4183 4184 def test_unchanged(self): 4185 self.unchanged("class X(): pass") 4186 self.unchanged("class X(object): pass") 4187 self.unchanged("class X(object1, object2): pass") 4188 self.unchanged("class X(object1, object2, object3): pass") 4189 self.unchanged("class X(metaclass=Meta): pass") 4190 self.unchanged("class X(b, arg=23, metclass=Meta): pass") 4191 self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass") 4192 4193 s = """ 4194 class X: 4195 def __metaclass__(self): pass 4196 """ 4197 self.unchanged(s) 4198 4199 s = """ 4200 class X: 4201 a[23] = 74 4202 """ 4203 self.unchanged(s) 4204 4205 def test_comments(self): 4206 b = """ 4207 class X: 4208 # hi 4209 __metaclass__ = AppleMeta 4210 """ 4211 a = """ 4212 class X(metaclass=AppleMeta): 4213 # hi 4214 pass 4215 """ 4216 self.check(b, a) 4217 4218 b = """ 4219 class X: 4220 __metaclass__ = Meta 4221 # Bedtime! 4222 """ 4223 a = """ 4224 class X(metaclass=Meta): 4225 pass 4226 # Bedtime! 4227 """ 4228 self.check(b, a) 4229 4230 def test_meta(self): 4231 # no-parent class, odd body 4232 b = """ 4233 class X(): 4234 __metaclass__ = Q 4235 pass 4236 """ 4237 a = """ 4238 class X(metaclass=Q): 4239 pass 4240 """ 4241 self.check(b, a) 4242 4243 # one parent class, no body 4244 b = """class X(object): __metaclass__ = Q""" 4245 a = """class X(object, metaclass=Q): pass""" 4246 self.check(b, a) 4247 4248 4249 # one parent, simple body 4250 b = """ 4251 class X(object): 4252 __metaclass__ = Meta 4253 bar = 7 4254 """ 4255 a = """ 4256 class X(object, metaclass=Meta): 4257 bar = 7 4258 """ 4259 self.check(b, a) 4260 4261 b = """ 4262 class X: 4263 __metaclass__ = Meta; x = 4; g = 23 4264 """ 4265 a = """ 4266 class X(metaclass=Meta): 4267 x = 4; g = 23 4268 """ 4269 self.check(b, a) 4270 4271 # one parent, simple body, __metaclass__ last 4272 b = """ 4273 class X(object): 4274 bar = 7 4275 __metaclass__ = Meta 4276 """ 4277 a = """ 4278 class X(object, metaclass=Meta): 4279 bar = 7 4280 """ 4281 self.check(b, a) 4282 4283 # redefining __metaclass__ 4284 b = """ 4285 class X(): 4286 __metaclass__ = A 4287 __metaclass__ = B 4288 bar = 7 4289 """ 4290 a = """ 4291 class X(metaclass=B): 4292 bar = 7 4293 """ 4294 self.check(b, a) 4295 4296 # multiple inheritance, simple body 4297 b = """ 4298 class X(clsA, clsB): 4299 __metaclass__ = Meta 4300 bar = 7 4301 """ 4302 a = """ 4303 class X(clsA, clsB, metaclass=Meta): 4304 bar = 7 4305 """ 4306 self.check(b, a) 4307 4308 # keywords in the class statement 4309 b = """class m(a, arg=23): __metaclass__ = Meta""" 4310 a = """class m(a, arg=23, metaclass=Meta): pass""" 4311 self.check(b, a) 4312 4313 b = """ 4314 class X(expression(2 + 4)): 4315 __metaclass__ = Meta 4316 """ 4317 a = """ 4318 class X(expression(2 + 4), metaclass=Meta): 4319 pass 4320 """ 4321 self.check(b, a) 4322 4323 b = """ 4324 class X(expression(2 + 4), x**4): 4325 __metaclass__ = Meta 4326 """ 4327 a = """ 4328 class X(expression(2 + 4), x**4, metaclass=Meta): 4329 pass 4330 """ 4331 self.check(b, a) 4332 4333 b = """ 4334 class X: 4335 __metaclass__ = Meta 4336 save.py = 23 4337 """ 4338 a = """ 4339 class X(metaclass=Meta): 4340 save.py = 23 4341 """ 4342 self.check(b, a) 4343 4344 4345class Test_getcwdu(FixerTestCase): 4346 4347 fixer = 'getcwdu' 4348 4349 def test_basic(self): 4350 b = """os.getcwdu""" 4351 a = """os.getcwd""" 4352 self.check(b, a) 4353 4354 b = """os.getcwdu()""" 4355 a = """os.getcwd()""" 4356 self.check(b, a) 4357 4358 b = """meth = os.getcwdu""" 4359 a = """meth = os.getcwd""" 4360 self.check(b, a) 4361 4362 b = """os.getcwdu(args)""" 4363 a = """os.getcwd(args)""" 4364 self.check(b, a) 4365 4366 def test_comment(self): 4367 b = """os.getcwdu() # Foo""" 4368 a = """os.getcwd() # Foo""" 4369 self.check(b, a) 4370 4371 def test_unchanged(self): 4372 s = """os.getcwd()""" 4373 self.unchanged(s) 4374 4375 s = """getcwdu()""" 4376 self.unchanged(s) 4377 4378 s = """os.getcwdb()""" 4379 self.unchanged(s) 4380 4381 def test_indentation(self): 4382 b = """ 4383 if 1: 4384 os.getcwdu() 4385 """ 4386 a = """ 4387 if 1: 4388 os.getcwd() 4389 """ 4390 self.check(b, a) 4391 4392 def test_multilation(self): 4393 b = """os .getcwdu()""" 4394 a = """os .getcwd()""" 4395 self.check(b, a) 4396 4397 b = """os. getcwdu""" 4398 a = """os. getcwd""" 4399 self.check(b, a) 4400 4401 b = """os.getcwdu ( )""" 4402 a = """os.getcwd ( )""" 4403 self.check(b, a) 4404 4405 4406class Test_operator(FixerTestCase): 4407 4408 fixer = "operator" 4409 4410 def test_operator_isCallable(self): 4411 b = "operator.isCallable(x)" 4412 a = "callable(x)" 4413 self.check(b, a) 4414 4415 def test_operator_sequenceIncludes(self): 4416 b = "operator.sequenceIncludes(x, y)" 4417 a = "operator.contains(x, y)" 4418 self.check(b, a) 4419 4420 b = "operator .sequenceIncludes(x, y)" 4421 a = "operator .contains(x, y)" 4422 self.check(b, a) 4423 4424 b = "operator. sequenceIncludes(x, y)" 4425 a = "operator. contains(x, y)" 4426 self.check(b, a) 4427 4428 def test_operator_isSequenceType(self): 4429 b = "operator.isSequenceType(x)" 4430 a = "import collections.abc\nisinstance(x, collections.abc.Sequence)" 4431 self.check(b, a) 4432 4433 def test_operator_isMappingType(self): 4434 b = "operator.isMappingType(x)" 4435 a = "import collections.abc\nisinstance(x, collections.abc.Mapping)" 4436 self.check(b, a) 4437 4438 def test_operator_isNumberType(self): 4439 b = "operator.isNumberType(x)" 4440 a = "import numbers\nisinstance(x, numbers.Number)" 4441 self.check(b, a) 4442 4443 def test_operator_repeat(self): 4444 b = "operator.repeat(x, n)" 4445 a = "operator.mul(x, n)" 4446 self.check(b, a) 4447 4448 b = "operator .repeat(x, n)" 4449 a = "operator .mul(x, n)" 4450 self.check(b, a) 4451 4452 b = "operator. repeat(x, n)" 4453 a = "operator. mul(x, n)" 4454 self.check(b, a) 4455 4456 def test_operator_irepeat(self): 4457 b = "operator.irepeat(x, n)" 4458 a = "operator.imul(x, n)" 4459 self.check(b, a) 4460 4461 b = "operator .irepeat(x, n)" 4462 a = "operator .imul(x, n)" 4463 self.check(b, a) 4464 4465 b = "operator. irepeat(x, n)" 4466 a = "operator. imul(x, n)" 4467 self.check(b, a) 4468 4469 def test_bare_isCallable(self): 4470 s = "isCallable(x)" 4471 t = "You should use 'callable(x)' here." 4472 self.warns_unchanged(s, t) 4473 4474 def test_bare_sequenceIncludes(self): 4475 s = "sequenceIncludes(x, y)" 4476 t = "You should use 'operator.contains(x, y)' here." 4477 self.warns_unchanged(s, t) 4478 4479 def test_bare_operator_isSequenceType(self): 4480 s = "isSequenceType(z)" 4481 t = "You should use 'isinstance(z, collections.abc.Sequence)' here." 4482 self.warns_unchanged(s, t) 4483 4484 def test_bare_operator_isMappingType(self): 4485 s = "isMappingType(x)" 4486 t = "You should use 'isinstance(x, collections.abc.Mapping)' here." 4487 self.warns_unchanged(s, t) 4488 4489 def test_bare_operator_isNumberType(self): 4490 s = "isNumberType(y)" 4491 t = "You should use 'isinstance(y, numbers.Number)' here." 4492 self.warns_unchanged(s, t) 4493 4494 def test_bare_operator_repeat(self): 4495 s = "repeat(x, n)" 4496 t = "You should use 'operator.mul(x, n)' here." 4497 self.warns_unchanged(s, t) 4498 4499 def test_bare_operator_irepeat(self): 4500 s = "irepeat(y, 187)" 4501 t = "You should use 'operator.imul(y, 187)' here." 4502 self.warns_unchanged(s, t) 4503 4504 4505class Test_exitfunc(FixerTestCase): 4506 4507 fixer = "exitfunc" 4508 4509 def test_simple(self): 4510 b = """ 4511 import sys 4512 sys.exitfunc = my_atexit 4513 """ 4514 a = """ 4515 import sys 4516 import atexit 4517 atexit.register(my_atexit) 4518 """ 4519 self.check(b, a) 4520 4521 def test_names_import(self): 4522 b = """ 4523 import sys, crumbs 4524 sys.exitfunc = my_func 4525 """ 4526 a = """ 4527 import sys, crumbs, atexit 4528 atexit.register(my_func) 4529 """ 4530 self.check(b, a) 4531 4532 def test_complex_expression(self): 4533 b = """ 4534 import sys 4535 sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression 4536 """ 4537 a = """ 4538 import sys 4539 import atexit 4540 atexit.register(do(d)/a()+complex(f=23, g=23)*expression) 4541 """ 4542 self.check(b, a) 4543 4544 def test_comments(self): 4545 b = """ 4546 import sys # Foo 4547 sys.exitfunc = f # Blah 4548 """ 4549 a = """ 4550 import sys 4551 import atexit # Foo 4552 atexit.register(f) # Blah 4553 """ 4554 self.check(b, a) 4555 4556 b = """ 4557 import apples, sys, crumbs, larry # Pleasant comments 4558 sys.exitfunc = func 4559 """ 4560 a = """ 4561 import apples, sys, crumbs, larry, atexit # Pleasant comments 4562 atexit.register(func) 4563 """ 4564 self.check(b, a) 4565 4566 def test_in_a_function(self): 4567 b = """ 4568 import sys 4569 def f(): 4570 sys.exitfunc = func 4571 """ 4572 a = """ 4573 import sys 4574 import atexit 4575 def f(): 4576 atexit.register(func) 4577 """ 4578 self.check(b, a) 4579 4580 def test_no_sys_import(self): 4581 b = """sys.exitfunc = f""" 4582 a = """atexit.register(f)""" 4583 msg = ("Can't find sys import; Please add an atexit import at the " 4584 "top of your file.") 4585 self.warns(b, a, msg) 4586 4587 4588 def test_unchanged(self): 4589 s = """f(sys.exitfunc)""" 4590 self.unchanged(s) 4591 4592 4593class Test_asserts(FixerTestCase): 4594 4595 fixer = "asserts" 4596 4597 def test_deprecated_names(self): 4598 tests = [ 4599 ('self.assert_(True)', 'self.assertTrue(True)'), 4600 ('self.assertEquals(2, 2)', 'self.assertEqual(2, 2)'), 4601 ('self.assertNotEquals(2, 3)', 'self.assertNotEqual(2, 3)'), 4602 ('self.assertAlmostEquals(2, 3)', 'self.assertAlmostEqual(2, 3)'), 4603 ('self.assertNotAlmostEquals(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), 4604 ('self.failUnlessEqual(2, 2)', 'self.assertEqual(2, 2)'), 4605 ('self.failIfEqual(2, 3)', 'self.assertNotEqual(2, 3)'), 4606 ('self.failUnlessAlmostEqual(2, 3)', 'self.assertAlmostEqual(2, 3)'), 4607 ('self.failIfAlmostEqual(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), 4608 ('self.failUnless(True)', 'self.assertTrue(True)'), 4609 ('self.failUnlessRaises(foo)', 'self.assertRaises(foo)'), 4610 ('self.failIf(False)', 'self.assertFalse(False)'), 4611 ] 4612 for b, a in tests: 4613 self.check(b, a) 4614 4615 def test_variants(self): 4616 b = 'eq = self.assertEquals' 4617 a = 'eq = self.assertEqual' 4618 self.check(b, a) 4619 b = 'self.assertEquals(2, 3, msg="fail")' 4620 a = 'self.assertEqual(2, 3, msg="fail")' 4621 self.check(b, a) 4622 b = 'self.assertEquals(2, 3, msg="fail") # foo' 4623 a = 'self.assertEqual(2, 3, msg="fail") # foo' 4624 self.check(b, a) 4625 b = 'self.assertEquals (2, 3)' 4626 a = 'self.assertEqual (2, 3)' 4627 self.check(b, a) 4628 b = ' self.assertEquals (2, 3)' 4629 a = ' self.assertEqual (2, 3)' 4630 self.check(b, a) 4631 b = 'with self.failUnlessRaises(Explosion): explode()' 4632 a = 'with self.assertRaises(Explosion): explode()' 4633 self.check(b, a) 4634 b = 'with self.failUnlessRaises(Explosion) as cm: explode()' 4635 a = 'with self.assertRaises(Explosion) as cm: explode()' 4636 self.check(b, a) 4637 4638 def test_unchanged(self): 4639 self.unchanged('self.assertEqualsOnSaturday') 4640 self.unchanged('self.assertEqualsOnSaturday(3, 5)') 4641