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 # bpo-38871 2958 b = """filter(lambda x: True if x > 2 else False, [1, 2, 3])""" 2959 a = """[x for x in [1, 2, 3] if (True if x > 2 else False)]""" 2960 self.check(b, a) 2961 2962 def test_filter_trailers(self): 2963 b = """x = filter(None, 'abc')[0]""" 2964 a = """x = [_f for _f in 'abc' if _f][0]""" 2965 self.check(b, a) 2966 2967 b = """x = len(filter(f, 'abc')[0])""" 2968 a = """x = len(list(filter(f, 'abc'))[0])""" 2969 self.check(b, a) 2970 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 # Note the parens around x 2976 b = """x = filter(lambda (x): x%2 == 0, range(10))[0]""" 2977 a = """x = [x for x in range(10) if x%2 == 0][0]""" 2978 self.check(b, a) 2979 2980 def test_filter_nochange(self): 2981 a = """b.join(filter(f, 'abc'))""" 2982 self.unchanged(a) 2983 a = """(a + foo(5)).join(filter(f, 'abc'))""" 2984 self.unchanged(a) 2985 a = """iter(filter(f, 'abc'))""" 2986 self.unchanged(a) 2987 a = """list(filter(f, 'abc'))""" 2988 self.unchanged(a) 2989 a = """list(filter(f, 'abc'))[0]""" 2990 self.unchanged(a) 2991 a = """set(filter(f, 'abc'))""" 2992 self.unchanged(a) 2993 a = """set(filter(f, 'abc')).pop()""" 2994 self.unchanged(a) 2995 a = """tuple(filter(f, 'abc'))""" 2996 self.unchanged(a) 2997 a = """any(filter(f, 'abc'))""" 2998 self.unchanged(a) 2999 a = """all(filter(f, 'abc'))""" 3000 self.unchanged(a) 3001 a = """sum(filter(f, 'abc'))""" 3002 self.unchanged(a) 3003 a = """sorted(filter(f, 'abc'))""" 3004 self.unchanged(a) 3005 a = """sorted(filter(f, 'abc'), key=blah)""" 3006 self.unchanged(a) 3007 a = """sorted(filter(f, 'abc'), key=blah)[0]""" 3008 self.unchanged(a) 3009 a = """enumerate(filter(f, 'abc'))""" 3010 self.unchanged(a) 3011 a = """enumerate(filter(f, 'abc'), start=1)""" 3012 self.unchanged(a) 3013 a = """for i in filter(f, 'abc'): pass""" 3014 self.unchanged(a) 3015 a = """[x for x in filter(f, 'abc')]""" 3016 self.unchanged(a) 3017 a = """(x for x in filter(f, 'abc'))""" 3018 self.unchanged(a) 3019 3020 def test_future_builtins(self): 3021 a = "from future_builtins import spam, filter; filter(f, 'ham')" 3022 self.unchanged(a) 3023 3024 b = """from future_builtins import spam; x = filter(f, 'abc')""" 3025 a = """from future_builtins import spam; x = list(filter(f, 'abc'))""" 3026 self.check(b, a) 3027 3028 a = "from future_builtins import *; filter(f, 'ham')" 3029 self.unchanged(a) 3030 3031class Test_map(FixerTestCase): 3032 fixer = "map" 3033 3034 def check(self, b, a): 3035 self.unchanged("from future_builtins import map; " + b, a) 3036 super(Test_map, self).check(b, a) 3037 3038 def test_prefix_preservation(self): 3039 b = """x = map( f, 'abc' )""" 3040 a = """x = list(map( f, 'abc' ))""" 3041 self.check(b, a) 3042 3043 def test_map_trailers(self): 3044 b = """x = map(f, 'abc')[0]""" 3045 a = """x = list(map(f, 'abc'))[0]""" 3046 self.check(b, a) 3047 3048 b = """x = map(None, l)[0]""" 3049 a = """x = list(l)[0]""" 3050 self.check(b, a) 3051 3052 b = """x = map(lambda x:x, l)[0]""" 3053 a = """x = [x for x in l][0]""" 3054 self.check(b, a) 3055 3056 b = """x = map(f, 'abc')[0][1]""" 3057 a = """x = list(map(f, 'abc'))[0][1]""" 3058 self.check(b, a) 3059 3060 def test_trailing_comment(self): 3061 b = """x = map(f, 'abc') # foo""" 3062 a = """x = list(map(f, 'abc')) # foo""" 3063 self.check(b, a) 3064 3065 def test_None_with_multiple_arguments(self): 3066 s = """x = map(None, a, b, c)""" 3067 self.warns_unchanged(s, "cannot convert map(None, ...) with " 3068 "multiple arguments") 3069 3070 def test_map_basic(self): 3071 b = """x = map(f, 'abc')""" 3072 a = """x = list(map(f, 'abc'))""" 3073 self.check(b, a) 3074 3075 b = """x = len(map(f, 'abc', 'def'))""" 3076 a = """x = len(list(map(f, 'abc', 'def')))""" 3077 self.check(b, a) 3078 3079 b = """x = map(None, 'abc')""" 3080 a = """x = list('abc')""" 3081 self.check(b, a) 3082 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 # Note the parens around x 3088 b = """x = map(lambda (x): x+1, range(4))""" 3089 a = """x = [x+1 for x in range(4)]""" 3090 self.check(b, a) 3091 3092 b = """ 3093 foo() 3094 # foo 3095 map(f, x) 3096 """ 3097 a = """ 3098 foo() 3099 # foo 3100 list(map(f, x)) 3101 """ 3102 self.warns(b, a, "You should use a for loop here") 3103 3104 def test_map_nochange(self): 3105 a = """b.join(map(f, 'abc'))""" 3106 self.unchanged(a) 3107 a = """(a + foo(5)).join(map(f, 'abc'))""" 3108 self.unchanged(a) 3109 a = """iter(map(f, 'abc'))""" 3110 self.unchanged(a) 3111 a = """list(map(f, 'abc'))""" 3112 self.unchanged(a) 3113 a = """list(map(f, 'abc'))[0]""" 3114 self.unchanged(a) 3115 a = """set(map(f, 'abc'))""" 3116 self.unchanged(a) 3117 a = """set(map(f, 'abc')).pop()""" 3118 self.unchanged(a) 3119 a = """tuple(map(f, 'abc'))""" 3120 self.unchanged(a) 3121 a = """any(map(f, 'abc'))""" 3122 self.unchanged(a) 3123 a = """all(map(f, 'abc'))""" 3124 self.unchanged(a) 3125 a = """sum(map(f, 'abc'))""" 3126 self.unchanged(a) 3127 a = """sorted(map(f, 'abc'))""" 3128 self.unchanged(a) 3129 a = """sorted(map(f, 'abc'), key=blah)""" 3130 self.unchanged(a) 3131 a = """sorted(map(f, 'abc'), key=blah)[0]""" 3132 self.unchanged(a) 3133 a = """enumerate(map(f, 'abc'))""" 3134 self.unchanged(a) 3135 a = """enumerate(map(f, 'abc'), start=1)""" 3136 self.unchanged(a) 3137 a = """for i in map(f, 'abc'): pass""" 3138 self.unchanged(a) 3139 a = """[x for x in map(f, 'abc')]""" 3140 self.unchanged(a) 3141 a = """(x for x in map(f, 'abc'))""" 3142 self.unchanged(a) 3143 3144 def test_future_builtins(self): 3145 a = "from future_builtins import spam, map, eggs; map(f, 'ham')" 3146 self.unchanged(a) 3147 3148 b = """from future_builtins import spam, eggs; x = map(f, 'abc')""" 3149 a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))""" 3150 self.check(b, a) 3151 3152 a = "from future_builtins import *; map(f, 'ham')" 3153 self.unchanged(a) 3154 3155class Test_zip(FixerTestCase): 3156 fixer = "zip" 3157 3158 def check(self, b, a): 3159 self.unchanged("from future_builtins import zip; " + b, a) 3160 super(Test_zip, self).check(b, a) 3161 3162 def test_zip_basic(self): 3163 b = """x = zip()""" 3164 a = """x = list(zip())""" 3165 self.check(b, a) 3166 3167 b = """x = zip(a, b, c)""" 3168 a = """x = list(zip(a, b, c))""" 3169 self.check(b, a) 3170 3171 b = """x = len(zip(a, b))""" 3172 a = """x = len(list(zip(a, b)))""" 3173 self.check(b, a) 3174 3175 def test_zip_trailers(self): 3176 b = """x = zip(a, b, c)[0]""" 3177 a = """x = list(zip(a, b, c))[0]""" 3178 self.check(b, a) 3179 3180 b = """x = zip(a, b, c)[0][1]""" 3181 a = """x = list(zip(a, b, c))[0][1]""" 3182 self.check(b, a) 3183 3184 def test_zip_nochange(self): 3185 a = """b.join(zip(a, b))""" 3186 self.unchanged(a) 3187 a = """(a + foo(5)).join(zip(a, b))""" 3188 self.unchanged(a) 3189 a = """iter(zip(a, b))""" 3190 self.unchanged(a) 3191 a = """list(zip(a, b))""" 3192 self.unchanged(a) 3193 a = """list(zip(a, b))[0]""" 3194 self.unchanged(a) 3195 a = """set(zip(a, b))""" 3196 self.unchanged(a) 3197 a = """set(zip(a, b)).pop()""" 3198 self.unchanged(a) 3199 a = """tuple(zip(a, b))""" 3200 self.unchanged(a) 3201 a = """any(zip(a, b))""" 3202 self.unchanged(a) 3203 a = """all(zip(a, b))""" 3204 self.unchanged(a) 3205 a = """sum(zip(a, b))""" 3206 self.unchanged(a) 3207 a = """sorted(zip(a, b))""" 3208 self.unchanged(a) 3209 a = """sorted(zip(a, b), key=blah)""" 3210 self.unchanged(a) 3211 a = """sorted(zip(a, b), key=blah)[0]""" 3212 self.unchanged(a) 3213 a = """enumerate(zip(a, b))""" 3214 self.unchanged(a) 3215 a = """enumerate(zip(a, b), start=1)""" 3216 self.unchanged(a) 3217 a = """for i in zip(a, b): pass""" 3218 self.unchanged(a) 3219 a = """[x for x in zip(a, b)]""" 3220 self.unchanged(a) 3221 a = """(x for x in zip(a, b))""" 3222 self.unchanged(a) 3223 3224 def test_future_builtins(self): 3225 a = "from future_builtins import spam, zip, eggs; zip(a, b)" 3226 self.unchanged(a) 3227 3228 b = """from future_builtins import spam, eggs; x = zip(a, b)""" 3229 a = """from future_builtins import spam, eggs; x = list(zip(a, b))""" 3230 self.check(b, a) 3231 3232 a = "from future_builtins import *; zip(a, b)" 3233 self.unchanged(a) 3234 3235class Test_standarderror(FixerTestCase): 3236 fixer = "standarderror" 3237 3238 def test(self): 3239 b = """x = StandardError()""" 3240 a = """x = Exception()""" 3241 self.check(b, a) 3242 3243 b = """x = StandardError(a, b, c)""" 3244 a = """x = Exception(a, b, c)""" 3245 self.check(b, a) 3246 3247 b = """f(2 + StandardError(a, b, c))""" 3248 a = """f(2 + Exception(a, b, c))""" 3249 self.check(b, a) 3250 3251class Test_types(FixerTestCase): 3252 fixer = "types" 3253 3254 def test_basic_types_convert(self): 3255 b = """types.StringType""" 3256 a = """bytes""" 3257 self.check(b, a) 3258 3259 b = """types.DictType""" 3260 a = """dict""" 3261 self.check(b, a) 3262 3263 b = """types . IntType""" 3264 a = """int""" 3265 self.check(b, a) 3266 3267 b = """types.ListType""" 3268 a = """list""" 3269 self.check(b, a) 3270 3271 b = """types.LongType""" 3272 a = """int""" 3273 self.check(b, a) 3274 3275 b = """types.NoneType""" 3276 a = """type(None)""" 3277 self.check(b, a) 3278 3279 b = "types.StringTypes" 3280 a = "(str,)" 3281 self.check(b, a) 3282 3283class Test_idioms(FixerTestCase): 3284 fixer = "idioms" 3285 3286 def test_while(self): 3287 b = """while 1: foo()""" 3288 a = """while True: foo()""" 3289 self.check(b, a) 3290 3291 b = """while 1: foo()""" 3292 a = """while True: foo()""" 3293 self.check(b, a) 3294 3295 b = """ 3296 while 1: 3297 foo() 3298 """ 3299 a = """ 3300 while True: 3301 foo() 3302 """ 3303 self.check(b, a) 3304 3305 def test_while_unchanged(self): 3306 s = """while 11: foo()""" 3307 self.unchanged(s) 3308 3309 s = """while 0: foo()""" 3310 self.unchanged(s) 3311 3312 s = """while foo(): foo()""" 3313 self.unchanged(s) 3314 3315 s = """while []: foo()""" 3316 self.unchanged(s) 3317 3318 def test_eq_simple(self): 3319 b = """type(x) == T""" 3320 a = """isinstance(x, T)""" 3321 self.check(b, a) 3322 3323 b = """if type(x) == T: pass""" 3324 a = """if isinstance(x, T): pass""" 3325 self.check(b, a) 3326 3327 def test_eq_reverse(self): 3328 b = """T == type(x)""" 3329 a = """isinstance(x, T)""" 3330 self.check(b, a) 3331 3332 b = """if T == type(x): pass""" 3333 a = """if isinstance(x, T): pass""" 3334 self.check(b, a) 3335 3336 def test_eq_expression(self): 3337 b = """type(x+y) == d.get('T')""" 3338 a = """isinstance(x+y, d.get('T'))""" 3339 self.check(b, a) 3340 3341 b = """type( x + y) == d.get('T')""" 3342 a = """isinstance(x + y, d.get('T'))""" 3343 self.check(b, a) 3344 3345 def test_is_simple(self): 3346 b = """type(x) is T""" 3347 a = """isinstance(x, T)""" 3348 self.check(b, a) 3349 3350 b = """if type(x) is T: pass""" 3351 a = """if isinstance(x, T): pass""" 3352 self.check(b, a) 3353 3354 def test_is_reverse(self): 3355 b = """T is type(x)""" 3356 a = """isinstance(x, T)""" 3357 self.check(b, a) 3358 3359 b = """if T is type(x): pass""" 3360 a = """if isinstance(x, T): pass""" 3361 self.check(b, a) 3362 3363 def test_is_expression(self): 3364 b = """type(x+y) is d.get('T')""" 3365 a = """isinstance(x+y, d.get('T'))""" 3366 self.check(b, a) 3367 3368 b = """type( x + y) is d.get('T')""" 3369 a = """isinstance(x + y, d.get('T'))""" 3370 self.check(b, a) 3371 3372 def test_is_not_simple(self): 3373 b = """type(x) is not T""" 3374 a = """not isinstance(x, T)""" 3375 self.check(b, a) 3376 3377 b = """if type(x) is not T: pass""" 3378 a = """if not isinstance(x, T): pass""" 3379 self.check(b, a) 3380 3381 def test_is_not_reverse(self): 3382 b = """T is not type(x)""" 3383 a = """not isinstance(x, T)""" 3384 self.check(b, a) 3385 3386 b = """if T is not type(x): pass""" 3387 a = """if not isinstance(x, T): pass""" 3388 self.check(b, a) 3389 3390 def test_is_not_expression(self): 3391 b = """type(x+y) is not d.get('T')""" 3392 a = """not isinstance(x+y, d.get('T'))""" 3393 self.check(b, a) 3394 3395 b = """type( x + y) is not d.get('T')""" 3396 a = """not isinstance(x + y, d.get('T'))""" 3397 self.check(b, a) 3398 3399 def test_ne_simple(self): 3400 b = """type(x) != T""" 3401 a = """not isinstance(x, T)""" 3402 self.check(b, a) 3403 3404 b = """if type(x) != T: pass""" 3405 a = """if not isinstance(x, T): pass""" 3406 self.check(b, a) 3407 3408 def test_ne_reverse(self): 3409 b = """T != type(x)""" 3410 a = """not isinstance(x, T)""" 3411 self.check(b, a) 3412 3413 b = """if T != type(x): pass""" 3414 a = """if not isinstance(x, T): pass""" 3415 self.check(b, a) 3416 3417 def test_ne_expression(self): 3418 b = """type(x+y) != d.get('T')""" 3419 a = """not isinstance(x+y, d.get('T'))""" 3420 self.check(b, a) 3421 3422 b = """type( x + y) != d.get('T')""" 3423 a = """not isinstance(x + y, d.get('T'))""" 3424 self.check(b, a) 3425 3426 def test_type_unchanged(self): 3427 a = """type(x).__name__""" 3428 self.unchanged(a) 3429 3430 def test_sort_list_call(self): 3431 b = """ 3432 v = list(t) 3433 v.sort() 3434 foo(v) 3435 """ 3436 a = """ 3437 v = sorted(t) 3438 foo(v) 3439 """ 3440 self.check(b, a) 3441 3442 b = """ 3443 v = list(foo(b) + d) 3444 v.sort() 3445 foo(v) 3446 """ 3447 a = """ 3448 v = sorted(foo(b) + d) 3449 foo(v) 3450 """ 3451 self.check(b, a) 3452 3453 b = """ 3454 while x: 3455 v = list(t) 3456 v.sort() 3457 foo(v) 3458 """ 3459 a = """ 3460 while x: 3461 v = sorted(t) 3462 foo(v) 3463 """ 3464 self.check(b, a) 3465 3466 b = """ 3467 v = list(t) 3468 # foo 3469 v.sort() 3470 foo(v) 3471 """ 3472 a = """ 3473 v = sorted(t) 3474 # foo 3475 foo(v) 3476 """ 3477 self.check(b, a) 3478 3479 b = r""" 3480 v = list( t) 3481 v.sort() 3482 foo(v) 3483 """ 3484 a = r""" 3485 v = sorted( t) 3486 foo(v) 3487 """ 3488 self.check(b, a) 3489 3490 b = r""" 3491 try: 3492 m = list(s) 3493 m.sort() 3494 except: pass 3495 """ 3496 3497 a = r""" 3498 try: 3499 m = sorted(s) 3500 except: pass 3501 """ 3502 self.check(b, a) 3503 3504 b = r""" 3505 try: 3506 m = list(s) 3507 # foo 3508 m.sort() 3509 except: pass 3510 """ 3511 3512 a = r""" 3513 try: 3514 m = sorted(s) 3515 # foo 3516 except: pass 3517 """ 3518 self.check(b, a) 3519 3520 b = r""" 3521 m = list(s) 3522 # more comments 3523 m.sort()""" 3524 3525 a = r""" 3526 m = sorted(s) 3527 # more comments""" 3528 self.check(b, a) 3529 3530 def test_sort_simple_expr(self): 3531 b = """ 3532 v = t 3533 v.sort() 3534 foo(v) 3535 """ 3536 a = """ 3537 v = sorted(t) 3538 foo(v) 3539 """ 3540 self.check(b, a) 3541 3542 b = """ 3543 v = foo(b) 3544 v.sort() 3545 foo(v) 3546 """ 3547 a = """ 3548 v = sorted(foo(b)) 3549 foo(v) 3550 """ 3551 self.check(b, a) 3552 3553 b = """ 3554 v = b.keys() 3555 v.sort() 3556 foo(v) 3557 """ 3558 a = """ 3559 v = sorted(b.keys()) 3560 foo(v) 3561 """ 3562 self.check(b, a) 3563 3564 b = """ 3565 v = foo(b) + d 3566 v.sort() 3567 foo(v) 3568 """ 3569 a = """ 3570 v = sorted(foo(b) + d) 3571 foo(v) 3572 """ 3573 self.check(b, a) 3574 3575 b = """ 3576 while x: 3577 v = t 3578 v.sort() 3579 foo(v) 3580 """ 3581 a = """ 3582 while x: 3583 v = sorted(t) 3584 foo(v) 3585 """ 3586 self.check(b, a) 3587 3588 b = """ 3589 v = t 3590 # foo 3591 v.sort() 3592 foo(v) 3593 """ 3594 a = """ 3595 v = sorted(t) 3596 # foo 3597 foo(v) 3598 """ 3599 self.check(b, a) 3600 3601 b = r""" 3602 v = t 3603 v.sort() 3604 foo(v) 3605 """ 3606 a = r""" 3607 v = sorted(t) 3608 foo(v) 3609 """ 3610 self.check(b, a) 3611 3612 def test_sort_unchanged(self): 3613 s = """ 3614 v = list(t) 3615 w.sort() 3616 foo(w) 3617 """ 3618 self.unchanged(s) 3619 3620 s = """ 3621 v = list(t) 3622 v.sort(u) 3623 foo(v) 3624 """ 3625 self.unchanged(s) 3626 3627class Test_basestring(FixerTestCase): 3628 fixer = "basestring" 3629 3630 def test_basestring(self): 3631 b = """isinstance(x, basestring)""" 3632 a = """isinstance(x, str)""" 3633 self.check(b, a) 3634 3635class Test_buffer(FixerTestCase): 3636 fixer = "buffer" 3637 3638 def test_buffer(self): 3639 b = """x = buffer(y)""" 3640 a = """x = memoryview(y)""" 3641 self.check(b, a) 3642 3643 def test_slicing(self): 3644 b = """buffer(y)[4:5]""" 3645 a = """memoryview(y)[4:5]""" 3646 self.check(b, a) 3647 3648class Test_future(FixerTestCase): 3649 fixer = "future" 3650 3651 def test_future(self): 3652 b = """from __future__ import braces""" 3653 a = """""" 3654 self.check(b, a) 3655 3656 b = """# comment\nfrom __future__ import braces""" 3657 a = """# comment\n""" 3658 self.check(b, a) 3659 3660 b = """from __future__ import braces\n# comment""" 3661 a = """\n# comment""" 3662 self.check(b, a) 3663 3664 def test_run_order(self): 3665 self.assert_runs_after('print') 3666 3667class Test_itertools(FixerTestCase): 3668 fixer = "itertools" 3669 3670 def checkall(self, before, after): 3671 # Because we need to check with and without the itertools prefix 3672 # and on each of the three functions, these loops make it all 3673 # much easier 3674 for i in ('itertools.', ''): 3675 for f in ('map', 'filter', 'zip'): 3676 b = before %(i+'i'+f) 3677 a = after %(f) 3678 self.check(b, a) 3679 3680 def test_0(self): 3681 # A simple example -- test_1 covers exactly the same thing, 3682 # but it's not quite as clear. 3683 b = "itertools.izip(a, b)" 3684 a = "zip(a, b)" 3685 self.check(b, a) 3686 3687 def test_1(self): 3688 b = """%s(f, a)""" 3689 a = """%s(f, a)""" 3690 self.checkall(b, a) 3691 3692 def test_qualified(self): 3693 b = """itertools.ifilterfalse(a, b)""" 3694 a = """itertools.filterfalse(a, b)""" 3695 self.check(b, a) 3696 3697 b = """itertools.izip_longest(a, b)""" 3698 a = """itertools.zip_longest(a, b)""" 3699 self.check(b, a) 3700 3701 def test_2(self): 3702 b = """ifilterfalse(a, b)""" 3703 a = """filterfalse(a, b)""" 3704 self.check(b, a) 3705 3706 b = """izip_longest(a, b)""" 3707 a = """zip_longest(a, b)""" 3708 self.check(b, a) 3709 3710 def test_space_1(self): 3711 b = """ %s(f, a)""" 3712 a = """ %s(f, a)""" 3713 self.checkall(b, a) 3714 3715 def test_space_2(self): 3716 b = """ itertools.ifilterfalse(a, b)""" 3717 a = """ itertools.filterfalse(a, b)""" 3718 self.check(b, a) 3719 3720 b = """ itertools.izip_longest(a, b)""" 3721 a = """ itertools.zip_longest(a, b)""" 3722 self.check(b, a) 3723 3724 def test_run_order(self): 3725 self.assert_runs_after('map', 'zip', 'filter') 3726 3727 3728class Test_itertools_imports(FixerTestCase): 3729 fixer = 'itertools_imports' 3730 3731 def test_reduced(self): 3732 b = "from itertools import imap, izip, foo" 3733 a = "from itertools import foo" 3734 self.check(b, a) 3735 3736 b = "from itertools import bar, imap, izip, foo" 3737 a = "from itertools import bar, foo" 3738 self.check(b, a) 3739 3740 b = "from itertools import chain, imap, izip" 3741 a = "from itertools import chain" 3742 self.check(b, a) 3743 3744 def test_comments(self): 3745 b = "#foo\nfrom itertools import imap, izip" 3746 a = "#foo\n" 3747 self.check(b, a) 3748 3749 def test_none(self): 3750 b = "from itertools import imap, izip" 3751 a = "" 3752 self.check(b, a) 3753 3754 b = "from itertools import izip" 3755 a = "" 3756 self.check(b, a) 3757 3758 def test_import_as(self): 3759 b = "from itertools import izip, bar as bang, imap" 3760 a = "from itertools import bar as bang" 3761 self.check(b, a) 3762 3763 b = "from itertools import izip as _zip, imap, bar" 3764 a = "from itertools import bar" 3765 self.check(b, a) 3766 3767 b = "from itertools import imap as _map" 3768 a = "" 3769 self.check(b, a) 3770 3771 b = "from itertools import imap as _map, izip as _zip" 3772 a = "" 3773 self.check(b, a) 3774 3775 s = "from itertools import bar as bang" 3776 self.unchanged(s) 3777 3778 def test_ifilter_and_zip_longest(self): 3779 for name in "filterfalse", "zip_longest": 3780 b = "from itertools import i%s" % (name,) 3781 a = "from itertools import %s" % (name,) 3782 self.check(b, a) 3783 3784 b = "from itertools import imap, i%s, foo" % (name,) 3785 a = "from itertools import %s, foo" % (name,) 3786 self.check(b, a) 3787 3788 b = "from itertools import bar, i%s, foo" % (name,) 3789 a = "from itertools import bar, %s, foo" % (name,) 3790 self.check(b, a) 3791 3792 def test_import_star(self): 3793 s = "from itertools import *" 3794 self.unchanged(s) 3795 3796 3797 def test_unchanged(self): 3798 s = "from itertools import foo" 3799 self.unchanged(s) 3800 3801 3802class Test_import(FixerTestCase): 3803 fixer = "import" 3804 3805 def setUp(self): 3806 super(Test_import, self).setUp() 3807 # Need to replace fix_import's exists method 3808 # so we can check that it's doing the right thing 3809 self.files_checked = [] 3810 self.present_files = set() 3811 self.always_exists = True 3812 def fake_exists(name): 3813 self.files_checked.append(name) 3814 return self.always_exists or (name in self.present_files) 3815 3816 from lib2to3.fixes import fix_import 3817 fix_import.exists = fake_exists 3818 3819 def tearDown(self): 3820 from lib2to3.fixes import fix_import 3821 fix_import.exists = os.path.exists 3822 3823 def check_both(self, b, a): 3824 self.always_exists = True 3825 super(Test_import, self).check(b, a) 3826 self.always_exists = False 3827 super(Test_import, self).unchanged(b) 3828 3829 def test_files_checked(self): 3830 def p(path): 3831 # Takes a unix path and returns a path with correct separators 3832 return os.path.pathsep.join(path.split("/")) 3833 3834 self.always_exists = False 3835 self.present_files = set(['__init__.py']) 3836 expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd') 3837 names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py")) 3838 3839 for name in names_to_test: 3840 self.files_checked = [] 3841 self.filename = name 3842 self.unchanged("import jam") 3843 3844 if os.path.dirname(name): 3845 name = os.path.dirname(name) + '/jam' 3846 else: 3847 name = 'jam' 3848 expected_checks = set(name + ext for ext in expected_extensions) 3849 expected_checks.add("__init__.py") 3850 3851 self.assertEqual(set(self.files_checked), expected_checks) 3852 3853 def test_not_in_package(self): 3854 s = "import bar" 3855 self.always_exists = False 3856 self.present_files = set(["bar.py"]) 3857 self.unchanged(s) 3858 3859 def test_with_absolute_import_enabled(self): 3860 s = "from __future__ import absolute_import\nimport bar" 3861 self.always_exists = False 3862 self.present_files = set(["__init__.py", "bar.py"]) 3863 self.unchanged(s) 3864 3865 def test_in_package(self): 3866 b = "import bar" 3867 a = "from . import bar" 3868 self.always_exists = False 3869 self.present_files = set(["__init__.py", "bar.py"]) 3870 self.check(b, a) 3871 3872 def test_import_from_package(self): 3873 b = "import bar" 3874 a = "from . import bar" 3875 self.always_exists = False 3876 self.present_files = set(["__init__.py", "bar" + os.path.sep]) 3877 self.check(b, a) 3878 3879 def test_already_relative_import(self): 3880 s = "from . import bar" 3881 self.unchanged(s) 3882 3883 def test_comments_and_indent(self): 3884 b = "import bar # Foo" 3885 a = "from . import bar # Foo" 3886 self.check(b, a) 3887 3888 def test_from(self): 3889 b = "from foo import bar, baz" 3890 a = "from .foo import bar, baz" 3891 self.check_both(b, a) 3892 3893 b = "from foo import bar" 3894 a = "from .foo import bar" 3895 self.check_both(b, a) 3896 3897 b = "from foo import (bar, baz)" 3898 a = "from .foo import (bar, baz)" 3899 self.check_both(b, a) 3900 3901 def test_dotted_from(self): 3902 b = "from green.eggs import ham" 3903 a = "from .green.eggs import ham" 3904 self.check_both(b, a) 3905 3906 def test_from_as(self): 3907 b = "from green.eggs import ham as spam" 3908 a = "from .green.eggs import ham as spam" 3909 self.check_both(b, a) 3910 3911 def test_import(self): 3912 b = "import foo" 3913 a = "from . import foo" 3914 self.check_both(b, a) 3915 3916 b = "import foo, bar" 3917 a = "from . import foo, bar" 3918 self.check_both(b, a) 3919 3920 b = "import foo, bar, x" 3921 a = "from . import foo, bar, x" 3922 self.check_both(b, a) 3923 3924 b = "import x, y, z" 3925 a = "from . import x, y, z" 3926 self.check_both(b, a) 3927 3928 def test_import_as(self): 3929 b = "import foo as x" 3930 a = "from . import foo as x" 3931 self.check_both(b, a) 3932 3933 b = "import a as b, b as c, c as d" 3934 a = "from . import a as b, b as c, c as d" 3935 self.check_both(b, a) 3936 3937 def test_local_and_absolute(self): 3938 self.always_exists = False 3939 self.present_files = set(["foo.py", "__init__.py"]) 3940 3941 s = "import foo, bar" 3942 self.warns_unchanged(s, "absolute and local imports together") 3943 3944 def test_dotted_import(self): 3945 b = "import foo.bar" 3946 a = "from . import foo.bar" 3947 self.check_both(b, a) 3948 3949 def test_dotted_import_as(self): 3950 b = "import foo.bar as bang" 3951 a = "from . import foo.bar as bang" 3952 self.check_both(b, a) 3953 3954 def test_prefix(self): 3955 b = """ 3956 # prefix 3957 import foo.bar 3958 """ 3959 a = """ 3960 # prefix 3961 from . import foo.bar 3962 """ 3963 self.check_both(b, a) 3964 3965 3966class Test_set_literal(FixerTestCase): 3967 3968 fixer = "set_literal" 3969 3970 def test_basic(self): 3971 b = """set([1, 2, 3])""" 3972 a = """{1, 2, 3}""" 3973 self.check(b, a) 3974 3975 b = """set((1, 2, 3))""" 3976 a = """{1, 2, 3}""" 3977 self.check(b, a) 3978 3979 b = """set((1,))""" 3980 a = """{1}""" 3981 self.check(b, a) 3982 3983 b = """set([1])""" 3984 self.check(b, a) 3985 3986 b = """set((a, b))""" 3987 a = """{a, b}""" 3988 self.check(b, a) 3989 3990 b = """set([a, b])""" 3991 self.check(b, a) 3992 3993 b = """set((a*234, f(args=23)))""" 3994 a = """{a*234, f(args=23)}""" 3995 self.check(b, a) 3996 3997 b = """set([a*23, f(23)])""" 3998 a = """{a*23, f(23)}""" 3999 self.check(b, a) 4000 4001 b = """set([a-234**23])""" 4002 a = """{a-234**23}""" 4003 self.check(b, a) 4004 4005 def test_listcomps(self): 4006 b = """set([x for x in y])""" 4007 a = """{x for x in y}""" 4008 self.check(b, a) 4009 4010 b = """set([x for x in y if x == m])""" 4011 a = """{x for x in y if x == m}""" 4012 self.check(b, a) 4013 4014 b = """set([x for x in y for a in b])""" 4015 a = """{x for x in y for a in b}""" 4016 self.check(b, a) 4017 4018 b = """set([f(x) - 23 for x in y])""" 4019 a = """{f(x) - 23 for x in y}""" 4020 self.check(b, a) 4021 4022 def test_whitespace(self): 4023 b = """set( [1, 2])""" 4024 a = """{1, 2}""" 4025 self.check(b, a) 4026 4027 b = """set([1 , 2])""" 4028 a = """{1 , 2}""" 4029 self.check(b, a) 4030 4031 b = """set([ 1 ])""" 4032 a = """{ 1 }""" 4033 self.check(b, a) 4034 4035 b = """set( [1] )""" 4036 a = """{1}""" 4037 self.check(b, a) 4038 4039 b = """set([ 1, 2 ])""" 4040 a = """{ 1, 2 }""" 4041 self.check(b, a) 4042 4043 b = """set([x for x in y ])""" 4044 a = """{x for x in y }""" 4045 self.check(b, a) 4046 4047 b = """set( 4048 [1, 2] 4049 ) 4050 """ 4051 a = """{1, 2}\n""" 4052 self.check(b, a) 4053 4054 def test_comments(self): 4055 b = """set((1, 2)) # Hi""" 4056 a = """{1, 2} # Hi""" 4057 self.check(b, a) 4058 4059 # This isn't optimal behavior, but the fixer is optional. 4060 b = """ 4061 # Foo 4062 set( # Bar 4063 (1, 2) 4064 ) 4065 """ 4066 a = """ 4067 # Foo 4068 {1, 2} 4069 """ 4070 self.check(b, a) 4071 4072 def test_unchanged(self): 4073 s = """set()""" 4074 self.unchanged(s) 4075 4076 s = """set(a)""" 4077 self.unchanged(s) 4078 4079 s = """set(a, b, c)""" 4080 self.unchanged(s) 4081 4082 # Don't transform generators because they might have to be lazy. 4083 s = """set(x for x in y)""" 4084 self.unchanged(s) 4085 4086 s = """set(x for x in y if z)""" 4087 self.unchanged(s) 4088 4089 s = """set(a*823-23**2 + f(23))""" 4090 self.unchanged(s) 4091 4092 4093class Test_sys_exc(FixerTestCase): 4094 fixer = "sys_exc" 4095 4096 def test_0(self): 4097 b = "sys.exc_type" 4098 a = "sys.exc_info()[0]" 4099 self.check(b, a) 4100 4101 def test_1(self): 4102 b = "sys.exc_value" 4103 a = "sys.exc_info()[1]" 4104 self.check(b, a) 4105 4106 def test_2(self): 4107 b = "sys.exc_traceback" 4108 a = "sys.exc_info()[2]" 4109 self.check(b, a) 4110 4111 def test_3(self): 4112 b = "sys.exc_type # Foo" 4113 a = "sys.exc_info()[0] # Foo" 4114 self.check(b, a) 4115 4116 def test_4(self): 4117 b = "sys. exc_type" 4118 a = "sys. exc_info()[0]" 4119 self.check(b, a) 4120 4121 def test_5(self): 4122 b = "sys .exc_type" 4123 a = "sys .exc_info()[0]" 4124 self.check(b, a) 4125 4126 4127class Test_paren(FixerTestCase): 4128 fixer = "paren" 4129 4130 def test_0(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_1(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_2(self): 4141 b = """[i for i in 1, 2 ]""" 4142 a = """[i for i in (1, 2) ]""" 4143 self.check(b, a) 4144 4145 def test_3(self): 4146 b = """[i for i in 1, 2 if i]""" 4147 a = """[i for i in (1, 2) if i]""" 4148 self.check(b, a) 4149 4150 def test_4(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_5(self): 4156 b = """(i for i in 1, 2)""" 4157 a = """(i for i in (1, 2))""" 4158 self.check(b, a) 4159 4160 def test_6(self): 4161 b = """(i for i in 1 ,2 if i)""" 4162 a = """(i for i in (1 ,2) if i)""" 4163 self.check(b, a) 4164 4165 def test_unchanged_0(self): 4166 s = """[i for i in (1, 2)]""" 4167 self.unchanged(s) 4168 4169 def test_unchanged_1(self): 4170 s = """[i for i in foo()]""" 4171 self.unchanged(s) 4172 4173 def test_unchanged_2(self): 4174 s = """[i for i in (1, 2) if nothing]""" 4175 self.unchanged(s) 4176 4177 def test_unchanged_3(self): 4178 s = """(i for i in (1, 2))""" 4179 self.unchanged(s) 4180 4181 def test_unchanged_4(self): 4182 s = """[i for i in m]""" 4183 self.unchanged(s) 4184 4185class Test_metaclass(FixerTestCase): 4186 4187 fixer = 'metaclass' 4188 4189 def test_unchanged(self): 4190 self.unchanged("class X(): pass") 4191 self.unchanged("class X(object): pass") 4192 self.unchanged("class X(object1, object2): pass") 4193 self.unchanged("class X(object1, object2, object3): pass") 4194 self.unchanged("class X(metaclass=Meta): pass") 4195 self.unchanged("class X(b, arg=23, metclass=Meta): pass") 4196 self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass") 4197 4198 s = """ 4199 class X: 4200 def __metaclass__(self): pass 4201 """ 4202 self.unchanged(s) 4203 4204 s = """ 4205 class X: 4206 a[23] = 74 4207 """ 4208 self.unchanged(s) 4209 4210 def test_comments(self): 4211 b = """ 4212 class X: 4213 # hi 4214 __metaclass__ = AppleMeta 4215 """ 4216 a = """ 4217 class X(metaclass=AppleMeta): 4218 # hi 4219 pass 4220 """ 4221 self.check(b, a) 4222 4223 b = """ 4224 class X: 4225 __metaclass__ = Meta 4226 # Bedtime! 4227 """ 4228 a = """ 4229 class X(metaclass=Meta): 4230 pass 4231 # Bedtime! 4232 """ 4233 self.check(b, a) 4234 4235 def test_meta(self): 4236 # no-parent class, odd body 4237 b = """ 4238 class X(): 4239 __metaclass__ = Q 4240 pass 4241 """ 4242 a = """ 4243 class X(metaclass=Q): 4244 pass 4245 """ 4246 self.check(b, a) 4247 4248 # one parent class, no body 4249 b = """class X(object): __metaclass__ = Q""" 4250 a = """class X(object, metaclass=Q): pass""" 4251 self.check(b, a) 4252 4253 4254 # one parent, simple body 4255 b = """ 4256 class X(object): 4257 __metaclass__ = Meta 4258 bar = 7 4259 """ 4260 a = """ 4261 class X(object, metaclass=Meta): 4262 bar = 7 4263 """ 4264 self.check(b, a) 4265 4266 b = """ 4267 class X: 4268 __metaclass__ = Meta; x = 4; g = 23 4269 """ 4270 a = """ 4271 class X(metaclass=Meta): 4272 x = 4; g = 23 4273 """ 4274 self.check(b, a) 4275 4276 # one parent, simple body, __metaclass__ last 4277 b = """ 4278 class X(object): 4279 bar = 7 4280 __metaclass__ = Meta 4281 """ 4282 a = """ 4283 class X(object, metaclass=Meta): 4284 bar = 7 4285 """ 4286 self.check(b, a) 4287 4288 # redefining __metaclass__ 4289 b = """ 4290 class X(): 4291 __metaclass__ = A 4292 __metaclass__ = B 4293 bar = 7 4294 """ 4295 a = """ 4296 class X(metaclass=B): 4297 bar = 7 4298 """ 4299 self.check(b, a) 4300 4301 # multiple inheritance, simple body 4302 b = """ 4303 class X(clsA, clsB): 4304 __metaclass__ = Meta 4305 bar = 7 4306 """ 4307 a = """ 4308 class X(clsA, clsB, metaclass=Meta): 4309 bar = 7 4310 """ 4311 self.check(b, a) 4312 4313 # keywords in the class statement 4314 b = """class m(a, arg=23): __metaclass__ = Meta""" 4315 a = """class m(a, arg=23, metaclass=Meta): pass""" 4316 self.check(b, a) 4317 4318 b = """ 4319 class X(expression(2 + 4)): 4320 __metaclass__ = Meta 4321 """ 4322 a = """ 4323 class X(expression(2 + 4), metaclass=Meta): 4324 pass 4325 """ 4326 self.check(b, a) 4327 4328 b = """ 4329 class X(expression(2 + 4), x**4): 4330 __metaclass__ = Meta 4331 """ 4332 a = """ 4333 class X(expression(2 + 4), x**4, metaclass=Meta): 4334 pass 4335 """ 4336 self.check(b, a) 4337 4338 b = """ 4339 class X: 4340 __metaclass__ = Meta 4341 save.py = 23 4342 """ 4343 a = """ 4344 class X(metaclass=Meta): 4345 save.py = 23 4346 """ 4347 self.check(b, a) 4348 4349 4350class Test_getcwdu(FixerTestCase): 4351 4352 fixer = 'getcwdu' 4353 4354 def test_basic(self): 4355 b = """os.getcwdu""" 4356 a = """os.getcwd""" 4357 self.check(b, a) 4358 4359 b = """os.getcwdu()""" 4360 a = """os.getcwd()""" 4361 self.check(b, a) 4362 4363 b = """meth = os.getcwdu""" 4364 a = """meth = os.getcwd""" 4365 self.check(b, a) 4366 4367 b = """os.getcwdu(args)""" 4368 a = """os.getcwd(args)""" 4369 self.check(b, a) 4370 4371 def test_comment(self): 4372 b = """os.getcwdu() # Foo""" 4373 a = """os.getcwd() # Foo""" 4374 self.check(b, a) 4375 4376 def test_unchanged(self): 4377 s = """os.getcwd()""" 4378 self.unchanged(s) 4379 4380 s = """getcwdu()""" 4381 self.unchanged(s) 4382 4383 s = """os.getcwdb()""" 4384 self.unchanged(s) 4385 4386 def test_indentation(self): 4387 b = """ 4388 if 1: 4389 os.getcwdu() 4390 """ 4391 a = """ 4392 if 1: 4393 os.getcwd() 4394 """ 4395 self.check(b, a) 4396 4397 def test_multilation(self): 4398 b = """os .getcwdu()""" 4399 a = """os .getcwd()""" 4400 self.check(b, a) 4401 4402 b = """os. getcwdu""" 4403 a = """os. getcwd""" 4404 self.check(b, a) 4405 4406 b = """os.getcwdu ( )""" 4407 a = """os.getcwd ( )""" 4408 self.check(b, a) 4409 4410 4411class Test_operator(FixerTestCase): 4412 4413 fixer = "operator" 4414 4415 def test_operator_isCallable(self): 4416 b = "operator.isCallable(x)" 4417 a = "callable(x)" 4418 self.check(b, a) 4419 4420 def test_operator_sequenceIncludes(self): 4421 b = "operator.sequenceIncludes(x, y)" 4422 a = "operator.contains(x, y)" 4423 self.check(b, a) 4424 4425 b = "operator .sequenceIncludes(x, y)" 4426 a = "operator .contains(x, y)" 4427 self.check(b, a) 4428 4429 b = "operator. sequenceIncludes(x, y)" 4430 a = "operator. contains(x, y)" 4431 self.check(b, a) 4432 4433 def test_operator_isSequenceType(self): 4434 b = "operator.isSequenceType(x)" 4435 a = "import collections.abc\nisinstance(x, collections.abc.Sequence)" 4436 self.check(b, a) 4437 4438 def test_operator_isMappingType(self): 4439 b = "operator.isMappingType(x)" 4440 a = "import collections.abc\nisinstance(x, collections.abc.Mapping)" 4441 self.check(b, a) 4442 4443 def test_operator_isNumberType(self): 4444 b = "operator.isNumberType(x)" 4445 a = "import numbers\nisinstance(x, numbers.Number)" 4446 self.check(b, a) 4447 4448 def test_operator_repeat(self): 4449 b = "operator.repeat(x, n)" 4450 a = "operator.mul(x, n)" 4451 self.check(b, a) 4452 4453 b = "operator .repeat(x, n)" 4454 a = "operator .mul(x, n)" 4455 self.check(b, a) 4456 4457 b = "operator. repeat(x, n)" 4458 a = "operator. mul(x, n)" 4459 self.check(b, a) 4460 4461 def test_operator_irepeat(self): 4462 b = "operator.irepeat(x, n)" 4463 a = "operator.imul(x, n)" 4464 self.check(b, a) 4465 4466 b = "operator .irepeat(x, n)" 4467 a = "operator .imul(x, n)" 4468 self.check(b, a) 4469 4470 b = "operator. irepeat(x, n)" 4471 a = "operator. imul(x, n)" 4472 self.check(b, a) 4473 4474 def test_bare_isCallable(self): 4475 s = "isCallable(x)" 4476 t = "You should use 'callable(x)' here." 4477 self.warns_unchanged(s, t) 4478 4479 def test_bare_sequenceIncludes(self): 4480 s = "sequenceIncludes(x, y)" 4481 t = "You should use 'operator.contains(x, y)' here." 4482 self.warns_unchanged(s, t) 4483 4484 def test_bare_operator_isSequenceType(self): 4485 s = "isSequenceType(z)" 4486 t = "You should use 'isinstance(z, collections.abc.Sequence)' here." 4487 self.warns_unchanged(s, t) 4488 4489 def test_bare_operator_isMappingType(self): 4490 s = "isMappingType(x)" 4491 t = "You should use 'isinstance(x, collections.abc.Mapping)' here." 4492 self.warns_unchanged(s, t) 4493 4494 def test_bare_operator_isNumberType(self): 4495 s = "isNumberType(y)" 4496 t = "You should use 'isinstance(y, numbers.Number)' here." 4497 self.warns_unchanged(s, t) 4498 4499 def test_bare_operator_repeat(self): 4500 s = "repeat(x, n)" 4501 t = "You should use 'operator.mul(x, n)' here." 4502 self.warns_unchanged(s, t) 4503 4504 def test_bare_operator_irepeat(self): 4505 s = "irepeat(y, 187)" 4506 t = "You should use 'operator.imul(y, 187)' here." 4507 self.warns_unchanged(s, t) 4508 4509 4510class Test_exitfunc(FixerTestCase): 4511 4512 fixer = "exitfunc" 4513 4514 def test_simple(self): 4515 b = """ 4516 import sys 4517 sys.exitfunc = my_atexit 4518 """ 4519 a = """ 4520 import sys 4521 import atexit 4522 atexit.register(my_atexit) 4523 """ 4524 self.check(b, a) 4525 4526 def test_names_import(self): 4527 b = """ 4528 import sys, crumbs 4529 sys.exitfunc = my_func 4530 """ 4531 a = """ 4532 import sys, crumbs, atexit 4533 atexit.register(my_func) 4534 """ 4535 self.check(b, a) 4536 4537 def test_complex_expression(self): 4538 b = """ 4539 import sys 4540 sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression 4541 """ 4542 a = """ 4543 import sys 4544 import atexit 4545 atexit.register(do(d)/a()+complex(f=23, g=23)*expression) 4546 """ 4547 self.check(b, a) 4548 4549 def test_comments(self): 4550 b = """ 4551 import sys # Foo 4552 sys.exitfunc = f # Blah 4553 """ 4554 a = """ 4555 import sys 4556 import atexit # Foo 4557 atexit.register(f) # Blah 4558 """ 4559 self.check(b, a) 4560 4561 b = """ 4562 import apples, sys, crumbs, larry # Pleasant comments 4563 sys.exitfunc = func 4564 """ 4565 a = """ 4566 import apples, sys, crumbs, larry, atexit # Pleasant comments 4567 atexit.register(func) 4568 """ 4569 self.check(b, a) 4570 4571 def test_in_a_function(self): 4572 b = """ 4573 import sys 4574 def f(): 4575 sys.exitfunc = func 4576 """ 4577 a = """ 4578 import sys 4579 import atexit 4580 def f(): 4581 atexit.register(func) 4582 """ 4583 self.check(b, a) 4584 4585 def test_no_sys_import(self): 4586 b = """sys.exitfunc = f""" 4587 a = """atexit.register(f)""" 4588 msg = ("Can't find sys import; Please add an atexit import at the " 4589 "top of your file.") 4590 self.warns(b, a, msg) 4591 4592 4593 def test_unchanged(self): 4594 s = """f(sys.exitfunc)""" 4595 self.unchanged(s) 4596 4597 4598class Test_asserts(FixerTestCase): 4599 4600 fixer = "asserts" 4601 4602 def test_deprecated_names(self): 4603 tests = [ 4604 ('self.assert_(True)', 'self.assertTrue(True)'), 4605 ('self.assertEquals(2, 2)', 'self.assertEqual(2, 2)'), 4606 ('self.assertNotEquals(2, 3)', 'self.assertNotEqual(2, 3)'), 4607 ('self.assertAlmostEquals(2, 3)', 'self.assertAlmostEqual(2, 3)'), 4608 ('self.assertNotAlmostEquals(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), 4609 ('self.failUnlessEqual(2, 2)', 'self.assertEqual(2, 2)'), 4610 ('self.failIfEqual(2, 3)', 'self.assertNotEqual(2, 3)'), 4611 ('self.failUnlessAlmostEqual(2, 3)', 'self.assertAlmostEqual(2, 3)'), 4612 ('self.failIfAlmostEqual(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), 4613 ('self.failUnless(True)', 'self.assertTrue(True)'), 4614 ('self.failUnlessRaises(foo)', 'self.assertRaises(foo)'), 4615 ('self.failIf(False)', 'self.assertFalse(False)'), 4616 ] 4617 for b, a in tests: 4618 self.check(b, a) 4619 4620 def test_variants(self): 4621 b = 'eq = self.assertEquals' 4622 a = 'eq = self.assertEqual' 4623 self.check(b, a) 4624 b = 'self.assertEquals(2, 3, msg="fail")' 4625 a = 'self.assertEqual(2, 3, msg="fail")' 4626 self.check(b, a) 4627 b = 'self.assertEquals(2, 3, msg="fail") # foo' 4628 a = 'self.assertEqual(2, 3, msg="fail") # foo' 4629 self.check(b, a) 4630 b = 'self.assertEquals (2, 3)' 4631 a = 'self.assertEqual (2, 3)' 4632 self.check(b, a) 4633 b = ' self.assertEquals (2, 3)' 4634 a = ' self.assertEqual (2, 3)' 4635 self.check(b, a) 4636 b = 'with self.failUnlessRaises(Explosion): explode()' 4637 a = 'with self.assertRaises(Explosion): explode()' 4638 self.check(b, a) 4639 b = 'with self.failUnlessRaises(Explosion) as cm: explode()' 4640 a = 'with self.assertRaises(Explosion) as cm: explode()' 4641 self.check(b, a) 4642 4643 def test_unchanged(self): 4644 self.unchanged('self.assertEqualsOnSaturday') 4645 self.unchanged('self.assertEqualsOnSaturday(3, 5)') 4646