PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/lib2to3/tests/test_fixers.py

http://unladen-swallow.googlecode.com/
Python | 4023 lines | 3981 code | 36 blank | 6 comment | 17 complexity | 8a68ab4c442c928a298d8ab47967b6e0 MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  1. #!/usr/bin/env python2.5
  2. """ Test suite for the fixer modules """
  3. # Author: Collin Winter
  4. # Testing imports
  5. try:
  6. from tests import support
  7. except ImportError:
  8. import support
  9. # Python imports
  10. import os
  11. import unittest
  12. from itertools import chain
  13. from operator import itemgetter
  14. # Local imports
  15. from lib2to3 import pygram, pytree, refactor, fixer_util
  16. class FixerTestCase(support.TestCase):
  17. def setUp(self, fix_list=None):
  18. if fix_list is None:
  19. fix_list = [self.fixer]
  20. options = {"print_function" : False}
  21. self.refactor = support.get_refactorer(fix_list, options)
  22. self.fixer_log = []
  23. self.filename = "<string>"
  24. for fixer in chain(self.refactor.pre_order,
  25. self.refactor.post_order):
  26. fixer.log = self.fixer_log
  27. def _check(self, before, after):
  28. before = support.reformat(before)
  29. after = support.reformat(after)
  30. tree = self.refactor.refactor_string(before, self.filename)
  31. self.failUnlessEqual(after, str(tree))
  32. return tree
  33. def check(self, before, after, ignore_warnings=False):
  34. tree = self._check(before, after)
  35. self.failUnless(tree.was_changed)
  36. if not ignore_warnings:
  37. self.failUnlessEqual(self.fixer_log, [])
  38. def warns(self, before, after, message, unchanged=False):
  39. tree = self._check(before, after)
  40. self.failUnless(message in "".join(self.fixer_log))
  41. if not unchanged:
  42. self.failUnless(tree.was_changed)
  43. def warns_unchanged(self, before, message):
  44. self.warns(before, before, message, unchanged=True)
  45. def unchanged(self, before, ignore_warnings=False):
  46. self._check(before, before)
  47. if not ignore_warnings:
  48. self.failUnlessEqual(self.fixer_log, [])
  49. def assert_runs_after(self, *names):
  50. fixes = [self.fixer]
  51. fixes.extend(names)
  52. options = {"print_function" : False}
  53. r = support.get_refactorer(fixes, options)
  54. (pre, post) = r.get_fixers()
  55. n = "fix_" + self.fixer
  56. if post and post[-1].__class__.__module__.endswith(n):
  57. # We're the last fixer to run
  58. return
  59. if pre and pre[-1].__class__.__module__.endswith(n) and not post:
  60. # We're the last in pre and post is empty
  61. return
  62. self.fail("Fixer run order (%s) is incorrect; %s should be last."\
  63. %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
  64. class Test_ne(FixerTestCase):
  65. fixer = "ne"
  66. def test_basic(self):
  67. b = """if x <> y:
  68. pass"""
  69. a = """if x != y:
  70. pass"""
  71. self.check(b, a)
  72. def test_no_spaces(self):
  73. b = """if x<>y:
  74. pass"""
  75. a = """if x!=y:
  76. pass"""
  77. self.check(b, a)
  78. def test_chained(self):
  79. b = """if x<>y<>z:
  80. pass"""
  81. a = """if x!=y!=z:
  82. pass"""
  83. self.check(b, a)
  84. class Test_has_key(FixerTestCase):
  85. fixer = "has_key"
  86. def test_1(self):
  87. b = """x = d.has_key("x") or d.has_key("y")"""
  88. a = """x = "x" in d or "y" in d"""
  89. self.check(b, a)
  90. def test_2(self):
  91. b = """x = a.b.c.d.has_key("x") ** 3"""
  92. a = """x = ("x" in a.b.c.d) ** 3"""
  93. self.check(b, a)
  94. def test_3(self):
  95. b = """x = a.b.has_key(1 + 2).__repr__()"""
  96. a = """x = (1 + 2 in a.b).__repr__()"""
  97. self.check(b, a)
  98. def test_4(self):
  99. b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
  100. a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
  101. self.check(b, a)
  102. def test_5(self):
  103. b = """x = a.has_key(f or g)"""
  104. a = """x = (f or g) in a"""
  105. self.check(b, a)
  106. def test_6(self):
  107. b = """x = a + b.has_key(c)"""
  108. a = """x = a + (c in b)"""
  109. self.check(b, a)
  110. def test_7(self):
  111. b = """x = a.has_key(lambda: 12)"""
  112. a = """x = (lambda: 12) in a"""
  113. self.check(b, a)
  114. def test_8(self):
  115. b = """x = a.has_key(a for a in b)"""
  116. a = """x = (a for a in b) in a"""
  117. self.check(b, a)
  118. def test_9(self):
  119. b = """if not a.has_key(b): pass"""
  120. a = """if b not in a: pass"""
  121. self.check(b, a)
  122. def test_10(self):
  123. b = """if not a.has_key(b).__repr__(): pass"""
  124. a = """if not (b in a).__repr__(): pass"""
  125. self.check(b, a)
  126. def test_11(self):
  127. b = """if not a.has_key(b) ** 2: pass"""
  128. a = """if not (b in a) ** 2: pass"""
  129. self.check(b, a)
  130. class Test_apply(FixerTestCase):
  131. fixer = "apply"
  132. def test_1(self):
  133. b = """x = apply(f, g + h)"""
  134. a = """x = f(*g + h)"""
  135. self.check(b, a)
  136. def test_2(self):
  137. b = """y = apply(f, g, h)"""
  138. a = """y = f(*g, **h)"""
  139. self.check(b, a)
  140. def test_3(self):
  141. b = """z = apply(fs[0], g or h, h or g)"""
  142. a = """z = fs[0](*g or h, **h or g)"""
  143. self.check(b, a)
  144. def test_4(self):
  145. b = """apply(f, (x, y) + t)"""
  146. a = """f(*(x, y) + t)"""
  147. self.check(b, a)
  148. def test_5(self):
  149. b = """apply(f, args,)"""
  150. a = """f(*args)"""
  151. self.check(b, a)
  152. def test_6(self):
  153. b = """apply(f, args, kwds,)"""
  154. a = """f(*args, **kwds)"""
  155. self.check(b, a)
  156. # Test that complex functions are parenthesized
  157. def test_complex_1(self):
  158. b = """x = apply(f+g, args)"""
  159. a = """x = (f+g)(*args)"""
  160. self.check(b, a)
  161. def test_complex_2(self):
  162. b = """x = apply(f*g, args)"""
  163. a = """x = (f*g)(*args)"""
  164. self.check(b, a)
  165. def test_complex_3(self):
  166. b = """x = apply(f**g, args)"""
  167. a = """x = (f**g)(*args)"""
  168. self.check(b, a)
  169. # But dotted names etc. not
  170. def test_dotted_name(self):
  171. b = """x = apply(f.g, args)"""
  172. a = """x = f.g(*args)"""
  173. self.check(b, a)
  174. def test_subscript(self):
  175. b = """x = apply(f[x], args)"""
  176. a = """x = f[x](*args)"""
  177. self.check(b, a)
  178. def test_call(self):
  179. b = """x = apply(f(), args)"""
  180. a = """x = f()(*args)"""
  181. self.check(b, a)
  182. # Extreme case
  183. def test_extreme(self):
  184. b = """x = apply(a.b.c.d.e.f, args, kwds)"""
  185. a = """x = a.b.c.d.e.f(*args, **kwds)"""
  186. self.check(b, a)
  187. # XXX Comments in weird places still get lost
  188. def test_weird_comments(self):
  189. b = """apply( # foo
  190. f, # bar
  191. args)"""
  192. a = """f(*args)"""
  193. self.check(b, a)
  194. # These should *not* be touched
  195. def test_unchanged_1(self):
  196. s = """apply()"""
  197. self.unchanged(s)
  198. def test_unchanged_2(self):
  199. s = """apply(f)"""
  200. self.unchanged(s)
  201. def test_unchanged_3(self):
  202. s = """apply(f,)"""
  203. self.unchanged(s)
  204. def test_unchanged_4(self):
  205. s = """apply(f, args, kwds, extras)"""
  206. self.unchanged(s)
  207. def test_unchanged_5(self):
  208. s = """apply(f, *args, **kwds)"""
  209. self.unchanged(s)
  210. def test_unchanged_6(self):
  211. s = """apply(f, *args)"""
  212. self.unchanged(s)
  213. def test_unchanged_7(self):
  214. s = """apply(func=f, args=args, kwds=kwds)"""
  215. self.unchanged(s)
  216. def test_unchanged_8(self):
  217. s = """apply(f, args=args, kwds=kwds)"""
  218. self.unchanged(s)
  219. def test_unchanged_9(self):
  220. s = """apply(f, args, kwds=kwds)"""
  221. self.unchanged(s)
  222. def test_space_1(self):
  223. a = """apply( f, args, kwds)"""
  224. b = """f(*args, **kwds)"""
  225. self.check(a, b)
  226. def test_space_2(self):
  227. a = """apply( f ,args,kwds )"""
  228. b = """f(*args, **kwds)"""
  229. self.check(a, b)
  230. class Test_intern(FixerTestCase):
  231. fixer = "intern"
  232. def test_prefix_preservation(self):
  233. b = """x = intern( a )"""
  234. a = """import sys\nx = sys.intern( a )"""
  235. self.check(b, a)
  236. b = """y = intern("b" # test
  237. )"""
  238. a = """import sys\ny = sys.intern("b" # test
  239. )"""
  240. self.check(b, a)
  241. b = """z = intern(a+b+c.d, )"""
  242. a = """import sys\nz = sys.intern(a+b+c.d, )"""
  243. self.check(b, a)
  244. def test(self):
  245. b = """x = intern(a)"""
  246. a = """import sys\nx = sys.intern(a)"""
  247. self.check(b, a)
  248. b = """z = intern(a+b+c.d,)"""
  249. a = """import sys\nz = sys.intern(a+b+c.d,)"""
  250. self.check(b, a)
  251. b = """intern("y%s" % 5).replace("y", "")"""
  252. a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
  253. self.check(b, a)
  254. # These should not be refactored
  255. def test_unchanged(self):
  256. s = """intern(a=1)"""
  257. self.unchanged(s)
  258. s = """intern(f, g)"""
  259. self.unchanged(s)
  260. s = """intern(*h)"""
  261. self.unchanged(s)
  262. s = """intern(**i)"""
  263. self.unchanged(s)
  264. s = """intern()"""
  265. self.unchanged(s)
  266. class Test_reduce(FixerTestCase):
  267. fixer = "reduce"
  268. def test_simple_call(self):
  269. b = "reduce(a, b, c)"
  270. a = "from functools import reduce\nreduce(a, b, c)"
  271. self.check(b, a)
  272. def test_call_with_lambda(self):
  273. b = "reduce(lambda x, y: x + y, seq)"
  274. a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)"
  275. self.check(b, a)
  276. def test_unchanged(self):
  277. s = "reduce(a)"
  278. self.unchanged(s)
  279. s = "reduce(a, b=42)"
  280. self.unchanged(s)
  281. s = "reduce(a, b, c, d)"
  282. self.unchanged(s)
  283. s = "reduce(**c)"
  284. self.unchanged(s)
  285. s = "reduce()"
  286. self.unchanged(s)
  287. class Test_print(FixerTestCase):
  288. fixer = "print"
  289. def test_prefix_preservation(self):
  290. b = """print 1, 1+1, 1+1+1"""
  291. a = """print(1, 1+1, 1+1+1)"""
  292. self.check(b, a)
  293. def test_idempotency(self):
  294. s = """print()"""
  295. self.unchanged(s)
  296. s = """print('')"""
  297. self.unchanged(s)
  298. def test_idempotency_print_as_function(self):
  299. print_stmt = pygram.python_grammar.keywords.pop("print")
  300. try:
  301. s = """print(1, 1+1, 1+1+1)"""
  302. self.unchanged(s)
  303. s = """print()"""
  304. self.unchanged(s)
  305. s = """print('')"""
  306. self.unchanged(s)
  307. finally:
  308. pygram.python_grammar.keywords["print"] = print_stmt
  309. def test_1(self):
  310. b = """print 1, 1+1, 1+1+1"""
  311. a = """print(1, 1+1, 1+1+1)"""
  312. self.check(b, a)
  313. def test_2(self):
  314. b = """print 1, 2"""
  315. a = """print(1, 2)"""
  316. self.check(b, a)
  317. def test_3(self):
  318. b = """print"""
  319. a = """print()"""
  320. self.check(b, a)
  321. def test_4(self):
  322. # from bug 3000
  323. b = """print whatever; print"""
  324. a = """print(whatever); print()"""
  325. self.check(b, a)
  326. def test_5(self):
  327. b = """print; print whatever;"""
  328. a = """print(); print(whatever);"""
  329. def test_tuple(self):
  330. b = """print (a, b, c)"""
  331. a = """print((a, b, c))"""
  332. self.check(b, a)
  333. # trailing commas
  334. def test_trailing_comma_1(self):
  335. b = """print 1, 2, 3,"""
  336. a = """print(1, 2, 3, end=' ')"""
  337. self.check(b, a)
  338. def test_trailing_comma_2(self):
  339. b = """print 1, 2,"""
  340. a = """print(1, 2, end=' ')"""
  341. self.check(b, a)
  342. def test_trailing_comma_3(self):
  343. b = """print 1,"""
  344. a = """print(1, end=' ')"""
  345. self.check(b, a)
  346. # >> stuff
  347. def test_vargs_without_trailing_comma(self):
  348. b = """print >>sys.stderr, 1, 2, 3"""
  349. a = """print(1, 2, 3, file=sys.stderr)"""
  350. self.check(b, a)
  351. def test_with_trailing_comma(self):
  352. b = """print >>sys.stderr, 1, 2,"""
  353. a = """print(1, 2, end=' ', file=sys.stderr)"""
  354. self.check(b, a)
  355. def test_no_trailing_comma(self):
  356. b = """print >>sys.stderr, 1+1"""
  357. a = """print(1+1, file=sys.stderr)"""
  358. self.check(b, a)
  359. def test_spaces_before_file(self):
  360. b = """print >> sys.stderr"""
  361. a = """print(file=sys.stderr)"""
  362. self.check(b, a)
  363. # With from __future__ import print_function
  364. def test_with_future_print_function(self):
  365. # XXX: These tests won't actually do anything until the parser
  366. # is fixed so it won't crash when it sees print(x=y).
  367. # When #2412 is fixed, the try/except block can be taken
  368. # out and the tests can be run like normal.
  369. # MvL: disable entirely for now, so that it doesn't print to stdout
  370. return
  371. try:
  372. s = "from __future__ import print_function\n"\
  373. "print('Hai!', end=' ')"
  374. self.unchanged(s)
  375. b = "print 'Hello, world!'"
  376. a = "print('Hello, world!')"
  377. self.check(b, a)
  378. s = "from __future__ import *\n"\
  379. "print('Hai!', end=' ')"
  380. self.unchanged(s)
  381. except:
  382. return
  383. else:
  384. self.assertFalse(True, "#2421 has been fixed -- printing tests "\
  385. "need to be updated!")
  386. class Test_exec(FixerTestCase):
  387. fixer = "exec"
  388. def test_prefix_preservation(self):
  389. b = """ exec code in ns1, ns2"""
  390. a = """ exec(code, ns1, ns2)"""
  391. self.check(b, a)
  392. def test_basic(self):
  393. b = """exec code"""
  394. a = """exec(code)"""
  395. self.check(b, a)
  396. def test_with_globals(self):
  397. b = """exec code in ns"""
  398. a = """exec(code, ns)"""
  399. self.check(b, a)
  400. def test_with_globals_locals(self):
  401. b = """exec code in ns1, ns2"""
  402. a = """exec(code, ns1, ns2)"""
  403. self.check(b, a)
  404. def test_complex_1(self):
  405. b = """exec (a.b()) in ns"""
  406. a = """exec((a.b()), ns)"""
  407. self.check(b, a)
  408. def test_complex_2(self):
  409. b = """exec a.b() + c in ns"""
  410. a = """exec(a.b() + c, ns)"""
  411. self.check(b, a)
  412. # These should not be touched
  413. def test_unchanged_1(self):
  414. s = """exec(code)"""
  415. self.unchanged(s)
  416. def test_unchanged_2(self):
  417. s = """exec (code)"""
  418. self.unchanged(s)
  419. def test_unchanged_3(self):
  420. s = """exec(code, ns)"""
  421. self.unchanged(s)
  422. def test_unchanged_4(self):
  423. s = """exec(code, ns1, ns2)"""
  424. self.unchanged(s)
  425. class Test_repr(FixerTestCase):
  426. fixer = "repr"
  427. def test_prefix_preservation(self):
  428. b = """x = `1 + 2`"""
  429. a = """x = repr(1 + 2)"""
  430. self.check(b, a)
  431. def test_simple_1(self):
  432. b = """x = `1 + 2`"""
  433. a = """x = repr(1 + 2)"""
  434. self.check(b, a)
  435. def test_simple_2(self):
  436. b = """y = `x`"""
  437. a = """y = repr(x)"""
  438. self.check(b, a)
  439. def test_complex(self):
  440. b = """z = `y`.__repr__()"""
  441. a = """z = repr(y).__repr__()"""
  442. self.check(b, a)
  443. def test_tuple(self):
  444. b = """x = `1, 2, 3`"""
  445. a = """x = repr((1, 2, 3))"""
  446. self.check(b, a)
  447. def test_nested(self):
  448. b = """x = `1 + `2``"""
  449. a = """x = repr(1 + repr(2))"""
  450. self.check(b, a)
  451. def test_nested_tuples(self):
  452. b = """x = `1, 2 + `3, 4``"""
  453. a = """x = repr((1, 2 + repr((3, 4))))"""
  454. self.check(b, a)
  455. class Test_except(FixerTestCase):
  456. fixer = "except"
  457. def test_prefix_preservation(self):
  458. b = """
  459. try:
  460. pass
  461. except (RuntimeError, ImportError), e:
  462. pass"""
  463. a = """
  464. try:
  465. pass
  466. except (RuntimeError, ImportError) as e:
  467. pass"""
  468. self.check(b, a)
  469. def test_simple(self):
  470. b = """
  471. try:
  472. pass
  473. except Foo, e:
  474. pass"""
  475. a = """
  476. try:
  477. pass
  478. except Foo as e:
  479. pass"""
  480. self.check(b, a)
  481. def test_simple_no_space_before_target(self):
  482. b = """
  483. try:
  484. pass
  485. except Foo,e:
  486. pass"""
  487. a = """
  488. try:
  489. pass
  490. except Foo as e:
  491. pass"""
  492. self.check(b, a)
  493. def test_tuple_unpack(self):
  494. b = """
  495. def foo():
  496. try:
  497. pass
  498. except Exception, (f, e):
  499. pass
  500. except ImportError, e:
  501. pass"""
  502. a = """
  503. def foo():
  504. try:
  505. pass
  506. except Exception as xxx_todo_changeme:
  507. (f, e) = xxx_todo_changeme.args
  508. pass
  509. except ImportError as e:
  510. pass"""
  511. self.check(b, a)
  512. def test_multi_class(self):
  513. b = """
  514. try:
  515. pass
  516. except (RuntimeError, ImportError), e:
  517. pass"""
  518. a = """
  519. try:
  520. pass
  521. except (RuntimeError, ImportError) as e:
  522. pass"""
  523. self.check(b, a)
  524. def test_list_unpack(self):
  525. b = """
  526. try:
  527. pass
  528. except Exception, [a, b]:
  529. pass"""
  530. a = """
  531. try:
  532. pass
  533. except Exception as xxx_todo_changeme:
  534. [a, b] = xxx_todo_changeme.args
  535. pass"""
  536. self.check(b, a)
  537. def test_weird_target_1(self):
  538. b = """
  539. try:
  540. pass
  541. except Exception, d[5]:
  542. pass"""
  543. a = """
  544. try:
  545. pass
  546. except Exception as xxx_todo_changeme:
  547. d[5] = xxx_todo_changeme
  548. pass"""
  549. self.check(b, a)
  550. def test_weird_target_2(self):
  551. b = """
  552. try:
  553. pass
  554. except Exception, a.foo:
  555. pass"""
  556. a = """
  557. try:
  558. pass
  559. except Exception as xxx_todo_changeme:
  560. a.foo = xxx_todo_changeme
  561. pass"""
  562. self.check(b, a)
  563. def test_weird_target_3(self):
  564. b = """
  565. try:
  566. pass
  567. except Exception, a().foo:
  568. pass"""
  569. a = """
  570. try:
  571. pass
  572. except Exception as xxx_todo_changeme:
  573. a().foo = xxx_todo_changeme
  574. pass"""
  575. self.check(b, a)
  576. def test_bare_except(self):
  577. b = """
  578. try:
  579. pass
  580. except Exception, a:
  581. pass
  582. except:
  583. pass"""
  584. a = """
  585. try:
  586. pass
  587. except Exception as a:
  588. pass
  589. except:
  590. pass"""
  591. self.check(b, a)
  592. def test_bare_except_and_else_finally(self):
  593. b = """
  594. try:
  595. pass
  596. except Exception, a:
  597. pass
  598. except:
  599. pass
  600. else:
  601. pass
  602. finally:
  603. pass"""
  604. a = """
  605. try:
  606. pass
  607. except Exception as a:
  608. pass
  609. except:
  610. pass
  611. else:
  612. pass
  613. finally:
  614. pass"""
  615. self.check(b, a)
  616. def test_multi_fixed_excepts_before_bare_except(self):
  617. b = """
  618. try:
  619. pass
  620. except TypeError, b:
  621. pass
  622. except Exception, a:
  623. pass
  624. except:
  625. pass"""
  626. a = """
  627. try:
  628. pass
  629. except TypeError as b:
  630. pass
  631. except Exception as a:
  632. pass
  633. except:
  634. pass"""
  635. self.check(b, a)
  636. # These should not be touched:
  637. def test_unchanged_1(self):
  638. s = """
  639. try:
  640. pass
  641. except:
  642. pass"""
  643. self.unchanged(s)
  644. def test_unchanged_2(self):
  645. s = """
  646. try:
  647. pass
  648. except Exception:
  649. pass"""
  650. self.unchanged(s)
  651. def test_unchanged_3(self):
  652. s = """
  653. try:
  654. pass
  655. except (Exception, SystemExit):
  656. pass"""
  657. self.unchanged(s)
  658. class Test_raise(FixerTestCase):
  659. fixer = "raise"
  660. def test_basic(self):
  661. b = """raise Exception, 5"""
  662. a = """raise Exception(5)"""
  663. self.check(b, a)
  664. def test_prefix_preservation(self):
  665. b = """raise Exception,5"""
  666. a = """raise Exception(5)"""
  667. self.check(b, a)
  668. b = """raise Exception, 5"""
  669. a = """raise Exception(5)"""
  670. self.check(b, a)
  671. def test_with_comments(self):
  672. b = """raise Exception, 5 # foo"""
  673. a = """raise Exception(5) # foo"""
  674. self.check(b, a)
  675. b = """raise E, (5, 6) % (a, b) # foo"""
  676. a = """raise E((5, 6) % (a, b)) # foo"""
  677. self.check(b, a)
  678. b = """def foo():
  679. raise Exception, 5, 6 # foo"""
  680. a = """def foo():
  681. raise Exception(5).with_traceback(6) # foo"""
  682. self.check(b, a)
  683. def test_tuple_value(self):
  684. b = """raise Exception, (5, 6, 7)"""
  685. a = """raise Exception(5, 6, 7)"""
  686. self.check(b, a)
  687. def test_tuple_detection(self):
  688. b = """raise E, (5, 6) % (a, b)"""
  689. a = """raise E((5, 6) % (a, b))"""
  690. self.check(b, a)
  691. def test_tuple_exc_1(self):
  692. b = """raise (((E1, E2), E3), E4), V"""
  693. a = """raise E1(V)"""
  694. self.check(b, a)
  695. def test_tuple_exc_2(self):
  696. b = """raise (E1, (E2, E3), E4), V"""
  697. a = """raise E1(V)"""
  698. self.check(b, a)
  699. # These should produce a warning
  700. def test_string_exc(self):
  701. s = """raise 'foo'"""
  702. self.warns_unchanged(s, "Python 3 does not support string exceptions")
  703. def test_string_exc_val(self):
  704. s = """raise "foo", 5"""
  705. self.warns_unchanged(s, "Python 3 does not support string exceptions")
  706. def test_string_exc_val_tb(self):
  707. s = """raise "foo", 5, 6"""
  708. self.warns_unchanged(s, "Python 3 does not support string exceptions")
  709. # These should result in traceback-assignment
  710. def test_tb_1(self):
  711. b = """def foo():
  712. raise Exception, 5, 6"""
  713. a = """def foo():
  714. raise Exception(5).with_traceback(6)"""
  715. self.check(b, a)
  716. def test_tb_2(self):
  717. b = """def foo():
  718. a = 5
  719. raise Exception, 5, 6
  720. b = 6"""
  721. a = """def foo():
  722. a = 5
  723. raise Exception(5).with_traceback(6)
  724. b = 6"""
  725. self.check(b, a)
  726. def test_tb_3(self):
  727. b = """def foo():
  728. raise Exception,5,6"""
  729. a = """def foo():
  730. raise Exception(5).with_traceback(6)"""
  731. self.check(b, a)
  732. def test_tb_4(self):
  733. b = """def foo():
  734. a = 5
  735. raise Exception,5,6
  736. b = 6"""
  737. a = """def foo():
  738. a = 5
  739. raise Exception(5).with_traceback(6)
  740. b = 6"""
  741. self.check(b, a)
  742. def test_tb_5(self):
  743. b = """def foo():
  744. raise Exception, (5, 6, 7), 6"""
  745. a = """def foo():
  746. raise Exception(5, 6, 7).with_traceback(6)"""
  747. self.check(b, a)
  748. def test_tb_6(self):
  749. b = """def foo():
  750. a = 5
  751. raise Exception, (5, 6, 7), 6
  752. b = 6"""
  753. a = """def foo():
  754. a = 5
  755. raise Exception(5, 6, 7).with_traceback(6)
  756. b = 6"""
  757. self.check(b, a)
  758. class Test_throw(FixerTestCase):
  759. fixer = "throw"
  760. def test_1(self):
  761. b = """g.throw(Exception, 5)"""
  762. a = """g.throw(Exception(5))"""
  763. self.check(b, a)
  764. def test_2(self):
  765. b = """g.throw(Exception,5)"""
  766. a = """g.throw(Exception(5))"""
  767. self.check(b, a)
  768. def test_3(self):
  769. b = """g.throw(Exception, (5, 6, 7))"""
  770. a = """g.throw(Exception(5, 6, 7))"""
  771. self.check(b, a)
  772. def test_4(self):
  773. b = """5 + g.throw(Exception, 5)"""
  774. a = """5 + g.throw(Exception(5))"""
  775. self.check(b, a)
  776. # These should produce warnings
  777. def test_warn_1(self):
  778. s = """g.throw("foo")"""
  779. self.warns_unchanged(s, "Python 3 does not support string exceptions")
  780. def test_warn_2(self):
  781. s = """g.throw("foo", 5)"""
  782. self.warns_unchanged(s, "Python 3 does not support string exceptions")
  783. def test_warn_3(self):
  784. s = """g.throw("foo", 5, 6)"""
  785. self.warns_unchanged(s, "Python 3 does not support string exceptions")
  786. # These should not be touched
  787. def test_untouched_1(self):
  788. s = """g.throw(Exception)"""
  789. self.unchanged(s)
  790. def test_untouched_2(self):
  791. s = """g.throw(Exception(5, 6))"""
  792. self.unchanged(s)
  793. def test_untouched_3(self):
  794. s = """5 + g.throw(Exception(5, 6))"""
  795. self.unchanged(s)
  796. # These should result in traceback-assignment
  797. def test_tb_1(self):
  798. b = """def foo():
  799. g.throw(Exception, 5, 6)"""
  800. a = """def foo():
  801. g.throw(Exception(5).with_traceback(6))"""
  802. self.check(b, a)
  803. def test_tb_2(self):
  804. b = """def foo():
  805. a = 5
  806. g.throw(Exception, 5, 6)
  807. b = 6"""
  808. a = """def foo():
  809. a = 5
  810. g.throw(Exception(5).with_traceback(6))
  811. b = 6"""
  812. self.check(b, a)
  813. def test_tb_3(self):
  814. b = """def foo():
  815. g.throw(Exception,5,6)"""
  816. a = """def foo():
  817. g.throw(Exception(5).with_traceback(6))"""
  818. self.check(b, a)
  819. def test_tb_4(self):
  820. b = """def foo():
  821. a = 5
  822. g.throw(Exception,5,6)
  823. b = 6"""
  824. a = """def foo():
  825. a = 5
  826. g.throw(Exception(5).with_traceback(6))
  827. b = 6"""
  828. self.check(b, a)
  829. def test_tb_5(self):
  830. b = """def foo():
  831. g.throw(Exception, (5, 6, 7), 6)"""
  832. a = """def foo():
  833. g.throw(Exception(5, 6, 7).with_traceback(6))"""
  834. self.check(b, a)
  835. def test_tb_6(self):
  836. b = """def foo():
  837. a = 5
  838. g.throw(Exception, (5, 6, 7), 6)
  839. b = 6"""
  840. a = """def foo():
  841. a = 5
  842. g.throw(Exception(5, 6, 7).with_traceback(6))
  843. b = 6"""
  844. self.check(b, a)
  845. def test_tb_7(self):
  846. b = """def foo():
  847. a + g.throw(Exception, 5, 6)"""
  848. a = """def foo():
  849. a + g.throw(Exception(5).with_traceback(6))"""
  850. self.check(b, a)
  851. def test_tb_8(self):
  852. b = """def foo():
  853. a = 5
  854. a + g.throw(Exception, 5, 6)
  855. b = 6"""
  856. a = """def foo():
  857. a = 5
  858. a + g.throw(Exception(5).with_traceback(6))
  859. b = 6"""
  860. self.check(b, a)
  861. class Test_long(FixerTestCase):
  862. fixer = "long"
  863. def test_1(self):
  864. b = """x = long(x)"""
  865. a = """x = int(x)"""
  866. self.check(b, a)
  867. def test_2(self):
  868. b = """y = isinstance(x, long)"""
  869. a = """y = isinstance(x, int)"""
  870. self.check(b, a)
  871. def test_3(self):
  872. b = """z = type(x) in (int, long)"""
  873. a = """z = type(x) in (int, int)"""
  874. self.check(b, a)
  875. def test_unchanged(self):
  876. s = """long = True"""
  877. self.unchanged(s)
  878. s = """s.long = True"""
  879. self.unchanged(s)
  880. s = """def long(): pass"""
  881. self.unchanged(s)
  882. s = """class long(): pass"""
  883. self.unchanged(s)
  884. s = """def f(long): pass"""
  885. self.unchanged(s)
  886. s = """def f(g, long): pass"""
  887. self.unchanged(s)
  888. s = """def f(x, long=True): pass"""
  889. self.unchanged(s)
  890. def test_prefix_preservation(self):
  891. b = """x = long( x )"""
  892. a = """x = int( x )"""
  893. self.check(b, a)
  894. class Test_execfile(FixerTestCase):
  895. fixer = "execfile"
  896. def test_conversion(self):
  897. b = """execfile("fn")"""
  898. a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
  899. self.check(b, a)
  900. b = """execfile("fn", glob)"""
  901. a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
  902. self.check(b, a)
  903. b = """execfile("fn", glob, loc)"""
  904. a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
  905. self.check(b, a)
  906. b = """execfile("fn", globals=glob)"""
  907. a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
  908. self.check(b, a)
  909. b = """execfile("fn", locals=loc)"""
  910. a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
  911. self.check(b, a)
  912. b = """execfile("fn", globals=glob, locals=loc)"""
  913. a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
  914. self.check(b, a)
  915. def test_spacing(self):
  916. b = """execfile( "fn" )"""
  917. a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
  918. self.check(b, a)
  919. b = """execfile("fn", globals = glob)"""
  920. a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)"""
  921. self.check(b, a)
  922. class Test_isinstance(FixerTestCase):
  923. fixer = "isinstance"
  924. def test_remove_multiple_items(self):
  925. b = """isinstance(x, (int, int, int))"""
  926. a = """isinstance(x, int)"""
  927. self.check(b, a)
  928. b = """isinstance(x, (int, float, int, int, float))"""
  929. a = """isinstance(x, (int, float))"""
  930. self.check(b, a)
  931. b = """isinstance(x, (int, float, int, int, float, str))"""
  932. a = """isinstance(x, (int, float, str))"""
  933. self.check(b, a)
  934. b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
  935. a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
  936. self.check(b, a)
  937. def test_prefix_preservation(self):
  938. b = """if isinstance( foo(), ( bar, bar, baz )) : pass"""
  939. a = """if isinstance( foo(), ( bar, baz )) : pass"""
  940. self.check(b, a)
  941. def test_unchanged(self):
  942. self.unchanged("isinstance(x, (str, int))")
  943. class Test_dict(FixerTestCase):
  944. fixer = "dict"
  945. def test_prefix_preservation(self):
  946. b = "if d. keys ( ) : pass"
  947. a = "if list(d. keys ( )) : pass"
  948. self.check(b, a)
  949. b = "if d. items ( ) : pass"
  950. a = "if list(d. items ( )) : pass"
  951. self.check(b, a)
  952. b = "if d. iterkeys ( ) : pass"
  953. a = "if iter(d. keys ( )) : pass"
  954. self.check(b, a)
  955. b = "[i for i in d. iterkeys( ) ]"
  956. a = "[i for i in d. keys( ) ]"
  957. self.check(b, a)
  958. def test_trailing_comment(self):
  959. b = "d.keys() # foo"
  960. a = "list(d.keys()) # foo"
  961. self.check(b, a)
  962. b = "d.items() # foo"
  963. a = "list(d.items()) # foo"
  964. self.check(b, a)
  965. b = "d.iterkeys() # foo"
  966. a = "iter(d.keys()) # foo"
  967. self.check(b, a)
  968. b = """[i for i in d.iterkeys() # foo
  969. ]"""
  970. a = """[i for i in d.keys() # foo
  971. ]"""
  972. self.check(b, a)
  973. def test_unchanged(self):
  974. for wrapper in fixer_util.consuming_calls:
  975. s = "s = %s(d.keys())" % wrapper
  976. self.unchanged(s)
  977. s = "s = %s(d.values())" % wrapper
  978. self.unchanged(s)
  979. s = "s = %s(d.items())" % wrapper
  980. self.unchanged(s)
  981. def test_01(self):
  982. b = "d.keys()"
  983. a = "list(d.keys())"
  984. self.check(b, a)
  985. b = "a[0].foo().keys()"
  986. a = "list(a[0].foo().keys())"
  987. self.check(b, a)
  988. def test_02(self):
  989. b = "d.items()"
  990. a = "list(d.items())"
  991. self.check(b, a)
  992. def test_03(self):
  993. b = "d.values()"
  994. a = "list(d.values())"
  995. self.check(b, a)
  996. def test_04(self):
  997. b = "d.iterkeys()"
  998. a = "iter(d.keys())"
  999. self.check(b, a)
  1000. def test_05(self):
  1001. b = "d.iteritems()"
  1002. a = "iter(d.items())"
  1003. self.check(b, a)
  1004. def test_06(self):
  1005. b = "d.itervalues()"
  1006. a = "iter(d.values())"
  1007. self.check(b, a)
  1008. def test_07(self):
  1009. s = "list(d.keys())"
  1010. self.unchanged(s)
  1011. def test_08(self):
  1012. s = "sorted(d.keys())"
  1013. self.unchanged(s)
  1014. def test_09(self):
  1015. b = "iter(d.keys())"
  1016. a = "iter(list(d.keys()))"
  1017. self.check(b, a)
  1018. def test_10(self):
  1019. b = "foo(d.keys())"
  1020. a = "foo(list(d.keys()))"
  1021. self.check(b, a)
  1022. def test_11(self):
  1023. b = "for i in d.keys(): print i"
  1024. a = "for i in list(d.keys()): print i"
  1025. self.check(b, a)
  1026. def test_12(self):
  1027. b = "for i in d.iterkeys(): print i"
  1028. a = "for i in d.keys(): print i"
  1029. self.check(b, a)
  1030. def test_13(self):
  1031. b = "[i for i in d.keys()]"
  1032. a = "[i for i in list(d.keys())]"
  1033. self.check(b, a)
  1034. def test_14(self):
  1035. b = "[i for i in d.iterkeys()]"
  1036. a = "[i for i in d.keys()]"
  1037. self.check(b, a)
  1038. def test_15(self):
  1039. b = "(i for i in d.keys())"
  1040. a = "(i for i in list(d.keys()))"
  1041. self.check(b, a)
  1042. def test_16(self):
  1043. b = "(i for i in d.iterkeys())"
  1044. a = "(i for i in d.keys())"
  1045. self.check(b, a)
  1046. def test_17(self):
  1047. b = "iter(d.iterkeys())"
  1048. a = "iter(d.keys())"
  1049. self.check(b, a)
  1050. def test_18(self):
  1051. b = "list(d.iterkeys())"
  1052. a = "list(d.keys())"
  1053. self.check(b, a)
  1054. def test_19(self):
  1055. b = "sorted(d.iterkeys())"
  1056. a = "sorted(d.keys())"
  1057. self.check(b, a)
  1058. def test_20(self):
  1059. b = "foo(d.iterkeys())"
  1060. a = "foo(iter(d.keys()))"
  1061. self.check(b, a)
  1062. def test_21(self):
  1063. b = "print h.iterkeys().next()"
  1064. a = "print iter(h.keys()).next()"
  1065. self.check(b, a)
  1066. def test_22(self):
  1067. b = "print h.keys()[0]"
  1068. a = "print list(h.keys())[0]"
  1069. self.check(b, a)
  1070. def test_23(self):
  1071. b = "print list(h.iterkeys().next())"
  1072. a = "print list(iter(h.keys()).next())"
  1073. self.check(b, a)
  1074. def test_24(self):
  1075. b = "for x in h.keys()[0]: print x"
  1076. a = "for x in list(h.keys())[0]: print x"
  1077. self.check(b, a)
  1078. class Test_xrange(FixerTestCase):
  1079. fixer = "xrange"
  1080. def test_prefix_preservation(self):
  1081. b = """x = xrange( 10 )"""
  1082. a = """x = range( 10 )"""
  1083. self.check(b, a)
  1084. b = """x = xrange( 1 , 10 )"""
  1085. a = """x = range( 1 , 10 )"""
  1086. self.check(b, a)
  1087. b = """x = xrange( 0 , 10 , 2 )"""
  1088. a = """x = range( 0 , 10 , 2 )"""
  1089. self.check(b, a)
  1090. def test_single_arg(self):
  1091. b = """x = xrange(10)"""
  1092. a = """x = range(10)"""
  1093. self.check(b, a)
  1094. def test_two_args(self):
  1095. b = """x = xrange(1, 10)"""
  1096. a = """x = range(1, 10)"""
  1097. self.check(b, a)
  1098. def test_three_args(self):
  1099. b = """x = xrange(0, 10, 2)"""
  1100. a = """x = range(0, 10, 2)"""
  1101. self.check(b, a)
  1102. def test_wrap_in_list(self):
  1103. b = """x = range(10, 3, 9)"""
  1104. a = """x = list(range(10, 3, 9))"""
  1105. self.check(b, a)
  1106. b = """x = foo(range(10, 3, 9))"""
  1107. a = """x = foo(list(range(10, 3, 9)))"""
  1108. self.check(b, a)
  1109. b = """x = range(10, 3, 9) + [4]"""
  1110. a = """x = list(range(10, 3, 9)) + [4]"""
  1111. self.check(b, a)
  1112. b = """x = range(10)[::-1]"""
  1113. a = """x = list(range(10))[::-1]"""
  1114. self.check(b, a)
  1115. b = """x = range(10) [3]"""
  1116. a = """x = list(range(10)) [3]"""
  1117. self.check(b, a)
  1118. def test_xrange_in_for(self):
  1119. b = """for i in xrange(10):\n j=i"""
  1120. a = """for i in range(10):\n j=i"""
  1121. self.check(b, a)
  1122. b = """[i for i in xrange(10)]"""
  1123. a = """[i for i in range(10)]"""
  1124. self.check(b, a)
  1125. def test_range_in_for(self):
  1126. self.unchanged("for i in range(10): pass")
  1127. self.unchanged("[i for i in range(10)]")
  1128. def test_in_contains_test(self):
  1129. self.unchanged("x in range(10, 3, 9)")
  1130. def test_in_consuming_context(self):
  1131. for call in fixer_util.consuming_calls:
  1132. self.unchanged("a = %s(range(10))" % call)
  1133. class Test_raw_input(FixerTestCase):
  1134. fixer = "raw_input"
  1135. def test_prefix_preservation(self):
  1136. b = """x = raw_input( )"""
  1137. a = """x = input( )"""
  1138. self.check(b, a)
  1139. b = """x = raw_input( '' )"""
  1140. a = """x = input( '' )"""
  1141. self.check(b, a)
  1142. def test_1(self):
  1143. b = """x = raw_input()"""
  1144. a = """x = input()"""
  1145. self.check(b, a)
  1146. def test_2(self):
  1147. b = """x = raw_input('')"""
  1148. a = """x = input('')"""
  1149. self.check(b, a)
  1150. def test_3(self):
  1151. b = """x = raw_input('prompt')"""
  1152. a = """x = input('prompt')"""
  1153. self.check(b, a)
  1154. def test_4(self):
  1155. b = """x = raw_input(foo(a) + 6)"""
  1156. a = """x = input(foo(a) + 6)"""
  1157. self.check(b, a)
  1158. def test_5(self):
  1159. b = """x = raw_input(invite).split()"""
  1160. a = """x = input(invite).split()"""
  1161. self.check(b, a)
  1162. def test_6(self):
  1163. b = """x = raw_input(invite) . split ()"""
  1164. a = """x = input(invite) . split ()"""
  1165. self.check(b, a)
  1166. def test_8(self):
  1167. b = "x = int(raw_input())"
  1168. a = "x = int(input())"
  1169. self.check(b, a)
  1170. class Test_funcattrs(FixerTestCase):
  1171. fixer = "funcattrs"
  1172. attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
  1173. def test(self):
  1174. for attr in self.attrs:
  1175. b = "a.func_%s" % attr
  1176. a = "a.__%s__" % attr
  1177. self.check(b, a)
  1178. b = "self.foo.func_%s.foo_bar" % attr
  1179. a = "self.foo.__%s__.foo_bar" % attr
  1180. self.check(b, a)
  1181. def test_unchanged(self):
  1182. for attr in self.attrs:
  1183. s = "foo(func_%s + 5)" % attr
  1184. self.unchanged(s)
  1185. s = "f(foo.__%s__)" % attr
  1186. self.unchanged(s)
  1187. s = "f(foo.__%s__.foo)" % attr
  1188. self.unchanged(s)
  1189. class Test_xreadlines(FixerTestCase):
  1190. fixer = "xreadlines"
  1191. def test_call(self):
  1192. b = "for x in f.xreadlines(): pass"
  1193. a = "for x in f: pass"
  1194. self.check(b, a)
  1195. b = "for x in foo().xreadlines(): pass"
  1196. a = "for x in foo(): pass"
  1197. self.check(b, a)
  1198. b = "for x in (5 + foo()).xreadlines(): pass"
  1199. a = "for x in (5 + foo()): pass"
  1200. self.check(b, a)
  1201. def test_attr_ref(self):
  1202. b = "foo(f.xreadlines + 5)"
  1203. a = "foo(f.__iter__ + 5)"
  1204. self.check(b, a)
  1205. b = "foo(f().xreadlines + 5)"
  1206. a = "foo(f().__iter__ + 5)"
  1207. self.check(b, a)
  1208. b = "foo((5 + f()).xreadlines + 5)"
  1209. a = "foo((5 + f()).__iter__ + 5)"
  1210. self.check(b, a)
  1211. def test_unchanged(self):
  1212. s = "for x in f.xreadlines(5): pass"
  1213. self.unchanged(s)
  1214. s = "for x in f.xreadlines(k=5): pass"
  1215. self.unchanged(s)
  1216. s = "for x in f.xreadlines(*k, **v): pass"
  1217. self.unchanged(s)
  1218. s = "foo(xreadlines)"
  1219. self.unchanged(s)
  1220. class ImportsFixerTests:
  1221. def test_import_module(self):
  1222. for old, new in self.modules.items():
  1223. b = "import %s" % old
  1224. a = "import %s" % new
  1225. self.check(b, a)
  1226. b = "import foo, %s, bar" % old
  1227. a = "import foo, %s, bar" % new
  1228. self.check(b, a)
  1229. def test_import_from(self):
  1230. for old, new in self.modules.items():
  1231. b = "from %s import foo" % old
  1232. a = "from %s import foo" % new
  1233. self.check(b, a)
  1234. b = "from %s import foo, bar" % old
  1235. a = "from %s import foo, bar" % new
  1236. self.check(b, a)
  1237. b = "from %s import (yes, no)" % old
  1238. a = "from %s import (yes, no)" % new
  1239. self.check(b, a)
  1240. def test_import_module_as(self):
  1241. for old, new in self.modules.items():
  1242. b = "import %s as foo_bar" % old
  1243. a = "import %s as foo_bar" % new
  1244. self.check(b, a)
  1245. b = "import %s as foo_bar" % old
  1246. a = "import %s as foo_bar" % new
  1247. self.check(b, a)
  1248. def test_import_from_as(self):
  1249. for old, new in self.modules.items():
  1250. b = "from %s import foo as bar" % old
  1251. a = "from %s import foo as bar" % new
  1252. self.check(b, a)
  1253. def test_star(self):
  1254. for old, new in self.modules.items():
  1255. b = "from %s import *" % old
  1256. a = "from %s import *" % new
  1257. self.check(b, a)
  1258. def test_import_module_usage(self):
  1259. for old, new in self.modules.items():
  1260. b = """
  1261. import %s
  1262. foo(%s.bar)
  1263. """ % (old, old)
  1264. a = """
  1265. import %s
  1266. foo(%s.bar)
  1267. """ % (new, new)
  1268. self.check(b, a)
  1269. b = """
  1270. from %s import x
  1271. %s = 23
  1272. """ % (old, old)
  1273. a = """
  1274. from %s import x
  1275. %s = 23
  1276. """ % (new, old)
  1277. self.check(b, a)
  1278. s = """
  1279. def f():
  1280. %s.method()
  1281. """ % (old,)
  1282. self.unchanged(s)
  1283. # test nested usage
  1284. b = """
  1285. import %s
  1286. %s.bar(%s.foo)
  1287. """ % (old, old, old)
  1288. a = """
  1289. import %s
  1290. %s.bar(%s.foo)
  1291. """ % (new, new, new)
  1292. self.check(b, a)
  1293. b = """
  1294. import %s
  1295. x.%s
  1296. """ % (old, old)
  1297. a = """
  1298. import %s
  1299. x.%s
  1300. """ % (new, old)
  1301. self.check(b, a)
  1302. class Test_imports(FixerTestCase, ImportsFixerTests):
  1303. fixer = "imports"
  1304. from ..fixes.fix_imports import MAPPING as modules
  1305. def test_multiple_imports(self):
  1306. b = """import urlparse, cStringIO"""
  1307. a = """import urllib.parse, io"""
  1308. self.check(b, a)
  1309. def test_multiple_imports_as(self):
  1310. b = """
  1311. import copy_reg as bar, HTMLParser as foo, urlparse
  1312. s = urlparse.spam(bar.foo())
  1313. """
  1314. a = """
  1315. import copyreg as bar, html.parser as foo, urllib.parse
  1316. s = urllib.parse.spam(bar.foo())
  1317. """
  1318. self.check(b, a)
  1319. class Test_imports2(FixerTestCase, ImportsFixerTests):
  1320. fixer = "imports2"
  1321. from ..fixes.fix_imports2 import MAPPING as modules
  1322. class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
  1323. def setUp(self):
  1324. super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
  1325. from ..fixes.fix_imports2 import MAPPING as mapping2
  1326. self.modules = mapping2.copy()
  1327. from ..fixes.fix_imports import MAPPING as mapping1
  1328. for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
  1329. self.modules[key] = mapping1[key]
  1330. class Test_urllib(FixerTestCase):
  1331. fixer = "urllib"
  1332. from ..fixes.fix_urllib import MAPPING as modules
  1333. def test_import_module(self):
  1334. for old, changes in self.modules.items():
  1335. b = "import %s" % old
  1336. a = "import %s" % ", ".join(map(itemgetter(0), changes))
  1337. self.check(b, a)
  1338. def test_import_from(self):
  1339. for old, changes in self.modules.items():
  1340. all_members = []
  1341. for new, members in changes:
  1342. for member in members:
  1343. all_members.append(member)
  1344. b = "from %s import %s" % (old, member)
  1345. a = "from %s import %s" % (new, member)
  1346. self.check(b, a)
  1347. s = "from foo import %s" % member
  1348. self.unchanged(s)
  1349. b = "from %s import %s" % (old, ", ".join(members))
  1350. a = "from %s import %s" % (new, ", ".join(members))
  1351. self.check(b, a)
  1352. s = "from foo import %s" % ", ".join(members)
  1353. self.unchanged(s)
  1354. # test the breaking of a module into multiple replacements
  1355. b = "from %s import %s" % (old, ", ".join(all_members))
  1356. a = "\n".join(["from %s import %s" % (new, ", ".join(members))
  1357. for (new, members) in changes])
  1358. self.check(b, a)
  1359. def test_import_module_as(self):
  1360. for old in self.modules:
  1361. s = "import %s as foo" % old
  1362. self.warns_unchanged(s, "This module is now multiple modules")
  1363. def test_import_from_as(self):
  1364. for old, changes in self.modules.items():
  1365. for new, members in changes:
  1366. for member in members:
  1367. b = "from %s import %s as foo_bar" % (old, member)
  1368. a = "from %s import %s as foo_bar" % (new, member)
  1369. self.check(b, a)
  1370. def test_star(self):
  1371. for old in self.modules:
  1372. s = "from %s import *" % old
  1373. self.warns_unchanged(s, "Cannot handle star imports")
  1374. def test_import_module_usage(self):
  1375. for old, changes in self.modules.items():
  1376. for new, members in changes:
  1377. for member in members:
  1378. b = """
  1379. import %s
  1380. foo(%s.%s)
  1381. """ % (old, old, member)
  1382. a = """
  1383. import %s
  1384. foo(%s.%s)
  1385. """ % (", ".join([n for (n, mems)
  1386. in self.modules[old]]),
  1387. new, member)
  1388. self.check(b, a)
  1389. class Test_input(FixerTestCase):
  1390. fixer = "input"
  1391. def test_prefix_preservation(self):
  1392. b = """x = input( )"""
  1393. a = """x = eval(input( ))"""
  1394. self.check(b, a)
  1395. b = """x = input( '' )"""
  1396. a = """x = eval(input( '' ))"""
  1397. self.check(b, a)
  1398. def test_trailing_comment(self):
  1399. b = """x = input() # foo"""
  1400. a = """x = eval(input()) # foo"""
  1401. self.check(b, a)
  1402. def test_idempotency(self):
  1403. s = """x = eval(input())"""
  1404. self.unchanged(s)
  1405. s = """x = eval(input(''))"""
  1406. self.unchanged(s)
  1407. s = """x = eval(input(foo(5) + 9))"""
  1408. self.unchanged(s)
  1409. def test_1(self):
  1410. b = """x = input()"""
  1411. a = """x = eval(input())"""
  1412. self.check(b, a)
  1413. def test_2(self):
  1414. b = """x = input('')"""
  1415. a = """x = eval(input(''))"""
  1416. self.check(b, a)
  1417. def test_3(self):
  1418. b = """x = input('prompt')"""
  1419. a = """x = eval(input('prompt'))"""
  1420. self.check(b, a)
  1421. def test_4(self):
  1422. b = """x = input(foo(5) + 9)"""
  1423. a = """x = eval(input(foo(5) + 9))"""
  1424. self.check(b, a)
  1425. class Test_tuple_params(FixerTestCase):
  1426. fixer = "tuple_params"
  1427. def test_unchanged_1(self):
  1428. s = """def foo(): pass"""
  1429. self.unchanged(s)
  1430. def test_unchanged_2(self):
  1431. s = """def foo(a, b, c): pass"""
  1432. self.unchanged(s)
  1433. def test_unchanged_3(self):
  1434. s = """def foo(a=3, b=4, c=5): pass"""
  1435. self.unchanged(s)
  1436. def test_1(self):
  1437. b = """
  1438. def foo(((a, b), c)):
  1439. x = 5"""
  1440. a = """
  1441. def foo(xxx_todo_changeme):
  1442. ((a, b), c) = xxx_todo_changeme
  1443. x = 5"""
  1444. self.check(b, a)
  1445. def test_2(self):
  1446. b = """
  1447. def foo(((a, b), c), d):
  1448. x = 5"""
  1449. a = """
  1450. def foo(xxx_todo_changeme, d):
  1451. ((a, b), c) = xxx_todo_changeme
  1452. x = 5"""
  1453. self.check(b, a)
  1454. def test_3(self):
  1455. b = """
  1456. def foo(((a, b), c), d) -> e:
  1457. x = 5"""
  1458. a = """
  1459. def foo(xxx_todo_changeme, d) -> e:
  1460. ((a, b), c) = xxx_todo_changeme
  1461. x = 5"""
  1462. self.check(b, a)
  1463. def test_semicolon(self):
  1464. b = """
  1465. def foo(((a, b), c)): x = 5; y = 7"""
  1466. a = """
  1467. def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
  1468. self.check(b, a)
  1469. def test_keywords(self):
  1470. b = """
  1471. def foo(((a, b), c), d, e=5) -> z:
  1472. x = 5"""
  1473. a = """
  1474. def foo(xxx_todo_changeme, d, e=5) -> z:
  1475. ((a, b), c) = xxx_todo_changeme
  1476. x = 5"""
  1477. self.check(b, a)
  1478. def test_varargs(self):
  1479. b = """
  1480. def foo(((a, b), c), d, *vargs, **kwargs) -> z:
  1481. x = 5"""
  1482. a = """
  1483. def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z:
  1484. ((a, b), c) = xxx_todo_changeme
  1485. x = 5"""
  1486. self.check(b, a)
  1487. def test_multi_1(self):
  1488. b = """
  1489. def foo(((a, b), c), (d, e, f)) -> z:
  1490. x = 5"""
  1491. a = """
  1492. def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
  1493. ((a, b), c) = xxx_todo_changeme
  1494. (d, e, f) = xxx_todo_changeme1
  1495. x = 5"""
  1496. self.check(b, a)
  1497. def test_multi_2(self):
  1498. b = """
  1499. def foo(x, ((a, b), c), d, (e, f, g), y) -> z:
  1500. x = 5"""
  1501. a = """
  1502. def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z:
  1503. ((a, b), c) = xxx_todo_changeme
  1504. (e, f, g) = xxx_todo_changeme1
  1505. x = 5"""
  1506. self.check(b, a)
  1507. def test_docstring(self):
  1508. b = """
  1509. def foo(((a, b), c), (d, e, f)) -> z:
  1510. "foo foo foo foo"
  1511. x = 5"""
  1512. a = """
  1513. def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
  1514. "foo foo foo foo"
  1515. ((a, b), c) = xxx_todo_changeme
  1516. (d, e, f) = xxx_todo_changeme1
  1517. x = 5"""
  1518. self.check(b, a)
  1519. def test_lambda_no_change(self):
  1520. s = """lambda x: x + 5"""
  1521. self.unchanged(s)
  1522. def test_lambda_parens_single_arg(self):
  1523. b = """lambda (x): x + 5"""
  1524. a = """lambda x: x + 5"""
  1525. self.check(b, a)
  1526. b = """lambda(x): x + 5"""
  1527. a = """lambda x: x + 5"""
  1528. self.check(b, a)
  1529. b = """lambda ((((x)))): x + 5"""
  1530. a = """lambda x: x + 5"""
  1531. self.check(b, a)
  1532. b = """lambda((((x)))): x + 5"""
  1533. a = """lambda x: x + 5"""
  1534. self.check(b, a)
  1535. def test_lambda_simple(self):
  1536. b = """lambda (x, y): x + f(y)"""
  1537. a = """lambda x_y: x_y[0] + f(x_y[1])"""
  1538. self.check(b, a)
  1539. b = """lambda(x, y): x + f(y)"""
  1540. a = """lambda x_y: x_y[0] + f(x_y[1])"""
  1541. self.check(b, a)
  1542. b = """lambda (((x, y))): x + f(y)"""
  1543. a = """lambda x_y: x_y[0] + f(x_y[1])"""
  1544. self.check(b, a)
  1545. b = """lambda(((x, y))): x + f(y)"""
  1546. a = """lambda x_y: x_y[0] + f(x_y[1])"""
  1547. self.check(b, a)
  1548. def test_lambda_one_tuple(self):
  1549. b = """lambda (x,): x + f(x)"""
  1550. a = """lambda x1: x1[0] + f(x1[0])"""
  1551. self.check(b, a)
  1552. b = """lambda (((x,))): x + f(x)"""
  1553. a = """lambda x1: x1[0] + f(x1[0])"""
  1554. self.check(b, a)
  1555. def test_lambda_simple_multi_use(self):
  1556. b = """lambda (x, y): x + x + f(x) + x"""
  1557. a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]"""
  1558. self.check(b, a)
  1559. def test_lambda_simple_reverse(self):
  1560. b = """lambda (x, y): y + x"""
  1561. a = """lambda x_y: x_y[1] + x_y[0]"""
  1562. self.check(b, a)
  1563. def test_lambda_nested(self):
  1564. b = """lambda (x, (y, z)): x + y + z"""
  1565. a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
  1566. self.check(b, a)
  1567. b = """lambda (((x, (y, z)))): x + y + z"""
  1568. a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
  1569. self.check(b, a)
  1570. def test_lambda_nested_multi_use(self):
  1571. b = """lambda (x, (y, z)): x + y + f(y)"""
  1572. a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])"""
  1573. self.check(b, a)
  1574. class Test_methodattrs(FixerTestCase):
  1575. fixer = "methodattrs"
  1576. attrs = ["func", "self", "class"]
  1577. def test(self):
  1578. for attr in self.attrs:
  1579. b = "a.im_%s" % attr
  1580. if attr == "class":
  1581. a = "a.__self__.__class__"
  1582. else:
  1583. a = "a.__%s__" % attr
  1584. self.check(b, a)
  1585. b = "self.foo.im_%s.foo_bar" % attr
  1586. if attr == "class":
  1587. a = "self.foo.__self__.__class__.foo_bar"
  1588. else:
  1589. a = "self.foo.__%s__.foo_bar" % attr
  1590. self.check(b, a)
  1591. def test_unchanged(self):
  1592. for attr in self.attrs:
  1593. s = "foo(im_%s + 5)" % attr
  1594. self.unchanged(s)
  1595. s = "f(foo.__%s__)" % attr
  1596. self.unchanged(s)
  1597. s = "f(foo.__%s__.foo)" % attr
  1598. self.unchanged(s)
  1599. class Test_next(FixerTestCase):
  1600. fixer = "next"
  1601. def test_1(self):
  1602. b = """it.next()"""
  1603. a = """next(it)"""
  1604. self.check(b, a)
  1605. def test_2(self):
  1606. b = """a.b.c.d.next()"""
  1607. a = """next(a.b.c.d)"""
  1608. self.check(b, a)
  1609. def test_3(self):
  1610. b = """(a + b).next()"""
  1611. a = """next((a + b))"""
  1612. self.check(b, a)
  1613. def test_4(self):
  1614. b = """a().next()"""
  1615. a = """next(a())"""
  1616. self.check(b, a)
  1617. def test_5(self):
  1618. b = """a().next() + b"""
  1619. a = """next(a()) + b"""
  1620. self.check(b, a)
  1621. def test_6(self):
  1622. b = """c( a().next() + b)"""
  1623. a = """c( next(a()) + b)"""
  1624. self.check(b, a)
  1625. def test_prefix_preservation_1(self):
  1626. b = """
  1627. for a in b:
  1628. foo(a)
  1629. a.next()
  1630. """
  1631. a = """
  1632. for a in b:
  1633. foo(a)
  1634. next(a)
  1635. """
  1636. self.check(b, a)
  1637. def test_prefix_preservation_2(self):
  1638. b = """
  1639. for a in b:
  1640. foo(a) # abc
  1641. # def
  1642. a.next()
  1643. """
  1644. a = """
  1645. for a in b:
  1646. foo(a) # abc
  1647. # def
  1648. next(a)
  1649. """
  1650. self.check(b, a)
  1651. def test_prefix_preservation_3(self):
  1652. b = """
  1653. next = 5
  1654. for a in b:
  1655. foo(a)
  1656. a.next()
  1657. """
  1658. a = """
  1659. next = 5
  1660. for a in b:
  1661. foo(a)
  1662. a.__next__()
  1663. """
  1664. self.check(b, a, ignore_warnings=True)
  1665. def test_prefix_preservation_4(self):
  1666. b = """
  1667. next = 5
  1668. for a in b:
  1669. foo(a) # abc
  1670. # def
  1671. a.next()
  1672. """
  1673. a = """
  1674. next = 5
  1675. for a in b:
  1676. foo(a) # abc
  1677. # def
  1678. a.__next__()
  1679. """
  1680. self.check(b, a, ignore_warnings=True)
  1681. def test_prefix_preservation_5(self):
  1682. b = """
  1683. next = 5
  1684. for a in b:
  1685. foo(foo(a), # abc
  1686. a.next())
  1687. """
  1688. a = """
  1689. next = 5
  1690. for a in b:
  1691. foo(foo(a), # abc
  1692. a.__next__())
  1693. """
  1694. self.check(b, a, ignore_warnings=True)
  1695. def test_prefix_preservation_6(self):
  1696. b = """
  1697. for a in b:
  1698. foo(foo(a), # abc
  1699. a.next())
  1700. """
  1701. a = """
  1702. for a in b:
  1703. foo(foo(a), # abc
  1704. next(a))
  1705. """
  1706. self.check(b, a)
  1707. def test_method_1(self):
  1708. b = """
  1709. class A:
  1710. def next(self):
  1711. pass
  1712. """
  1713. a = """
  1714. class A:
  1715. def __next__(self):
  1716. pass
  1717. """
  1718. self.check(b, a)
  1719. def test_method_2(self):
  1720. b = """
  1721. class A(object):
  1722. def next(self):
  1723. pass
  1724. """
  1725. a = """
  1726. class A(object):
  1727. def __next__(self):
  1728. pass
  1729. """
  1730. self.check(b, a)
  1731. def test_method_3(self):
  1732. b = """
  1733. class A:
  1734. def next(x):
  1735. pass
  1736. """
  1737. a = """
  1738. class A:
  1739. def __next__(x):
  1740. pass
  1741. """
  1742. self.check(b, a)
  1743. def test_method_4(self):
  1744. b = """
  1745. class A:
  1746. def __init__(self, foo):
  1747. self.foo = foo
  1748. def next(self):
  1749. pass
  1750. def __iter__(self):
  1751. return self
  1752. """
  1753. a = """
  1754. class A:
  1755. def __init__(self, foo):
  1756. self.foo = foo
  1757. def __next__(self):
  1758. pass
  1759. def __iter__(self):
  1760. return self
  1761. """
  1762. self.check(b, a)
  1763. def test_method_unchanged(self):
  1764. s = """
  1765. class A:
  1766. def next(self, a, b):
  1767. pass
  1768. """
  1769. self.unchanged(s)
  1770. def test_shadowing_assign_simple(self):
  1771. s = """
  1772. next = foo
  1773. class A:
  1774. def next(self, a, b):
  1775. pass
  1776. """
  1777. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1778. def test_shadowing_assign_tuple_1(self):
  1779. s = """
  1780. (next, a) = foo
  1781. class A:
  1782. def next(self, a, b):
  1783. pass
  1784. """
  1785. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1786. def test_shadowing_assign_tuple_2(self):
  1787. s = """
  1788. (a, (b, (next, c)), a) = foo
  1789. class A:
  1790. def next(self, a, b):
  1791. pass
  1792. """
  1793. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1794. def test_shadowing_assign_list_1(self):
  1795. s = """
  1796. [next, a] = foo
  1797. class A:
  1798. def next(self, a, b):
  1799. pass
  1800. """
  1801. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1802. def test_shadowing_assign_list_2(self):
  1803. s = """
  1804. [a, [b, [next, c]], a] = foo
  1805. class A:
  1806. def next(self, a, b):
  1807. pass
  1808. """
  1809. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1810. def test_builtin_assign(self):
  1811. s = """
  1812. def foo():
  1813. __builtin__.next = foo
  1814. class A:
  1815. def next(self, a, b):
  1816. pass
  1817. """
  1818. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1819. def test_builtin_assign_in_tuple(self):
  1820. s = """
  1821. def foo():
  1822. (a, __builtin__.next) = foo
  1823. class A:
  1824. def next(self, a, b):
  1825. pass
  1826. """
  1827. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1828. def test_builtin_assign_in_list(self):
  1829. s = """
  1830. def foo():
  1831. [a, __builtin__.next] = foo
  1832. class A:
  1833. def next(self, a, b):
  1834. pass
  1835. """
  1836. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1837. def test_assign_to_next(self):
  1838. s = """
  1839. def foo():
  1840. A.next = foo
  1841. class A:
  1842. def next(self, a, b):
  1843. pass
  1844. """
  1845. self.unchanged(s)
  1846. def test_assign_to_next_in_tuple(self):
  1847. s = """
  1848. def foo():
  1849. (a, A.next) = foo
  1850. class A:
  1851. def next(self, a, b):
  1852. pass
  1853. """
  1854. self.unchanged(s)
  1855. def test_assign_to_next_in_list(self):
  1856. s = """
  1857. def foo():
  1858. [a, A.next] = foo
  1859. class A:
  1860. def next(self, a, b):
  1861. pass
  1862. """
  1863. self.unchanged(s)
  1864. def test_shadowing_import_1(self):
  1865. s = """
  1866. import foo.bar as next
  1867. class A:
  1868. def next(self, a, b):
  1869. pass
  1870. """
  1871. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1872. def test_shadowing_import_2(self):
  1873. s = """
  1874. import bar, bar.foo as next
  1875. class A:
  1876. def next(self, a, b):
  1877. pass
  1878. """
  1879. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1880. def test_shadowing_import_3(self):
  1881. s = """
  1882. import bar, bar.foo as next, baz
  1883. class A:
  1884. def next(self, a, b):
  1885. pass
  1886. """
  1887. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1888. def test_shadowing_import_from_1(self):
  1889. s = """
  1890. from x import next
  1891. class A:
  1892. def next(self, a, b):
  1893. pass
  1894. """
  1895. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1896. def test_shadowing_import_from_2(self):
  1897. s = """
  1898. from x.a import next
  1899. class A:
  1900. def next(self, a, b):
  1901. pass
  1902. """
  1903. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1904. def test_shadowing_import_from_3(self):
  1905. s = """
  1906. from x import a, next, b
  1907. class A:
  1908. def next(self, a, b):
  1909. pass
  1910. """
  1911. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1912. def test_shadowing_import_from_4(self):
  1913. s = """
  1914. from x.a import a, next, b
  1915. class A:
  1916. def next(self, a, b):
  1917. pass
  1918. """
  1919. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1920. def test_shadowing_funcdef_1(self):
  1921. s = """
  1922. def next(a):
  1923. pass
  1924. class A:
  1925. def next(self, a, b):
  1926. pass
  1927. """
  1928. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1929. def test_shadowing_funcdef_2(self):
  1930. b = """
  1931. def next(a):
  1932. pass
  1933. class A:
  1934. def next(self):
  1935. pass
  1936. it.next()
  1937. """
  1938. a = """
  1939. def next(a):
  1940. pass
  1941. class A:
  1942. def __next__(self):
  1943. pass
  1944. it.__next__()
  1945. """
  1946. self.warns(b, a, "Calls to builtin next() possibly shadowed")
  1947. def test_shadowing_global_1(self):
  1948. s = """
  1949. def f():
  1950. global next
  1951. next = 5
  1952. """
  1953. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1954. def test_shadowing_global_2(self):
  1955. s = """
  1956. def f():
  1957. global a, next, b
  1958. next = 5
  1959. """
  1960. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1961. def test_shadowing_for_simple(self):
  1962. s = """
  1963. for next in it():
  1964. pass
  1965. b = 5
  1966. c = 6
  1967. """
  1968. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1969. def test_shadowing_for_tuple_1(self):
  1970. s = """
  1971. for next, b in it():
  1972. pass
  1973. b = 5
  1974. c = 6
  1975. """
  1976. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1977. def test_shadowing_for_tuple_2(self):
  1978. s = """
  1979. for a, (next, c), b in it():
  1980. pass
  1981. b = 5
  1982. c = 6
  1983. """
  1984. self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
  1985. def test_noncall_access_1(self):
  1986. b = """gnext = g.next"""
  1987. a = """gnext = g.__next__"""
  1988. self.check(b, a)
  1989. def test_noncall_access_2(self):
  1990. b = """f(g.next + 5)"""
  1991. a = """f(g.__next__ + 5)"""
  1992. self.check(b, a)
  1993. def test_noncall_access_3(self):
  1994. b = """f(g().next + 5)"""
  1995. a = """f(g().__next__ + 5)"""
  1996. self.check(b, a)
  1997. class Test_nonzero(FixerTestCase):
  1998. fixer = "nonzero"
  1999. def test_1(self):
  2000. b = """
  2001. class A:
  2002. def __nonzero__(self):
  2003. pass
  2004. """
  2005. a = """
  2006. class A:
  2007. def __bool__(self):
  2008. pass
  2009. """
  2010. self.check(b, a)
  2011. def test_2(self):
  2012. b = """
  2013. class A(object):
  2014. def __nonzero__(self):
  2015. pass
  2016. """
  2017. a = """
  2018. class A(object):
  2019. def __bool__(self):
  2020. pass
  2021. """
  2022. self.check(b, a)
  2023. def test_unchanged_1(self):
  2024. s = """
  2025. class A(object):
  2026. def __bool__(self):
  2027. pass
  2028. """
  2029. self.unchanged(s)
  2030. def test_unchanged_2(self):
  2031. s = """
  2032. class A(object):
  2033. def __nonzero__(self, a):
  2034. pass
  2035. """
  2036. self.unchanged(s)
  2037. def test_unchanged_func(self):
  2038. s = """
  2039. def __nonzero__(self):
  2040. pass
  2041. """
  2042. self.unchanged(s)
  2043. class Test_numliterals(FixerTestCase):
  2044. fixer = "numliterals"
  2045. def test_octal_1(self):
  2046. b = """0755"""
  2047. a = """0o755"""
  2048. self.check(b, a)
  2049. def test_long_int_1(self):
  2050. b = """a = 12L"""
  2051. a = """a = 12"""
  2052. self.check(b, a)
  2053. def test_long_int_2(self):
  2054. b = """a = 12l"""
  2055. a = """a = 12"""
  2056. self.check(b, a)
  2057. def test_long_hex(self):
  2058. b = """b = 0x12l"""
  2059. a = """b = 0x12"""
  2060. self.check(b, a)
  2061. def test_comments_and_spacing(self):
  2062. b = """b = 0x12L"""
  2063. a = """b = 0x12"""
  2064. self.check(b, a)
  2065. b = """b = 0755 # spam"""
  2066. a = """b = 0o755 # spam"""
  2067. self.check(b, a)
  2068. def test_unchanged_int(self):
  2069. s = """5"""
  2070. self.unchanged(s)
  2071. def test_unchanged_float(self):
  2072. s = """5.0"""
  2073. self.unchanged(s)
  2074. def test_unchanged_octal(self):
  2075. s = """0o755"""
  2076. self.unchanged(s)
  2077. def test_unchanged_hex(self):
  2078. s = """0xABC"""
  2079. self.unchanged(s)
  2080. def test_unchanged_exp(self):
  2081. s = """5.0e10"""
  2082. self.unchanged(s)
  2083. def test_unchanged_complex_int(self):
  2084. s = """5 + 4j"""
  2085. self.unchanged(s)
  2086. def test_unchanged_complex_float(self):
  2087. s = """5.4 + 4.9j"""
  2088. self.unchanged(s)
  2089. def test_unchanged_complex_bare(self):
  2090. s = """4j"""
  2091. self.unchanged(s)
  2092. s = """4.4j"""
  2093. self.unchanged(s)
  2094. class Test_renames(FixerTestCase):
  2095. fixer = "renames"
  2096. modules = {"sys": ("maxint", "maxsize"),
  2097. }
  2098. def test_import_from(self):
  2099. for mod, (old, new) in self.modules.items():
  2100. b = "from %s import %s" % (mod, old)
  2101. a = "from %s import %s" % (mod, new)
  2102. self.check(b, a)
  2103. s = "from foo import %s" % old
  2104. self.unchanged(s)
  2105. def test_import_from_as(self):
  2106. for mod, (old, new) in self.modules.items():
  2107. b = "from %s import %s as foo_bar" % (mod, old)
  2108. a = "from %s import %s as foo_bar" % (mod, new)
  2109. self.check(b, a)
  2110. def test_import_module_usage(self):
  2111. for mod, (old, new) in self.modules.items():
  2112. b = """
  2113. import %s
  2114. foo(%s, %s.%s)
  2115. """ % (mod, mod, mod, old)
  2116. a = """
  2117. import %s
  2118. foo(%s, %s.%s)
  2119. """ % (mod, mod, mod, new)
  2120. self.check(b, a)
  2121. def XXX_test_from_import_usage(self):
  2122. # not implemented yet
  2123. for mod, (old, new) in self.modules.items():
  2124. b = """
  2125. from %s import %s
  2126. foo(%s, %s)
  2127. """ % (mod, old, mod, old)
  2128. a = """
  2129. from %s import %s
  2130. foo(%s, %s)
  2131. """ % (mod, new, mod, new)
  2132. self.check(b, a)
  2133. class Test_unicode(FixerTestCase):
  2134. fixer = "unicode"
  2135. def test_unicode_call(self):
  2136. b = """unicode(x, y, z)"""
  2137. a = """str(x, y, z)"""
  2138. self.check(b, a)
  2139. def test_unicode_literal_1(self):
  2140. b = '''u"x"'''
  2141. a = '''"x"'''
  2142. self.check(b, a)
  2143. def test_unicode_literal_2(self):
  2144. b = """ur'x'"""
  2145. a = """r'x'"""
  2146. self.check(b, a)
  2147. def test_unicode_literal_3(self):
  2148. b = """UR'''x'''"""
  2149. a = """R'''x'''"""
  2150. self.check(b, a)
  2151. class Test_callable(FixerTestCase):
  2152. fixer = "callable"
  2153. def test_prefix_preservation(self):
  2154. b = """callable( x)"""
  2155. a = """hasattr( x, '__call__')"""
  2156. self.check(b, a)
  2157. b = """if callable(x): pass"""
  2158. a = """if hasattr(x, '__call__'): pass"""
  2159. self.check(b, a)
  2160. def test_callable_call(self):
  2161. b = """callable(x)"""
  2162. a = """hasattr(x, '__call__')"""
  2163. self.check(b, a)
  2164. def test_callable_should_not_change(self):
  2165. a = """callable(*x)"""
  2166. self.unchanged(a)
  2167. a = """callable(x, y)"""
  2168. self.unchanged(a)
  2169. a = """callable(x, kw=y)"""
  2170. self.unchanged(a)
  2171. a = """callable()"""
  2172. self.unchanged(a)
  2173. class Test_filter(FixerTestCase):
  2174. fixer = "filter"
  2175. def test_prefix_preservation(self):
  2176. b = """x = filter( foo, 'abc' )"""
  2177. a = """x = list(filter( foo, 'abc' ))"""
  2178. self.check(b, a)
  2179. b = """x = filter( None , 'abc' )"""
  2180. a = """x = [_f for _f in 'abc' if _f]"""
  2181. self.check(b, a)
  2182. def test_filter_basic(self):
  2183. b = """x = filter(None, 'abc')"""
  2184. a = """x = [_f for _f in 'abc' if _f]"""
  2185. self.check(b, a)
  2186. b = """x = len(filter(f, 'abc'))"""
  2187. a = """x = len(list(filter(f, 'abc')))"""
  2188. self.check(b, a)
  2189. b = """x = filter(lambda x: x%2 == 0, range(10))"""
  2190. a = """x = [x for x in range(10) if x%2 == 0]"""
  2191. self.check(b, a)
  2192. # Note the parens around x
  2193. b = """x = filter(lambda (x): x%2 == 0, range(10))"""
  2194. a = """x = [x for x in range(10) if x%2 == 0]"""
  2195. self.check(b, a)
  2196. # XXX This (rare) case is not supported
  2197. ## b = """x = filter(f, 'abc')[0]"""
  2198. ## a = """x = list(filter(f, 'abc'))[0]"""
  2199. ## self.check(b, a)
  2200. def test_filter_nochange(self):
  2201. a = """b.join(filter(f, 'abc'))"""
  2202. self.unchanged(a)
  2203. a = """(a + foo(5)).join(filter(f, 'abc'))"""
  2204. self.unchanged(a)
  2205. a = """iter(filter(f, 'abc'))"""
  2206. self.unchanged(a)
  2207. a = """list(filter(f, 'abc'))"""
  2208. self.unchanged(a)
  2209. a = """list(filter(f, 'abc'))[0]"""
  2210. self.unchanged(a)
  2211. a = """set(filter(f, 'abc'))"""
  2212. self.unchanged(a)
  2213. a = """set(filter(f, 'abc')).pop()"""
  2214. self.unchanged(a)
  2215. a = """tuple(filter(f, 'abc'))"""
  2216. self.unchanged(a)
  2217. a = """any(filter(f, 'abc'))"""
  2218. self.unchanged(a)
  2219. a = """all(filter(f, 'abc'))"""
  2220. self.unchanged(a)
  2221. a = """sum(filter(f, 'abc'))"""
  2222. self.unchanged(a)
  2223. a = """sorted(filter(f, 'abc'))"""
  2224. self.unchanged(a)
  2225. a = """sorted(filter(f, 'abc'), key=blah)"""
  2226. self.unchanged(a)
  2227. a = """sorted(filter(f, 'abc'), key=blah)[0]"""
  2228. self.unchanged(a)
  2229. a = """for i in filter(f, 'abc'): pass"""
  2230. self.unchanged(a)
  2231. a = """[x for x in filter(f, 'abc')]"""
  2232. self.unchanged(a)
  2233. a = """(x for x in filter(f, 'abc'))"""
  2234. self.unchanged(a)
  2235. def test_future_builtins(self):
  2236. a = "from future_builtins import spam, filter; filter(f, 'ham')"
  2237. self.unchanged(a)
  2238. b = """from future_builtins import spam; x = filter(f, 'abc')"""
  2239. a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
  2240. self.check(b, a)
  2241. a = "from future_builtins import *; filter(f, 'ham')"
  2242. self.unchanged(a)
  2243. class Test_map(FixerTestCase):
  2244. fixer = "map"
  2245. def check(self, b, a):
  2246. self.unchanged("from future_builtins import map; " + b, a)
  2247. super(Test_map, self).check(b, a)
  2248. def test_prefix_preservation(self):
  2249. b = """x = map( f, 'abc' )"""
  2250. a = """x = list(map( f, 'abc' ))"""
  2251. self.check(b, a)
  2252. def test_trailing_comment(self):
  2253. b = """x = map(f, 'abc') # foo"""
  2254. a = """x = list(map(f, 'abc')) # foo"""
  2255. self.check(b, a)
  2256. def test_map_basic(self):
  2257. b = """x = map(f, 'abc')"""
  2258. a = """x = list(map(f, 'abc'))"""
  2259. self.check(b, a)
  2260. b = """x = len(map(f, 'abc', 'def'))"""
  2261. a = """x = len(list(map(f, 'abc', 'def')))"""
  2262. self.check(b, a)
  2263. b = """x = map(None, 'abc')"""
  2264. a = """x = list('abc')"""
  2265. self.check(b, a)
  2266. b = """x = map(None, 'abc', 'def')"""
  2267. a = """x = list(map(None, 'abc', 'def'))"""
  2268. self.check(b, a)
  2269. b = """x = map(lambda x: x+1, range(4))"""
  2270. a = """x = [x+1 for x in range(4)]"""
  2271. self.check(b, a)
  2272. # Note the parens around x
  2273. b = """x = map(lambda (x): x+1, range(4))"""
  2274. a = """x = [x+1 for x in range(4)]"""
  2275. self.check(b, a)
  2276. b = """
  2277. foo()
  2278. # foo
  2279. map(f, x)
  2280. """
  2281. a = """
  2282. foo()
  2283. # foo
  2284. list(map(f, x))
  2285. """
  2286. self.warns(b, a, "You should use a for loop here")
  2287. # XXX This (rare) case is not supported
  2288. ## b = """x = map(f, 'abc')[0]"""
  2289. ## a = """x = list(map(f, 'abc'))[0]"""
  2290. ## self.check(b, a)
  2291. def test_map_nochange(self):
  2292. a = """b.join(map(f, 'abc'))"""
  2293. self.unchanged(a)
  2294. a = """(a + foo(5)).join(map(f, 'abc'))"""
  2295. self.unchanged(a)
  2296. a = """iter(map(f, 'abc'))"""
  2297. self.unchanged(a)
  2298. a = """list(map(f, 'abc'))"""
  2299. self.unchanged(a)
  2300. a = """list(map(f, 'abc'))[0]"""
  2301. self.unchanged(a)
  2302. a = """set(map(f, 'abc'))"""
  2303. self.unchanged(a)
  2304. a = """set(map(f, 'abc')).pop()"""
  2305. self.unchanged(a)
  2306. a = """tuple(map(f, 'abc'))"""
  2307. self.unchanged(a)
  2308. a = """any(map(f, 'abc'))"""
  2309. self.unchanged(a)
  2310. a = """all(map(f, 'abc'))"""
  2311. self.unchanged(a)
  2312. a = """sum(map(f, 'abc'))"""
  2313. self.unchanged(a)
  2314. a = """sorted(map(f, 'abc'))"""
  2315. self.unchanged(a)
  2316. a = """sorted(map(f, 'abc'), key=blah)"""
  2317. self.unchanged(a)
  2318. a = """sorted(map(f, 'abc'), key=blah)[0]"""
  2319. self.unchanged(a)
  2320. a = """for i in map(f, 'abc'): pass"""
  2321. self.unchanged(a)
  2322. a = """[x for x in map(f, 'abc')]"""
  2323. self.unchanged(a)
  2324. a = """(x for x in map(f, 'abc'))"""
  2325. self.unchanged(a)
  2326. def test_future_builtins(self):
  2327. a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
  2328. self.unchanged(a)
  2329. b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
  2330. a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
  2331. self.check(b, a)
  2332. a = "from future_builtins import *; map(f, 'ham')"
  2333. self.unchanged(a)
  2334. class Test_zip(FixerTestCase):
  2335. fixer = "zip"
  2336. def check(self, b, a):
  2337. self.unchanged("from future_builtins import zip; " + b, a)
  2338. super(Test_zip, self).check(b, a)
  2339. def test_zip_basic(self):
  2340. b = """x = zip(a, b, c)"""
  2341. a = """x = list(zip(a, b, c))"""
  2342. self.check(b, a)
  2343. b = """x = len(zip(a, b))"""
  2344. a = """x = len(list(zip(a, b)))"""
  2345. self.check(b, a)
  2346. def test_zip_nochange(self):
  2347. a = """b.join(zip(a, b))"""
  2348. self.unchanged(a)
  2349. a = """(a + foo(5)).join(zip(a, b))"""
  2350. self.unchanged(a)
  2351. a = """iter(zip(a, b))"""
  2352. self.unchanged(a)
  2353. a = """list(zip(a, b))"""
  2354. self.unchanged(a)
  2355. a = """list(zip(a, b))[0]"""
  2356. self.unchanged(a)
  2357. a = """set(zip(a, b))"""
  2358. self.unchanged(a)
  2359. a = """set(zip(a, b)).pop()"""
  2360. self.unchanged(a)
  2361. a = """tuple(zip(a, b))"""
  2362. self.unchanged(a)
  2363. a = """any(zip(a, b))"""
  2364. self.unchanged(a)
  2365. a = """all(zip(a, b))"""
  2366. self.unchanged(a)
  2367. a = """sum(zip(a, b))"""
  2368. self.unchanged(a)
  2369. a = """sorted(zip(a, b))"""
  2370. self.unchanged(a)
  2371. a = """sorted(zip(a, b), key=blah)"""
  2372. self.unchanged(a)
  2373. a = """sorted(zip(a, b), key=blah)[0]"""
  2374. self.unchanged(a)
  2375. a = """for i in zip(a, b): pass"""
  2376. self.unchanged(a)
  2377. a = """[x for x in zip(a, b)]"""
  2378. self.unchanged(a)
  2379. a = """(x for x in zip(a, b))"""
  2380. self.unchanged(a)
  2381. def test_future_builtins(self):
  2382. a = "from future_builtins import spam, zip, eggs; zip(a, b)"
  2383. self.unchanged(a)
  2384. b = """from future_builtins import spam, eggs; x = zip(a, b)"""
  2385. a = """from future_builtins import spam, eggs; x = list(zip(a, b))"""
  2386. self.check(b, a)
  2387. a = "from future_builtins import *; zip(a, b)"
  2388. self.unchanged(a)
  2389. class Test_standarderror(FixerTestCase):
  2390. fixer = "standarderror"
  2391. def test(self):
  2392. b = """x = StandardError()"""
  2393. a = """x = Exception()"""
  2394. self.check(b, a)
  2395. b = """x = StandardError(a, b, c)"""
  2396. a = """x = Exception(a, b, c)"""
  2397. self.check(b, a)
  2398. b = """f(2 + StandardError(a, b, c))"""
  2399. a = """f(2 + Exception(a, b, c))"""
  2400. self.check(b, a)
  2401. class Test_types(FixerTestCase):
  2402. fixer = "types"
  2403. def test_basic_types_convert(self):
  2404. b = """types.StringType"""
  2405. a = """bytes"""
  2406. self.check(b, a)
  2407. b = """types.DictType"""
  2408. a = """dict"""
  2409. self.check(b, a)
  2410. b = """types . IntType"""
  2411. a = """int"""
  2412. self.check(b, a)
  2413. b = """types.ListType"""
  2414. a = """list"""
  2415. self.check(b, a)
  2416. b = """types.LongType"""
  2417. a = """int"""
  2418. self.check(b, a)
  2419. b = """types.NoneType"""
  2420. a = """type(None)"""
  2421. self.check(b, a)
  2422. class Test_idioms(FixerTestCase):
  2423. fixer = "idioms"
  2424. def test_while(self):
  2425. b = """while 1: foo()"""
  2426. a = """while True: foo()"""
  2427. self.check(b, a)
  2428. b = """while 1: foo()"""
  2429. a = """while True: foo()"""
  2430. self.check(b, a)
  2431. b = """
  2432. while 1:
  2433. foo()
  2434. """
  2435. a = """
  2436. while True:
  2437. foo()
  2438. """
  2439. self.check(b, a)
  2440. def test_while_unchanged(self):
  2441. s = """while 11: foo()"""
  2442. self.unchanged(s)
  2443. s = """while 0: foo()"""
  2444. self.unchanged(s)
  2445. s = """while foo(): foo()"""
  2446. self.unchanged(s)
  2447. s = """while []: foo()"""
  2448. self.unchanged(s)
  2449. def test_eq_simple(self):
  2450. b = """type(x) == T"""
  2451. a = """isinstance(x, T)"""
  2452. self.check(b, a)
  2453. b = """if type(x) == T: pass"""
  2454. a = """if isinstance(x, T): pass"""
  2455. self.check(b, a)
  2456. def test_eq_reverse(self):
  2457. b = """T == type(x)"""
  2458. a = """isinstance(x, T)"""
  2459. self.check(b, a)
  2460. b = """if T == type(x): pass"""
  2461. a = """if isinstance(x, T): pass"""
  2462. self.check(b, a)
  2463. def test_eq_expression(self):
  2464. b = """type(x+y) == d.get('T')"""
  2465. a = """isinstance(x+y, d.get('T'))"""
  2466. self.check(b, a)
  2467. b = """type( x + y) == d.get('T')"""
  2468. a = """isinstance(x + y, d.get('T'))"""
  2469. self.check(b, a)
  2470. def test_is_simple(self):
  2471. b = """type(x) is T"""
  2472. a = """isinstance(x, T)"""
  2473. self.check(b, a)
  2474. b = """if type(x) is T: pass"""
  2475. a = """if isinstance(x, T): pass"""
  2476. self.check(b, a)
  2477. def test_is_reverse(self):
  2478. b = """T is type(x)"""
  2479. a = """isinstance(x, T)"""
  2480. self.check(b, a)
  2481. b = """if T is type(x): pass"""
  2482. a = """if isinstance(x, T): pass"""
  2483. self.check(b, a)
  2484. def test_is_expression(self):
  2485. b = """type(x+y) is d.get('T')"""
  2486. a = """isinstance(x+y, d.get('T'))"""
  2487. self.check(b, a)
  2488. b = """type( x + y) is d.get('T')"""
  2489. a = """isinstance(x + y, d.get('T'))"""
  2490. self.check(b, a)
  2491. def test_is_not_simple(self):
  2492. b = """type(x) is not T"""
  2493. a = """not isinstance(x, T)"""
  2494. self.check(b, a)
  2495. b = """if type(x) is not T: pass"""
  2496. a = """if not isinstance(x, T): pass"""
  2497. self.check(b, a)
  2498. def test_is_not_reverse(self):
  2499. b = """T is not type(x)"""
  2500. a = """not isinstance(x, T)"""
  2501. self.check(b, a)
  2502. b = """if T is not type(x): pass"""
  2503. a = """if not isinstance(x, T): pass"""
  2504. self.check(b, a)
  2505. def test_is_not_expression(self):
  2506. b = """type(x+y) is not d.get('T')"""
  2507. a = """not isinstance(x+y, d.get('T'))"""
  2508. self.check(b, a)
  2509. b = """type( x + y) is not d.get('T')"""
  2510. a = """not isinstance(x + y, d.get('T'))"""
  2511. self.check(b, a)
  2512. def test_ne_simple(self):
  2513. b = """type(x) != T"""
  2514. a = """not isinstance(x, T)"""
  2515. self.check(b, a)
  2516. b = """if type(x) != T: pass"""
  2517. a = """if not isinstance(x, T): pass"""
  2518. self.check(b, a)
  2519. def test_ne_reverse(self):
  2520. b = """T != type(x)"""
  2521. a = """not isinstance(x, T)"""
  2522. self.check(b, a)
  2523. b = """if T != type(x): pass"""
  2524. a = """if not isinstance(x, T): pass"""
  2525. self.check(b, a)
  2526. def test_ne_expression(self):
  2527. b = """type(x+y) != d.get('T')"""
  2528. a = """not isinstance(x+y, d.get('T'))"""
  2529. self.check(b, a)
  2530. b = """type( x + y) != d.get('T')"""
  2531. a = """not isinstance(x + y, d.get('T'))"""
  2532. self.check(b, a)
  2533. def test_type_unchanged(self):
  2534. a = """type(x).__name__"""
  2535. self.unchanged(a)
  2536. def test_sort_list_call(self):
  2537. b = """
  2538. v = list(t)
  2539. v.sort()
  2540. foo(v)
  2541. """
  2542. a = """
  2543. v = sorted(t)
  2544. foo(v)
  2545. """
  2546. self.check(b, a)
  2547. b = """
  2548. v = list(foo(b) + d)
  2549. v.sort()
  2550. foo(v)
  2551. """
  2552. a = """
  2553. v = sorted(foo(b) + d)
  2554. foo(v)
  2555. """
  2556. self.check(b, a)
  2557. b = """
  2558. while x:
  2559. v = list(t)
  2560. v.sort()
  2561. foo(v)
  2562. """
  2563. a = """
  2564. while x:
  2565. v = sorted(t)
  2566. foo(v)
  2567. """
  2568. self.check(b, a)
  2569. b = """
  2570. v = list(t)
  2571. # foo
  2572. v.sort()
  2573. foo(v)
  2574. """
  2575. a = """
  2576. v = sorted(t)
  2577. # foo
  2578. foo(v)
  2579. """
  2580. self.check(b, a)
  2581. b = r"""
  2582. v = list( t)
  2583. v.sort()
  2584. foo(v)
  2585. """
  2586. a = r"""
  2587. v = sorted( t)
  2588. foo(v)
  2589. """
  2590. self.check(b, a)
  2591. def test_sort_simple_expr(self):
  2592. b = """
  2593. v = t
  2594. v.sort()
  2595. foo(v)
  2596. """
  2597. a = """
  2598. v = sorted(t)
  2599. foo(v)
  2600. """
  2601. self.check(b, a)
  2602. b = """
  2603. v = foo(b)
  2604. v.sort()
  2605. foo(v)
  2606. """
  2607. a = """
  2608. v = sorted(foo(b))
  2609. foo(v)
  2610. """
  2611. self.check(b, a)
  2612. b = """
  2613. v = b.keys()
  2614. v.sort()
  2615. foo(v)
  2616. """
  2617. a = """
  2618. v = sorted(b.keys())
  2619. foo(v)
  2620. """
  2621. self.check(b, a)
  2622. b = """
  2623. v = foo(b) + d
  2624. v.sort()
  2625. foo(v)
  2626. """
  2627. a = """
  2628. v = sorted(foo(b) + d)
  2629. foo(v)
  2630. """
  2631. self.check(b, a)
  2632. b = """
  2633. while x:
  2634. v = t
  2635. v.sort()
  2636. foo(v)
  2637. """
  2638. a = """
  2639. while x:
  2640. v = sorted(t)
  2641. foo(v)
  2642. """
  2643. self.check(b, a)
  2644. b = """
  2645. v = t
  2646. # foo
  2647. v.sort()
  2648. foo(v)
  2649. """
  2650. a = """
  2651. v = sorted(t)
  2652. # foo
  2653. foo(v)
  2654. """
  2655. self.check(b, a)
  2656. b = r"""
  2657. v = t
  2658. v.sort()
  2659. foo(v)
  2660. """
  2661. a = r"""
  2662. v = sorted(t)
  2663. foo(v)
  2664. """
  2665. self.check(b, a)
  2666. def test_sort_unchanged(self):
  2667. s = """
  2668. v = list(t)
  2669. w.sort()
  2670. foo(w)
  2671. """
  2672. self.unchanged(s)
  2673. s = """
  2674. v = list(t)
  2675. v.sort(u)
  2676. foo(v)
  2677. """
  2678. self.unchanged(s)
  2679. class Test_basestring(FixerTestCase):
  2680. fixer = "basestring"
  2681. def test_basestring(self):
  2682. b = """isinstance(x, basestring)"""
  2683. a = """isinstance(x, str)"""
  2684. self.check(b, a)
  2685. class Test_buffer(FixerTestCase):
  2686. fixer = "buffer"
  2687. def test_buffer(self):
  2688. b = """x = buffer(y)"""
  2689. a = """x = memoryview(y)"""
  2690. self.check(b, a)
  2691. class Test_future(FixerTestCase):
  2692. fixer = "future"
  2693. def test_future(self):
  2694. b = """from __future__ import braces"""
  2695. a = """"""
  2696. self.check(b, a)
  2697. b = """# comment\nfrom __future__ import braces"""
  2698. a = """# comment\n"""
  2699. self.check(b, a)
  2700. b = """from __future__ import braces\n# comment"""
  2701. a = """\n# comment"""
  2702. self.check(b, a)
  2703. def test_run_order(self):
  2704. self.assert_runs_after('print')
  2705. class Test_itertools(FixerTestCase):
  2706. fixer = "itertools"
  2707. def checkall(self, before, after):
  2708. # Because we need to check with and without the itertools prefix
  2709. # and on each of the three functions, these loops make it all
  2710. # much easier
  2711. for i in ('itertools.', ''):
  2712. for f in ('map', 'filter', 'zip'):
  2713. b = before %(i+'i'+f)
  2714. a = after %(f)
  2715. self.check(b, a)
  2716. def test_0(self):
  2717. # A simple example -- test_1 covers exactly the same thing,
  2718. # but it's not quite as clear.
  2719. b = "itertools.izip(a, b)"
  2720. a = "zip(a, b)"
  2721. self.check(b, a)
  2722. def test_1(self):
  2723. b = """%s(f, a)"""
  2724. a = """%s(f, a)"""
  2725. self.checkall(b, a)
  2726. def test_2(self):
  2727. b = """itertools.ifilterfalse(a, b)"""
  2728. a = """itertools.filterfalse(a, b)"""
  2729. self.check(b, a)
  2730. def test_4(self):
  2731. b = """ifilterfalse(a, b)"""
  2732. a = """filterfalse(a, b)"""
  2733. self.check(b, a)
  2734. def test_space_1(self):
  2735. b = """ %s(f, a)"""
  2736. a = """ %s(f, a)"""
  2737. self.checkall(b, a)
  2738. def test_space_2(self):
  2739. b = """ itertools.ifilterfalse(a, b)"""
  2740. a = """ itertools.filterfalse(a, b)"""
  2741. self.check(b, a)
  2742. def test_run_order(self):
  2743. self.assert_runs_after('map', 'zip', 'filter')
  2744. class Test_itertools_imports(FixerTestCase):
  2745. fixer = 'itertools_imports'
  2746. def test_reduced(self):
  2747. b = "from itertools import imap, izip, foo"
  2748. a = "from itertools import foo"
  2749. self.check(b, a)
  2750. b = "from itertools import bar, imap, izip, foo"
  2751. a = "from itertools import bar, foo"
  2752. self.check(b, a)
  2753. def test_comments(self):
  2754. b = "#foo\nfrom itertools import imap, izip"
  2755. a = "#foo\n"
  2756. self.check(b, a)
  2757. def test_none(self):
  2758. b = "from itertools import imap, izip"
  2759. a = ""
  2760. self.check(b, a)
  2761. b = "from itertools import izip"
  2762. a = ""
  2763. self.check(b, a)
  2764. def test_import_as(self):
  2765. b = "from itertools import izip, bar as bang, imap"
  2766. a = "from itertools import bar as bang"
  2767. self.check(b, a)
  2768. s = "from itertools import bar as bang"
  2769. self.unchanged(s)
  2770. def test_ifilter(self):
  2771. b = "from itertools import ifilterfalse"
  2772. a = "from itertools import filterfalse"
  2773. self.check(b, a)
  2774. b = "from itertools import imap, ifilterfalse, foo"
  2775. a = "from itertools import filterfalse, foo"
  2776. self.check(b, a)
  2777. b = "from itertools import bar, ifilterfalse, foo"
  2778. a = "from itertools import bar, filterfalse, foo"
  2779. self.check(b, a)
  2780. def test_unchanged(self):
  2781. s = "from itertools import foo"
  2782. self.unchanged(s)
  2783. class Test_import(FixerTestCase):
  2784. fixer = "import"
  2785. def setUp(self):
  2786. super(Test_import, self).setUp()
  2787. # Need to replace fix_import's exists method
  2788. # so we can check that it's doing the right thing
  2789. self.files_checked = []
  2790. self.present_files = set()
  2791. self.always_exists = True
  2792. def fake_exists(name):
  2793. self.files_checked.append(name)
  2794. return self.always_exists or (name in self.present_files)
  2795. from ..fixes import fix_import
  2796. fix_import.exists = fake_exists
  2797. def tearDown(self):
  2798. from lib2to3.fixes import fix_import
  2799. fix_import.exists = os.path.exists
  2800. def check_both(self, b, a):
  2801. self.always_exists = True
  2802. super(Test_import, self).check(b, a)
  2803. self.always_exists = False
  2804. super(Test_import, self).unchanged(b)
  2805. def test_files_checked(self):
  2806. def p(path):
  2807. # Takes a unix path and returns a path with correct separators
  2808. return os.path.pathsep.join(path.split("/"))
  2809. self.always_exists = False
  2810. self.present_files = set(['__init__.py'])
  2811. expected_extensions = ('.py', os.path.pathsep, '.pyc', '.so',
  2812. '.sl', '.pyd')
  2813. names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py"))
  2814. for name in names_to_test:
  2815. self.files_checked = []
  2816. self.filename = name
  2817. self.unchanged("import jam")
  2818. if os.path.dirname(name):
  2819. name = os.path.dirname(name) + '/jam'
  2820. else:
  2821. name = 'jam'
  2822. expected_checks = set(name + ext for ext in expected_extensions)
  2823. expected_checks.add("__init__.py")
  2824. self.assertEqual(set(self.files_checked), expected_checks)
  2825. def test_not_in_package(self):
  2826. s = "import bar"
  2827. self.always_exists = False
  2828. self.present_files = set(["bar.py"])
  2829. self.unchanged(s)
  2830. def test_in_package(self):
  2831. b = "import bar"
  2832. a = "from . import bar"
  2833. self.always_exists = False
  2834. self.present_files = set(["__init__.py", "bar.py"])
  2835. self.check(b, a)
  2836. def test_comments_and_indent(self):
  2837. b = "import bar # Foo"
  2838. a = "from . import bar # Foo"
  2839. self.check(b, a)
  2840. def test_from(self):
  2841. b = "from foo import bar, baz"
  2842. a = "from .foo import bar, baz"
  2843. self.check_both(b, a)
  2844. b = "from foo import bar"
  2845. a = "from .foo import bar"
  2846. self.check_both(b, a)
  2847. b = "from foo import (bar, baz)"
  2848. a = "from .foo import (bar, baz)"
  2849. self.check_both(b, a)
  2850. def test_dotted_from(self):
  2851. b = "from green.eggs import ham"
  2852. a = "from .green.eggs import ham"
  2853. self.check_both(b, a)
  2854. def test_from_as(self):
  2855. b = "from green.eggs import ham as spam"
  2856. a = "from .green.eggs import ham as spam"
  2857. self.check_both(b, a)
  2858. def test_import(self):
  2859. b = "import foo"
  2860. a = "from . import foo"
  2861. self.check_both(b, a)
  2862. b = "import foo, bar"
  2863. a = "from . import foo, bar"
  2864. self.check_both(b, a)
  2865. b = "import foo, bar, x"
  2866. a = "from . import foo, bar, x"
  2867. self.check_both(b, a)
  2868. b = "import x, y, z"
  2869. a = "from . import x, y, z"
  2870. self.check_both(b, a)
  2871. def test_import_as(self):
  2872. b = "import foo as x"
  2873. a = "from . import foo as x"
  2874. self.check_both(b, a)
  2875. b = "import a as b, b as c, c as d"
  2876. a = "from . import a as b, b as c, c as d"
  2877. self.check_both(b, a)
  2878. def test_local_and_absolute(self):
  2879. self.always_exists = False
  2880. self.present_files = set(["foo.py", "__init__.py"])
  2881. s = "import foo, bar"
  2882. self.warns_unchanged(s, "absolute and local imports together")
  2883. def test_dotted_import(self):
  2884. b = "import foo.bar"
  2885. a = "from . import foo.bar"
  2886. self.check_both(b, a)
  2887. def test_dotted_import_as(self):
  2888. b = "import foo.bar as bang"
  2889. a = "from . import foo.bar as bang"
  2890. self.check_both(b, a)
  2891. def test_prefix(self):
  2892. b = """
  2893. # prefix
  2894. import foo.bar
  2895. """
  2896. a = """
  2897. # prefix
  2898. from . import foo.bar
  2899. """
  2900. self.check_both(b, a)
  2901. class Test_set_literal(FixerTestCase):
  2902. fixer = "set_literal"
  2903. def test_basic(self):
  2904. b = """set([1, 2, 3])"""
  2905. a = """{1, 2, 3}"""
  2906. self.check(b, a)
  2907. b = """set((1, 2, 3))"""
  2908. a = """{1, 2, 3}"""
  2909. self.check(b, a)
  2910. b = """set((1,))"""
  2911. a = """{1}"""
  2912. self.check(b, a)
  2913. b = """set([1])"""
  2914. self.check(b, a)
  2915. b = """set((a, b))"""
  2916. a = """{a, b}"""
  2917. self.check(b, a)
  2918. b = """set([a, b])"""
  2919. self.check(b, a)
  2920. b = """set((a*234, f(args=23)))"""
  2921. a = """{a*234, f(args=23)}"""
  2922. self.check(b, a)
  2923. b = """set([a*23, f(23)])"""
  2924. a = """{a*23, f(23)}"""
  2925. self.check(b, a)
  2926. b = """set([a-234**23])"""
  2927. a = """{a-234**23}"""
  2928. self.check(b, a)
  2929. def test_listcomps(self):
  2930. b = """set([x for x in y])"""
  2931. a = """{x for x in y}"""
  2932. self.check(b, a)
  2933. b = """set([x for x in y if x == m])"""
  2934. a = """{x for x in y if x == m}"""
  2935. self.check(b, a)
  2936. b = """set([x for x in y for a in b])"""
  2937. a = """{x for x in y for a in b}"""
  2938. self.check(b, a)
  2939. b = """set([f(x) - 23 for x in y])"""
  2940. a = """{f(x) - 23 for x in y}"""
  2941. self.check(b, a)
  2942. def test_whitespace(self):
  2943. b = """set( [1, 2])"""
  2944. a = """{1, 2}"""
  2945. self.check(b, a)
  2946. b = """set([1 , 2])"""
  2947. a = """{1 , 2}"""
  2948. self.check(b, a)
  2949. b = """set([ 1 ])"""
  2950. a = """{ 1 }"""
  2951. self.check(b, a)
  2952. b = """set( [1] )"""
  2953. a = """{1}"""
  2954. self.check(b, a)
  2955. b = """set([ 1, 2 ])"""
  2956. a = """{ 1, 2 }"""
  2957. self.check(b, a)
  2958. b = """set([x for x in y ])"""
  2959. a = """{x for x in y }"""
  2960. self.check(b, a)
  2961. b = """set(
  2962. [1, 2]
  2963. )
  2964. """
  2965. a = """{1, 2}\n"""
  2966. self.check(b, a)
  2967. def test_comments(self):
  2968. b = """set((1, 2)) # Hi"""
  2969. a = """{1, 2} # Hi"""
  2970. self.check(b, a)
  2971. # This isn't optimal behavior, but the fixer is optional.
  2972. b = """
  2973. # Foo
  2974. set( # Bar
  2975. (1, 2)
  2976. )
  2977. """
  2978. a = """
  2979. # Foo
  2980. {1, 2}
  2981. """
  2982. self.check(b, a)
  2983. def test_unchanged(self):
  2984. s = """set()"""
  2985. self.unchanged(s)
  2986. s = """set(a)"""
  2987. self.unchanged(s)
  2988. s = """set(a, b, c)"""
  2989. self.unchanged(s)
  2990. # Don't transform generators because they might have to be lazy.
  2991. s = """set(x for x in y)"""
  2992. self.unchanged(s)
  2993. s = """set(x for x in y if z)"""
  2994. self.unchanged(s)
  2995. s = """set(a*823-23**2 + f(23))"""
  2996. self.unchanged(s)
  2997. class Test_sys_exc(FixerTestCase):
  2998. fixer = "sys_exc"
  2999. def test_0(self):
  3000. b = "sys.exc_type"
  3001. a = "sys.exc_info()[0]"
  3002. self.check(b, a)
  3003. def test_1(self):
  3004. b = "sys.exc_value"
  3005. a = "sys.exc_info()[1]"
  3006. self.check(b, a)
  3007. def test_2(self):
  3008. b = "sys.exc_traceback"
  3009. a = "sys.exc_info()[2]"
  3010. self.check(b, a)
  3011. def test_3(self):
  3012. b = "sys.exc_type # Foo"
  3013. a = "sys.exc_info()[0] # Foo"
  3014. self.check(b, a)
  3015. def test_4(self):
  3016. b = "sys. exc_type"
  3017. a = "sys. exc_info()[0]"
  3018. self.check(b, a)
  3019. def test_5(self):
  3020. b = "sys .exc_type"
  3021. a = "sys .exc_info()[0]"
  3022. self.check(b, a)
  3023. class Test_paren(FixerTestCase):
  3024. fixer = "paren"
  3025. def test_0(self):
  3026. b = """[i for i in 1, 2 ]"""
  3027. a = """[i for i in (1, 2) ]"""
  3028. self.check(b, a)
  3029. def test_1(self):
  3030. b = """[i for i in 1, 2, ]"""
  3031. a = """[i for i in (1, 2,) ]"""
  3032. self.check(b, a)
  3033. def test_2(self):
  3034. b = """[i for i in 1, 2 ]"""
  3035. a = """[i for i in (1, 2) ]"""
  3036. self.check(b, a)
  3037. def test_3(self):
  3038. b = """[i for i in 1, 2 if i]"""
  3039. a = """[i for i in (1, 2) if i]"""
  3040. self.check(b, a)
  3041. def test_4(self):
  3042. b = """[i for i in 1, 2 ]"""
  3043. a = """[i for i in (1, 2) ]"""
  3044. self.check(b, a)
  3045. def test_5(self):
  3046. b = """(i for i in 1, 2)"""
  3047. a = """(i for i in (1, 2))"""
  3048. self.check(b, a)
  3049. def test_6(self):
  3050. b = """(i for i in 1 ,2 if i)"""
  3051. a = """(i for i in (1 ,2) if i)"""
  3052. self.check(b, a)
  3053. def test_unchanged_0(self):
  3054. s = """[i for i in (1, 2)]"""
  3055. self.unchanged(s)
  3056. def test_unchanged_1(self):
  3057. s = """[i for i in foo()]"""
  3058. self.unchanged(s)
  3059. def test_unchanged_2(self):
  3060. s = """[i for i in (1, 2) if nothing]"""
  3061. self.unchanged(s)
  3062. def test_unchanged_3(self):
  3063. s = """(i for i in (1, 2))"""
  3064. self.unchanged(s)
  3065. def test_unchanged_4(self):
  3066. s = """[i for i in m]"""
  3067. self.unchanged(s)
  3068. class Test_metaclass(FixerTestCase):
  3069. fixer = 'metaclass'
  3070. def test_unchanged(self):
  3071. self.unchanged("class X(): pass")
  3072. self.unchanged("class X(object): pass")
  3073. self.unchanged("class X(object1, object2): pass")
  3074. self.unchanged("class X(object1, object2, object3): pass")
  3075. self.unchanged("class X(metaclass=Meta): pass")
  3076. self.unchanged("class X(b, arg=23, metclass=Meta): pass")
  3077. self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass")
  3078. s = """
  3079. class X:
  3080. def __metaclass__(self): pass
  3081. """
  3082. self.unchanged(s)
  3083. s = """
  3084. class X:
  3085. a[23] = 74
  3086. """
  3087. self.unchanged(s)
  3088. def test_comments(self):
  3089. b = """
  3090. class X:
  3091. # hi
  3092. __metaclass__ = AppleMeta
  3093. """
  3094. a = """
  3095. class X(metaclass=AppleMeta):
  3096. # hi
  3097. pass
  3098. """
  3099. self.check(b, a)
  3100. b = """
  3101. class X:
  3102. __metaclass__ = Meta
  3103. # Bedtime!
  3104. """
  3105. a = """
  3106. class X(metaclass=Meta):
  3107. pass
  3108. # Bedtime!
  3109. """
  3110. self.check(b, a)
  3111. def test_meta(self):
  3112. # no-parent class, odd body
  3113. b = """
  3114. class X():
  3115. __metaclass__ = Q
  3116. pass
  3117. """
  3118. a = """
  3119. class X(metaclass=Q):
  3120. pass
  3121. """
  3122. self.check(b, a)
  3123. # one parent class, no body
  3124. b = """class X(object): __metaclass__ = Q"""
  3125. a = """class X(object, metaclass=Q): pass"""
  3126. self.check(b, a)
  3127. # one parent, simple body
  3128. b = """
  3129. class X(object):
  3130. __metaclass__ = Meta
  3131. bar = 7
  3132. """
  3133. a = """
  3134. class X(object, metaclass=Meta):
  3135. bar = 7
  3136. """
  3137. self.check(b, a)
  3138. b = """
  3139. class X:
  3140. __metaclass__ = Meta; x = 4; g = 23
  3141. """
  3142. a = """
  3143. class X(metaclass=Meta):
  3144. x = 4; g = 23
  3145. """
  3146. self.check(b, a)
  3147. # one parent, simple body, __metaclass__ last
  3148. b = """
  3149. class X(object):
  3150. bar = 7
  3151. __metaclass__ = Meta
  3152. """
  3153. a = """
  3154. class X(object, metaclass=Meta):
  3155. bar = 7
  3156. """
  3157. self.check(b, a)
  3158. # redefining __metaclass__
  3159. b = """
  3160. class X():
  3161. __metaclass__ = A
  3162. __metaclass__ = B
  3163. bar = 7
  3164. """
  3165. a = """
  3166. class X(metaclass=B):
  3167. bar = 7
  3168. """
  3169. self.check(b, a)
  3170. # multiple inheritance, simple body
  3171. b = """
  3172. class X(clsA, clsB):
  3173. __metaclass__ = Meta
  3174. bar = 7
  3175. """
  3176. a = """
  3177. class X(clsA, clsB, metaclass=Meta):
  3178. bar = 7
  3179. """
  3180. self.check(b, a)
  3181. # keywords in the class statement
  3182. b = """class m(a, arg=23): __metaclass__ = Meta"""
  3183. a = """class m(a, arg=23, metaclass=Meta): pass"""
  3184. self.check(b, a)
  3185. b = """
  3186. class X(expression(2 + 4)):
  3187. __metaclass__ = Meta
  3188. """
  3189. a = """
  3190. class X(expression(2 + 4), metaclass=Meta):
  3191. pass
  3192. """
  3193. self.check(b, a)
  3194. b = """
  3195. class X(expression(2 + 4), x**4):
  3196. __metaclass__ = Meta
  3197. """
  3198. a = """
  3199. class X(expression(2 + 4), x**4, metaclass=Meta):
  3200. pass
  3201. """
  3202. self.check(b, a)
  3203. b = """
  3204. class X:
  3205. __metaclass__ = Meta
  3206. save.py = 23
  3207. """
  3208. a = """
  3209. class X(metaclass=Meta):
  3210. save.py = 23
  3211. """
  3212. self.check(b, a)
  3213. class Test_getcwdu(FixerTestCase):
  3214. fixer = 'getcwdu'
  3215. def test_basic(self):
  3216. b = """os.getcwdu"""
  3217. a = """os.getcwd"""
  3218. self.check(b, a)
  3219. b = """os.getcwdu()"""
  3220. a = """os.getcwd()"""
  3221. self.check(b, a)
  3222. b = """meth = os.getcwdu"""
  3223. a = """meth = os.getcwd"""
  3224. self.check(b, a)
  3225. b = """os.getcwdu(args)"""
  3226. a = """os.getcwd(args)"""
  3227. self.check(b, a)
  3228. def test_comment(self):
  3229. b = """os.getcwdu() # Foo"""
  3230. a = """os.getcwd() # Foo"""
  3231. self.check(b, a)
  3232. def test_unchanged(self):
  3233. s = """os.getcwd()"""
  3234. self.unchanged(s)
  3235. s = """getcwdu()"""
  3236. self.unchanged(s)
  3237. s = """os.getcwdb()"""
  3238. self.unchanged(s)
  3239. def test_indentation(self):
  3240. b = """
  3241. if 1:
  3242. os.getcwdu()
  3243. """
  3244. a = """
  3245. if 1:
  3246. os.getcwd()
  3247. """
  3248. self.check(b, a)
  3249. def test_multilation(self):
  3250. b = """os .getcwdu()"""
  3251. a = """os .getcwd()"""
  3252. self.check(b, a)
  3253. b = """os. getcwdu"""
  3254. a = """os. getcwd"""
  3255. self.check(b, a)
  3256. b = """os.getcwdu ( )"""
  3257. a = """os.getcwd ( )"""
  3258. self.check(b, a)
  3259. if __name__ == "__main__":
  3260. import __main__
  3261. support.run_all_tests(__main__)