# Python code coverage for Lib/lib2to3/tests/test_fixers.py

# | count | content |
---|---|---|

1 | n/a | """ Test suite for the fixer modules """ |

2 | n/a | |

3 | n/a | # Python imports |

4 | n/a | import os |

5 | n/a | from itertools import chain |

6 | n/a | from operator import itemgetter |

7 | n/a | |

8 | n/a | # Local imports |

9 | n/a | from lib2to3 import pygram, fixer_util |

10 | n/a | from lib2to3.tests import support |

11 | n/a | |

12 | n/a | |

13 | n/a | class FixerTestCase(support.TestCase): |

14 | n/a | |

15 | n/a | # Other test cases can subclass this class and replace "fixer_pkg" with |

16 | n/a | # their own. |

17 | n/a | def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None): |

18 | n/a | if fix_list is None: |

19 | n/a | fix_list = [self.fixer] |

20 | n/a | self.refactor = support.get_refactorer(fixer_pkg, fix_list, options) |

21 | n/a | self.fixer_log = [] |

22 | n/a | self.filename = "<string>" |

23 | n/a | |

24 | n/a | for fixer in chain(self.refactor.pre_order, |

25 | n/a | self.refactor.post_order): |

26 | n/a | fixer.log = self.fixer_log |

27 | n/a | |

28 | n/a | def _check(self, before, after): |

29 | n/a | before = support.reformat(before) |

30 | n/a | after = support.reformat(after) |

31 | n/a | tree = self.refactor.refactor_string(before, self.filename) |

32 | n/a | self.assertEqual(after, str(tree)) |

33 | n/a | return tree |

34 | n/a | |

35 | n/a | def check(self, before, after, ignore_warnings=False): |

36 | n/a | tree = self._check(before, after) |

37 | n/a | self.assertTrue(tree.was_changed) |

38 | n/a | if not ignore_warnings: |

39 | n/a | self.assertEqual(self.fixer_log, []) |

40 | n/a | |

41 | n/a | def warns(self, before, after, message, unchanged=False): |

42 | n/a | tree = self._check(before, after) |

43 | n/a | self.assertIn(message, "".join(self.fixer_log)) |

44 | n/a | if not unchanged: |

45 | n/a | self.assertTrue(tree.was_changed) |

46 | n/a | |

47 | n/a | def warns_unchanged(self, before, message): |

48 | n/a | self.warns(before, before, message, unchanged=True) |

49 | n/a | |

50 | n/a | def unchanged(self, before, ignore_warnings=False): |

51 | n/a | self._check(before, before) |

52 | n/a | if not ignore_warnings: |

53 | n/a | self.assertEqual(self.fixer_log, []) |

54 | n/a | |

55 | n/a | def assert_runs_after(self, *names): |

56 | n/a | fixes = [self.fixer] |

57 | n/a | fixes.extend(names) |

58 | n/a | r = support.get_refactorer("lib2to3", fixes) |

59 | n/a | (pre, post) = r.get_fixers() |

60 | n/a | n = "fix_" + self.fixer |

61 | n/a | if post and post[-1].__class__.__module__.endswith(n): |

62 | n/a | # We're the last fixer to run |

63 | n/a | return |

64 | n/a | if pre and pre[-1].__class__.__module__.endswith(n) and not post: |

65 | n/a | # We're the last in pre and post is empty |

66 | n/a | return |

67 | n/a | self.fail("Fixer run order (%s) is incorrect; %s should be last."\ |

68 | n/a | %(", ".join([x.__class__.__module__ for x in (pre+post)]), n)) |

69 | n/a | |

70 | n/a | class Test_ne(FixerTestCase): |

71 | n/a | fixer = "ne" |

72 | n/a | |

73 | n/a | def test_basic(self): |

74 | n/a | b = """if x <> y: |

75 | n/a | pass""" |

76 | n/a | |

77 | n/a | a = """if x != y: |

78 | n/a | pass""" |

79 | n/a | self.check(b, a) |

80 | n/a | |

81 | n/a | def test_no_spaces(self): |

82 | n/a | b = """if x<>y: |

83 | n/a | pass""" |

84 | n/a | |

85 | n/a | a = """if x!=y: |

86 | n/a | pass""" |

87 | n/a | self.check(b, a) |

88 | n/a | |

89 | n/a | def test_chained(self): |

90 | n/a | b = """if x<>y<>z: |

91 | n/a | pass""" |

92 | n/a | |

93 | n/a | a = """if x!=y!=z: |

94 | n/a | pass""" |

95 | n/a | self.check(b, a) |

96 | n/a | |

97 | n/a | class Test_has_key(FixerTestCase): |

98 | n/a | fixer = "has_key" |

99 | n/a | |

100 | n/a | def test_1(self): |

101 | n/a | b = """x = d.has_key("x") or d.has_key("y")""" |

102 | n/a | a = """x = "x" in d or "y" in d""" |

103 | n/a | self.check(b, a) |

104 | n/a | |

105 | n/a | def test_2(self): |

106 | n/a | b = """x = a.b.c.d.has_key("x") ** 3""" |

107 | n/a | a = """x = ("x" in a.b.c.d) ** 3""" |

108 | n/a | self.check(b, a) |

109 | n/a | |

110 | n/a | def test_3(self): |

111 | n/a | b = """x = a.b.has_key(1 + 2).__repr__()""" |

112 | n/a | a = """x = (1 + 2 in a.b).__repr__()""" |

113 | n/a | self.check(b, a) |

114 | n/a | |

115 | n/a | def test_4(self): |

116 | n/a | b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4""" |

117 | n/a | a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4""" |

118 | n/a | self.check(b, a) |

119 | n/a | |

120 | n/a | def test_5(self): |

121 | n/a | b = """x = a.has_key(f or g)""" |

122 | n/a | a = """x = (f or g) in a""" |

123 | n/a | self.check(b, a) |

124 | n/a | |

125 | n/a | def test_6(self): |

126 | n/a | b = """x = a + b.has_key(c)""" |

127 | n/a | a = """x = a + (c in b)""" |

128 | n/a | self.check(b, a) |

129 | n/a | |

130 | n/a | def test_7(self): |

131 | n/a | b = """x = a.has_key(lambda: 12)""" |

132 | n/a | a = """x = (lambda: 12) in a""" |

133 | n/a | self.check(b, a) |

134 | n/a | |

135 | n/a | def test_8(self): |

136 | n/a | b = """x = a.has_key(a for a in b)""" |

137 | n/a | a = """x = (a for a in b) in a""" |

138 | n/a | self.check(b, a) |

139 | n/a | |

140 | n/a | def test_9(self): |

141 | n/a | b = """if not a.has_key(b): pass""" |

142 | n/a | a = """if b not in a: pass""" |

143 | n/a | self.check(b, a) |

144 | n/a | |

145 | n/a | def test_10(self): |

146 | n/a | b = """if not a.has_key(b).__repr__(): pass""" |

147 | n/a | a = """if not (b in a).__repr__(): pass""" |

148 | n/a | self.check(b, a) |

149 | n/a | |

150 | n/a | def test_11(self): |

151 | n/a | b = """if not a.has_key(b) ** 2: pass""" |

152 | n/a | a = """if not (b in a) ** 2: pass""" |

153 | n/a | self.check(b, a) |

154 | n/a | |

155 | n/a | class Test_apply(FixerTestCase): |

156 | n/a | fixer = "apply" |

157 | n/a | |

158 | n/a | def test_1(self): |

159 | n/a | b = """x = apply(f, g + h)""" |

160 | n/a | a = """x = f(*g + h)""" |

161 | n/a | self.check(b, a) |

162 | n/a | |

163 | n/a | def test_2(self): |

164 | n/a | b = """y = apply(f, g, h)""" |

165 | n/a | a = """y = f(*g, **h)""" |

166 | n/a | self.check(b, a) |

167 | n/a | |

168 | n/a | def test_3(self): |

169 | n/a | b = """z = apply(fs[0], g or h, h or g)""" |

170 | n/a | a = """z = fs[0](*g or h, **h or g)""" |

171 | n/a | self.check(b, a) |

172 | n/a | |

173 | n/a | def test_4(self): |

174 | n/a | b = """apply(f, (x, y) + t)""" |

175 | n/a | a = """f(*(x, y) + t)""" |

176 | n/a | self.check(b, a) |

177 | n/a | |

178 | n/a | def test_5(self): |

179 | n/a | b = """apply(f, args,)""" |

180 | n/a | a = """f(*args)""" |

181 | n/a | self.check(b, a) |

182 | n/a | |

183 | n/a | def test_6(self): |

184 | n/a | b = """apply(f, args, kwds,)""" |

185 | n/a | a = """f(*args, **kwds)""" |

186 | n/a | self.check(b, a) |

187 | n/a | |

188 | n/a | # Test that complex functions are parenthesized |

189 | n/a | |

190 | n/a | def test_complex_1(self): |

191 | n/a | b = """x = apply(f+g, args)""" |

192 | n/a | a = """x = (f+g)(*args)""" |

193 | n/a | self.check(b, a) |

194 | n/a | |

195 | n/a | def test_complex_2(self): |

196 | n/a | b = """x = apply(f*g, args)""" |

197 | n/a | a = """x = (f*g)(*args)""" |

198 | n/a | self.check(b, a) |

199 | n/a | |

200 | n/a | def test_complex_3(self): |

201 | n/a | b = """x = apply(f**g, args)""" |

202 | n/a | a = """x = (f**g)(*args)""" |

203 | n/a | self.check(b, a) |

204 | n/a | |

205 | n/a | # But dotted names etc. not |

206 | n/a | |

207 | n/a | def test_dotted_name(self): |

208 | n/a | b = """x = apply(f.g, args)""" |

209 | n/a | a = """x = f.g(*args)""" |

210 | n/a | self.check(b, a) |

211 | n/a | |

212 | n/a | def test_subscript(self): |

213 | n/a | b = """x = apply(f[x], args)""" |

214 | n/a | a = """x = f[x](*args)""" |

215 | n/a | self.check(b, a) |

216 | n/a | |

217 | n/a | def test_call(self): |

218 | n/a | b = """x = apply(f(), args)""" |

219 | n/a | a = """x = f()(*args)""" |

220 | n/a | self.check(b, a) |

221 | n/a | |

222 | n/a | # Extreme case |

223 | n/a | def test_extreme(self): |

224 | n/a | b = """x = apply(a.b.c.d.e.f, args, kwds)""" |

225 | n/a | a = """x = a.b.c.d.e.f(*args, **kwds)""" |

226 | n/a | self.check(b, a) |

227 | n/a | |

228 | n/a | # XXX Comments in weird places still get lost |

229 | n/a | def test_weird_comments(self): |

230 | n/a | b = """apply( # foo |

231 | n/a | f, # bar |

232 | n/a | args)""" |

233 | n/a | a = """f(*args)""" |

234 | n/a | self.check(b, a) |

235 | n/a | |

236 | n/a | # These should *not* be touched |

237 | n/a | |

238 | n/a | def test_unchanged_1(self): |

239 | n/a | s = """apply()""" |

240 | n/a | self.unchanged(s) |

241 | n/a | |

242 | n/a | def test_unchanged_2(self): |

243 | n/a | s = """apply(f)""" |

244 | n/a | self.unchanged(s) |

245 | n/a | |

246 | n/a | def test_unchanged_3(self): |

247 | n/a | s = """apply(f,)""" |

248 | n/a | self.unchanged(s) |

249 | n/a | |

250 | n/a | def test_unchanged_4(self): |

251 | n/a | s = """apply(f, args, kwds, extras)""" |

252 | n/a | self.unchanged(s) |

253 | n/a | |

254 | n/a | def test_unchanged_5(self): |

255 | n/a | s = """apply(f, *args, **kwds)""" |

256 | n/a | self.unchanged(s) |

257 | n/a | |

258 | n/a | def test_unchanged_6(self): |

259 | n/a | s = """apply(f, *args)""" |

260 | n/a | self.unchanged(s) |

261 | n/a | |

262 | n/a | def test_unchanged_6b(self): |

263 | n/a | s = """apply(f, **kwds)""" |

264 | n/a | self.unchanged(s) |

265 | n/a | |

266 | n/a | def test_unchanged_7(self): |

267 | n/a | s = """apply(func=f, args=args, kwds=kwds)""" |

268 | n/a | self.unchanged(s) |

269 | n/a | |

270 | n/a | def test_unchanged_8(self): |

271 | n/a | s = """apply(f, args=args, kwds=kwds)""" |

272 | n/a | self.unchanged(s) |

273 | n/a | |

274 | n/a | def test_unchanged_9(self): |

275 | n/a | s = """apply(f, args, kwds=kwds)""" |

276 | n/a | self.unchanged(s) |

277 | n/a | |

278 | n/a | def test_space_1(self): |

279 | n/a | a = """apply( f, args, kwds)""" |

280 | n/a | b = """f(*args, **kwds)""" |

281 | n/a | self.check(a, b) |

282 | n/a | |

283 | n/a | def test_space_2(self): |

284 | n/a | a = """apply( f ,args,kwds )""" |

285 | n/a | b = """f(*args, **kwds)""" |

286 | n/a | self.check(a, b) |

287 | n/a | |

288 | n/a | class Test_reload(FixerTestCase): |

289 | n/a | fixer = "reload" |

290 | n/a | |

291 | n/a | def test(self): |

292 | n/a | b = """reload(a)""" |

293 | n/a | a = """import imp\nimp.reload(a)""" |

294 | n/a | self.check(b, a) |

295 | n/a | |

296 | n/a | def test_comment(self): |

297 | n/a | b = """reload( a ) # comment""" |

298 | n/a | a = """import imp\nimp.reload( a ) # comment""" |

299 | n/a | self.check(b, a) |

300 | n/a | |

301 | n/a | # PEP 8 comments |

302 | n/a | b = """reload( a ) # comment""" |

303 | n/a | a = """import imp\nimp.reload( a ) # comment""" |

304 | n/a | self.check(b, a) |

305 | n/a | |

306 | n/a | def test_space(self): |

307 | n/a | b = """reload( a )""" |

308 | n/a | a = """import imp\nimp.reload( a )""" |

309 | n/a | self.check(b, a) |

310 | n/a | |

311 | n/a | b = """reload( a)""" |

312 | n/a | a = """import imp\nimp.reload( a)""" |

313 | n/a | self.check(b, a) |

314 | n/a | |

315 | n/a | b = """reload(a )""" |

316 | n/a | a = """import imp\nimp.reload(a )""" |

317 | n/a | self.check(b, a) |

318 | n/a | |

319 | n/a | def test_unchanged(self): |

320 | n/a | s = """reload(a=1)""" |

321 | n/a | self.unchanged(s) |

322 | n/a | |

323 | n/a | s = """reload(f, g)""" |

324 | n/a | self.unchanged(s) |

325 | n/a | |

326 | n/a | s = """reload(f, *h)""" |

327 | n/a | self.unchanged(s) |

328 | n/a | |

329 | n/a | s = """reload(f, *h, **i)""" |

330 | n/a | self.unchanged(s) |

331 | n/a | |

332 | n/a | s = """reload(f, **i)""" |

333 | n/a | self.unchanged(s) |

334 | n/a | |

335 | n/a | s = """reload(*h, **i)""" |

336 | n/a | self.unchanged(s) |

337 | n/a | |

338 | n/a | s = """reload(*h)""" |

339 | n/a | self.unchanged(s) |

340 | n/a | |

341 | n/a | s = """reload(**i)""" |

342 | n/a | self.unchanged(s) |

343 | n/a | |

344 | n/a | s = """reload()""" |

345 | n/a | self.unchanged(s) |

346 | n/a | |

347 | n/a | class Test_intern(FixerTestCase): |

348 | n/a | fixer = "intern" |

349 | n/a | |

350 | n/a | def test_prefix_preservation(self): |

351 | n/a | b = """x = intern( a )""" |

352 | n/a | a = """import sys\nx = sys.intern( a )""" |

353 | n/a | self.check(b, a) |

354 | n/a | |

355 | n/a | b = """y = intern("b" # test |

356 | n/a | )""" |

357 | n/a | a = """import sys\ny = sys.intern("b" # test |

358 | n/a | )""" |

359 | n/a | self.check(b, a) |

360 | n/a | |

361 | n/a | b = """z = intern(a+b+c.d, )""" |

362 | n/a | a = """import sys\nz = sys.intern(a+b+c.d, )""" |

363 | n/a | self.check(b, a) |

364 | n/a | |

365 | n/a | def test(self): |

366 | n/a | b = """x = intern(a)""" |

367 | n/a | a = """import sys\nx = sys.intern(a)""" |

368 | n/a | self.check(b, a) |

369 | n/a | |

370 | n/a | b = """z = intern(a+b+c.d,)""" |

371 | n/a | a = """import sys\nz = sys.intern(a+b+c.d,)""" |

372 | n/a | self.check(b, a) |

373 | n/a | |

374 | n/a | b = """intern("y%s" % 5).replace("y", "")""" |

375 | n/a | a = """import sys\nsys.intern("y%s" % 5).replace("y", "")""" |

376 | n/a | self.check(b, a) |

377 | n/a | |

378 | n/a | # These should not be refactored |

379 | n/a | |

380 | n/a | def test_unchanged(self): |

381 | n/a | s = """intern(a=1)""" |

382 | n/a | self.unchanged(s) |

383 | n/a | |

384 | n/a | s = """intern(f, g)""" |

385 | n/a | self.unchanged(s) |

386 | n/a | |

387 | n/a | s = """intern(*h)""" |

388 | n/a | self.unchanged(s) |

389 | n/a | |

390 | n/a | s = """intern(**i)""" |

391 | n/a | self.unchanged(s) |

392 | n/a | |

393 | n/a | s = """intern()""" |

394 | n/a | self.unchanged(s) |

395 | n/a | |

396 | n/a | class Test_reduce(FixerTestCase): |

397 | n/a | fixer = "reduce" |

398 | n/a | |

399 | n/a | def test_simple_call(self): |

400 | n/a | b = "reduce(a, b, c)" |

401 | n/a | a = "from functools import reduce\nreduce(a, b, c)" |

402 | n/a | self.check(b, a) |

403 | n/a | |

404 | n/a | def test_bug_7253(self): |

405 | n/a | # fix_tuple_params was being bad and orphaning nodes in the tree. |

406 | n/a | b = "def x(arg): reduce(sum, [])" |

407 | n/a | a = "from functools import reduce\ndef x(arg): reduce(sum, [])" |

408 | n/a | self.check(b, a) |

409 | n/a | |

410 | n/a | def test_call_with_lambda(self): |

411 | n/a | b = "reduce(lambda x, y: x + y, seq)" |

412 | n/a | a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)" |

413 | n/a | self.check(b, a) |

414 | n/a | |

415 | n/a | def test_unchanged(self): |

416 | n/a | s = "reduce(a)" |

417 | n/a | self.unchanged(s) |

418 | n/a | |

419 | n/a | s = "reduce(a, b=42)" |

420 | n/a | self.unchanged(s) |

421 | n/a | |

422 | n/a | s = "reduce(a, b, c, d)" |

423 | n/a | self.unchanged(s) |

424 | n/a | |

425 | n/a | s = "reduce(**c)" |

426 | n/a | self.unchanged(s) |

427 | n/a | |

428 | n/a | s = "reduce()" |

429 | n/a | self.unchanged(s) |

430 | n/a | |

431 | n/a | class Test_print(FixerTestCase): |

432 | n/a | fixer = "print" |

433 | n/a | |

434 | n/a | def test_prefix_preservation(self): |

435 | n/a | b = """print 1, 1+1, 1+1+1""" |

436 | n/a | a = """print(1, 1+1, 1+1+1)""" |

437 | n/a | self.check(b, a) |

438 | n/a | |

439 | n/a | def test_idempotency(self): |

440 | n/a | s = """print()""" |

441 | n/a | self.unchanged(s) |

442 | n/a | |

443 | n/a | s = """print('')""" |

444 | n/a | self.unchanged(s) |

445 | n/a | |

446 | n/a | def test_idempotency_print_as_function(self): |

447 | n/a | self.refactor.driver.grammar = pygram.python_grammar_no_print_statement |

448 | n/a | s = """print(1, 1+1, 1+1+1)""" |

449 | n/a | self.unchanged(s) |

450 | n/a | |

451 | n/a | s = """print()""" |

452 | n/a | self.unchanged(s) |

453 | n/a | |

454 | n/a | s = """print('')""" |

455 | n/a | self.unchanged(s) |

456 | n/a | |

457 | n/a | def test_1(self): |

458 | n/a | b = """print 1, 1+1, 1+1+1""" |

459 | n/a | a = """print(1, 1+1, 1+1+1)""" |

460 | n/a | self.check(b, a) |

461 | n/a | |

462 | n/a | def test_2(self): |

463 | n/a | b = """print 1, 2""" |

464 | n/a | a = """print(1, 2)""" |

465 | n/a | self.check(b, a) |

466 | n/a | |

467 | n/a | def test_3(self): |

468 | n/a | b = """print""" |

469 | n/a | a = """print()""" |

470 | n/a | self.check(b, a) |

471 | n/a | |

472 | n/a | def test_4(self): |

473 | n/a | # from bug 3000 |

474 | n/a | b = """print whatever; print""" |

475 | n/a | a = """print(whatever); print()""" |

476 | n/a | self.check(b, a) |

477 | n/a | |

478 | n/a | def test_5(self): |

479 | n/a | b = """print; print whatever;""" |

480 | n/a | a = """print(); print(whatever);""" |

481 | n/a | self.check(b, a) |

482 | n/a | |

483 | n/a | def test_tuple(self): |

484 | n/a | b = """print (a, b, c)""" |

485 | n/a | a = """print((a, b, c))""" |

486 | n/a | self.check(b, a) |

487 | n/a | |

488 | n/a | # trailing commas |

489 | n/a | |

490 | n/a | def test_trailing_comma_1(self): |

491 | n/a | b = """print 1, 2, 3,""" |

492 | n/a | a = """print(1, 2, 3, end=' ')""" |

493 | n/a | self.check(b, a) |

494 | n/a | |

495 | n/a | def test_trailing_comma_2(self): |

496 | n/a | b = """print 1, 2,""" |

497 | n/a | a = """print(1, 2, end=' ')""" |

498 | n/a | self.check(b, a) |

499 | n/a | |

500 | n/a | def test_trailing_comma_3(self): |

501 | n/a | b = """print 1,""" |

502 | n/a | a = """print(1, end=' ')""" |

503 | n/a | self.check(b, a) |

504 | n/a | |

505 | n/a | # >> stuff |

506 | n/a | |

507 | n/a | def test_vargs_without_trailing_comma(self): |

508 | n/a | b = """print >>sys.stderr, 1, 2, 3""" |

509 | n/a | a = """print(1, 2, 3, file=sys.stderr)""" |

510 | n/a | self.check(b, a) |

511 | n/a | |

512 | n/a | def test_with_trailing_comma(self): |

513 | n/a | b = """print >>sys.stderr, 1, 2,""" |

514 | n/a | a = """print(1, 2, end=' ', file=sys.stderr)""" |

515 | n/a | self.check(b, a) |

516 | n/a | |

517 | n/a | def test_no_trailing_comma(self): |

518 | n/a | b = """print >>sys.stderr, 1+1""" |

519 | n/a | a = """print(1+1, file=sys.stderr)""" |

520 | n/a | self.check(b, a) |

521 | n/a | |

522 | n/a | def test_spaces_before_file(self): |

523 | n/a | b = """print >> sys.stderr""" |

524 | n/a | a = """print(file=sys.stderr)""" |

525 | n/a | self.check(b, a) |

526 | n/a | |

527 | n/a | def test_with_future_print_function(self): |

528 | n/a | s = "from __future__ import print_function\n" \ |

529 | n/a | "print('Hai!', end=' ')" |

530 | n/a | self.unchanged(s) |

531 | n/a | |

532 | n/a | b = "print 'Hello, world!'" |

533 | n/a | a = "print('Hello, world!')" |

534 | n/a | self.check(b, a) |

535 | n/a | |

536 | n/a | |

537 | n/a | class Test_exec(FixerTestCase): |

538 | n/a | fixer = "exec" |

539 | n/a | |

540 | n/a | def test_prefix_preservation(self): |

541 | n/a | b = """ exec code in ns1, ns2""" |

542 | n/a | a = """ exec(code, ns1, ns2)""" |

543 | n/a | self.check(b, a) |

544 | n/a | |

545 | n/a | def test_basic(self): |

546 | n/a | b = """exec code""" |

547 | n/a | a = """exec(code)""" |

548 | n/a | self.check(b, a) |

549 | n/a | |

550 | n/a | def test_with_globals(self): |

551 | n/a | b = """exec code in ns""" |

552 | n/a | a = """exec(code, ns)""" |

553 | n/a | self.check(b, a) |

554 | n/a | |

555 | n/a | def test_with_globals_locals(self): |

556 | n/a | b = """exec code in ns1, ns2""" |

557 | n/a | a = """exec(code, ns1, ns2)""" |

558 | n/a | self.check(b, a) |

559 | n/a | |

560 | n/a | def test_complex_1(self): |

561 | n/a | b = """exec (a.b()) in ns""" |

562 | n/a | a = """exec((a.b()), ns)""" |

563 | n/a | self.check(b, a) |

564 | n/a | |

565 | n/a | def test_complex_2(self): |

566 | n/a | b = """exec a.b() + c in ns""" |

567 | n/a | a = """exec(a.b() + c, ns)""" |

568 | n/a | self.check(b, a) |

569 | n/a | |

570 | n/a | # These should not be touched |

571 | n/a | |

572 | n/a | def test_unchanged_1(self): |

573 | n/a | s = """exec(code)""" |

574 | n/a | self.unchanged(s) |

575 | n/a | |

576 | n/a | def test_unchanged_2(self): |

577 | n/a | s = """exec (code)""" |

578 | n/a | self.unchanged(s) |

579 | n/a | |

580 | n/a | def test_unchanged_3(self): |

581 | n/a | s = """exec(code, ns)""" |

582 | n/a | self.unchanged(s) |

583 | n/a | |

584 | n/a | def test_unchanged_4(self): |

585 | n/a | s = """exec(code, ns1, ns2)""" |

586 | n/a | self.unchanged(s) |

587 | n/a | |

588 | n/a | class Test_repr(FixerTestCase): |

589 | n/a | fixer = "repr" |

590 | n/a | |

591 | n/a | def test_prefix_preservation(self): |

592 | n/a | b = """x = `1 + 2`""" |

593 | n/a | a = """x = repr(1 + 2)""" |

594 | n/a | self.check(b, a) |

595 | n/a | |

596 | n/a | def test_simple_1(self): |

597 | n/a | b = """x = `1 + 2`""" |

598 | n/a | a = """x = repr(1 + 2)""" |

599 | n/a | self.check(b, a) |

600 | n/a | |

601 | n/a | def test_simple_2(self): |

602 | n/a | b = """y = `x`""" |

603 | n/a | a = """y = repr(x)""" |

604 | n/a | self.check(b, a) |

605 | n/a | |

606 | n/a | def test_complex(self): |

607 | n/a | b = """z = `y`.__repr__()""" |

608 | n/a | a = """z = repr(y).__repr__()""" |

609 | n/a | self.check(b, a) |

610 | n/a | |

611 | n/a | def test_tuple(self): |

612 | n/a | b = """x = `1, 2, 3`""" |

613 | n/a | a = """x = repr((1, 2, 3))""" |

614 | n/a | self.check(b, a) |

615 | n/a | |

616 | n/a | def test_nested(self): |

617 | n/a | b = """x = `1 + `2``""" |

618 | n/a | a = """x = repr(1 + repr(2))""" |

619 | n/a | self.check(b, a) |

620 | n/a | |

621 | n/a | def test_nested_tuples(self): |

622 | n/a | b = """x = `1, 2 + `3, 4``""" |

623 | n/a | a = """x = repr((1, 2 + repr((3, 4))))""" |

624 | n/a | self.check(b, a) |

625 | n/a | |

626 | n/a | class Test_except(FixerTestCase): |

627 | n/a | fixer = "except" |

628 | n/a | |

629 | n/a | def test_prefix_preservation(self): |

630 | n/a | b = """ |

631 | n/a | try: |

632 | n/a | pass |

633 | n/a | except (RuntimeError, ImportError), e: |

634 | n/a | pass""" |

635 | n/a | a = """ |

636 | n/a | try: |

637 | n/a | pass |

638 | n/a | except (RuntimeError, ImportError) as e: |

639 | n/a | pass""" |

640 | n/a | self.check(b, a) |

641 | n/a | |

642 | n/a | def test_simple(self): |

643 | n/a | b = """ |

644 | n/a | try: |

645 | n/a | pass |

646 | n/a | except Foo, e: |

647 | n/a | pass""" |

648 | n/a | a = """ |

649 | n/a | try: |

650 | n/a | pass |

651 | n/a | except Foo as e: |

652 | n/a | pass""" |

653 | n/a | self.check(b, a) |

654 | n/a | |

655 | n/a | def test_simple_no_space_before_target(self): |

656 | n/a | b = """ |

657 | n/a | try: |

658 | n/a | pass |

659 | n/a | except Foo,e: |

660 | n/a | pass""" |

661 | n/a | a = """ |

662 | n/a | try: |

663 | n/a | pass |

664 | n/a | except Foo as e: |

665 | n/a | pass""" |

666 | n/a | self.check(b, a) |

667 | n/a | |

668 | n/a | def test_tuple_unpack(self): |

669 | n/a | b = """ |

670 | n/a | def foo(): |

671 | n/a | try: |

672 | n/a | pass |

673 | n/a | except Exception, (f, e): |

674 | n/a | pass |

675 | n/a | except ImportError, e: |

676 | n/a | pass""" |

677 | n/a | |

678 | n/a | a = """ |

679 | n/a | def foo(): |

680 | n/a | try: |

681 | n/a | pass |

682 | n/a | except Exception as xxx_todo_changeme: |

683 | n/a | (f, e) = xxx_todo_changeme.args |

684 | n/a | pass |

685 | n/a | except ImportError as e: |

686 | n/a | pass""" |

687 | n/a | self.check(b, a) |

688 | n/a | |

689 | n/a | def test_multi_class(self): |

690 | n/a | b = """ |

691 | n/a | try: |

692 | n/a | pass |

693 | n/a | except (RuntimeError, ImportError), e: |

694 | n/a | pass""" |

695 | n/a | |

696 | n/a | a = """ |

697 | n/a | try: |

698 | n/a | pass |

699 | n/a | except (RuntimeError, ImportError) as e: |

700 | n/a | pass""" |

701 | n/a | self.check(b, a) |

702 | n/a | |

703 | n/a | def test_list_unpack(self): |

704 | n/a | b = """ |

705 | n/a | try: |

706 | n/a | pass |

707 | n/a | except Exception, [a, b]: |

708 | n/a | pass""" |

709 | n/a | |

710 | n/a | a = """ |

711 | n/a | try: |

712 | n/a | pass |

713 | n/a | except Exception as xxx_todo_changeme: |

714 | n/a | [a, b] = xxx_todo_changeme.args |

715 | n/a | pass""" |

716 | n/a | self.check(b, a) |

717 | n/a | |

718 | n/a | def test_weird_target_1(self): |

719 | n/a | b = """ |

720 | n/a | try: |

721 | n/a | pass |

722 | n/a | except Exception, d[5]: |

723 | n/a | pass""" |

724 | n/a | |

725 | n/a | a = """ |

726 | n/a | try: |

727 | n/a | pass |

728 | n/a | except Exception as xxx_todo_changeme: |

729 | n/a | d[5] = xxx_todo_changeme |

730 | n/a | pass""" |

731 | n/a | self.check(b, a) |

732 | n/a | |

733 | n/a | def test_weird_target_2(self): |

734 | n/a | b = """ |

735 | n/a | try: |

736 | n/a | pass |

737 | n/a | except Exception, a.foo: |

738 | n/a | pass""" |

739 | n/a | |

740 | n/a | a = """ |

741 | n/a | try: |

742 | n/a | pass |

743 | n/a | except Exception as xxx_todo_changeme: |

744 | n/a | a.foo = xxx_todo_changeme |

745 | n/a | pass""" |

746 | n/a | self.check(b, a) |

747 | n/a | |

748 | n/a | def test_weird_target_3(self): |

749 | n/a | b = """ |

750 | n/a | try: |

751 | n/a | pass |

752 | n/a | except Exception, a().foo: |

753 | n/a | pass""" |

754 | n/a | |

755 | n/a | a = """ |

756 | n/a | try: |

757 | n/a | pass |

758 | n/a | except Exception as xxx_todo_changeme: |

759 | n/a | a().foo = xxx_todo_changeme |

760 | n/a | pass""" |

761 | n/a | self.check(b, a) |

762 | n/a | |

763 | n/a | def test_bare_except(self): |

764 | n/a | b = """ |

765 | n/a | try: |

766 | n/a | pass |

767 | n/a | except Exception, a: |

768 | n/a | pass |

769 | n/a | except: |

770 | n/a | pass""" |

771 | n/a | |

772 | n/a | a = """ |

773 | n/a | try: |

774 | n/a | pass |

775 | n/a | except Exception as a: |

776 | n/a | pass |

777 | n/a | except: |

778 | n/a | pass""" |

779 | n/a | self.check(b, a) |

780 | n/a | |

781 | n/a | def test_bare_except_and_else_finally(self): |

782 | n/a | b = """ |

783 | n/a | try: |

784 | n/a | pass |

785 | n/a | except Exception, a: |

786 | n/a | pass |

787 | n/a | except: |

788 | n/a | pass |

789 | n/a | else: |

790 | n/a | pass |

791 | n/a | finally: |

792 | n/a | pass""" |

793 | n/a | |

794 | n/a | a = """ |

795 | n/a | try: |

796 | n/a | pass |

797 | n/a | except Exception as a: |

798 | n/a | pass |

799 | n/a | except: |

800 | n/a | pass |

801 | n/a | else: |

802 | n/a | pass |

803 | n/a | finally: |

804 | n/a | pass""" |

805 | n/a | self.check(b, a) |

806 | n/a | |

807 | n/a | def test_multi_fixed_excepts_before_bare_except(self): |

808 | n/a | b = """ |

809 | n/a | try: |

810 | n/a | pass |

811 | n/a | except TypeError, b: |

812 | n/a | pass |

813 | n/a | except Exception, a: |

814 | n/a | pass |

815 | n/a | except: |

816 | n/a | pass""" |

817 | n/a | |

818 | n/a | a = """ |

819 | n/a | try: |

820 | n/a | pass |

821 | n/a | except TypeError as b: |

822 | n/a | pass |

823 | n/a | except Exception as a: |

824 | n/a | pass |

825 | n/a | except: |

826 | n/a | pass""" |

827 | n/a | self.check(b, a) |

828 | n/a | |

829 | n/a | def test_one_line_suites(self): |

830 | n/a | b = """ |

831 | n/a | try: raise TypeError |

832 | n/a | except TypeError, e: |

833 | n/a | pass |

834 | n/a | """ |

835 | n/a | a = """ |

836 | n/a | try: raise TypeError |

837 | n/a | except TypeError as e: |

838 | n/a | pass |

839 | n/a | """ |

840 | n/a | self.check(b, a) |

841 | n/a | b = """ |

842 | n/a | try: |

843 | n/a | raise TypeError |

844 | n/a | except TypeError, e: pass |

845 | n/a | """ |

846 | n/a | a = """ |

847 | n/a | try: |

848 | n/a | raise TypeError |

849 | n/a | except TypeError as e: pass |

850 | n/a | """ |

851 | n/a | self.check(b, a) |

852 | n/a | b = """ |

853 | n/a | try: raise TypeError |

854 | n/a | except TypeError, e: pass |

855 | n/a | """ |

856 | n/a | a = """ |

857 | n/a | try: raise TypeError |

858 | n/a | except TypeError as e: pass |

859 | n/a | """ |

860 | n/a | self.check(b, a) |

861 | n/a | b = """ |

862 | n/a | try: raise TypeError |

863 | n/a | except TypeError, e: pass |

864 | n/a | else: function() |

865 | n/a | finally: done() |

866 | n/a | """ |

867 | n/a | a = """ |

868 | n/a | try: raise TypeError |

869 | n/a | except TypeError as e: pass |

870 | n/a | else: function() |

871 | n/a | finally: done() |

872 | n/a | """ |

873 | n/a | self.check(b, a) |

874 | n/a | |

875 | n/a | # These should not be touched: |

876 | n/a | |

877 | n/a | def test_unchanged_1(self): |

878 | n/a | s = """ |

879 | n/a | try: |

880 | n/a | pass |

881 | n/a | except: |

882 | n/a | pass""" |

883 | n/a | self.unchanged(s) |

884 | n/a | |

885 | n/a | def test_unchanged_2(self): |

886 | n/a | s = """ |

887 | n/a | try: |

888 | n/a | pass |

889 | n/a | except Exception: |

890 | n/a | pass""" |

891 | n/a | self.unchanged(s) |

892 | n/a | |

893 | n/a | def test_unchanged_3(self): |

894 | n/a | s = """ |

895 | n/a | try: |

896 | n/a | pass |

897 | n/a | except (Exception, SystemExit): |

898 | n/a | pass""" |

899 | n/a | self.unchanged(s) |

900 | n/a | |

901 | n/a | class Test_raise(FixerTestCase): |

902 | n/a | fixer = "raise" |

903 | n/a | |

904 | n/a | def test_basic(self): |

905 | n/a | b = """raise Exception, 5""" |

906 | n/a | a = """raise Exception(5)""" |

907 | n/a | self.check(b, a) |

908 | n/a | |

909 | n/a | def test_prefix_preservation(self): |

910 | n/a | b = """raise Exception,5""" |

911 | n/a | a = """raise Exception(5)""" |

912 | n/a | self.check(b, a) |

913 | n/a | |

914 | n/a | b = """raise Exception, 5""" |

915 | n/a | a = """raise Exception(5)""" |

916 | n/a | self.check(b, a) |

917 | n/a | |

918 | n/a | def test_with_comments(self): |

919 | n/a | b = """raise Exception, 5 # foo""" |

920 | n/a | a = """raise Exception(5) # foo""" |

921 | n/a | self.check(b, a) |

922 | n/a | |

923 | n/a | b = """raise E, (5, 6) % (a, b) # foo""" |

924 | n/a | a = """raise E((5, 6) % (a, b)) # foo""" |

925 | n/a | self.check(b, a) |

926 | n/a | |

927 | n/a | b = """def foo(): |

928 | n/a | raise Exception, 5, 6 # foo""" |

929 | n/a | a = """def foo(): |

930 | n/a | raise Exception(5).with_traceback(6) # foo""" |

931 | n/a | self.check(b, a) |

932 | n/a | |

933 | n/a | def test_None_value(self): |

934 | n/a | b = """raise Exception(5), None, tb""" |

935 | n/a | a = """raise Exception(5).with_traceback(tb)""" |

936 | n/a | self.check(b, a) |

937 | n/a | |

938 | n/a | def test_tuple_value(self): |

939 | n/a | b = """raise Exception, (5, 6, 7)""" |

940 | n/a | a = """raise Exception(5, 6, 7)""" |

941 | n/a | self.check(b, a) |

942 | n/a | |

943 | n/a | def test_tuple_detection(self): |

944 | n/a | b = """raise E, (5, 6) % (a, b)""" |

945 | n/a | a = """raise E((5, 6) % (a, b))""" |

946 | n/a | self.check(b, a) |

947 | n/a | |

948 | n/a | def test_tuple_exc_1(self): |

949 | n/a | b = """raise (((E1, E2), E3), E4), V""" |

950 | n/a | a = """raise E1(V)""" |

951 | n/a | self.check(b, a) |

952 | n/a | |

953 | n/a | def test_tuple_exc_2(self): |

954 | n/a | b = """raise (E1, (E2, E3), E4), V""" |

955 | n/a | a = """raise E1(V)""" |

956 | n/a | self.check(b, a) |

957 | n/a | |

958 | n/a | # These should produce a warning |

959 | n/a | |

960 | n/a | def test_string_exc(self): |

961 | n/a | s = """raise 'foo'""" |

962 | n/a | self.warns_unchanged(s, "Python 3 does not support string exceptions") |

963 | n/a | |

964 | n/a | def test_string_exc_val(self): |

965 | n/a | s = """raise "foo", 5""" |

966 | n/a | self.warns_unchanged(s, "Python 3 does not support string exceptions") |

967 | n/a | |

968 | n/a | def test_string_exc_val_tb(self): |

969 | n/a | s = """raise "foo", 5, 6""" |

970 | n/a | self.warns_unchanged(s, "Python 3 does not support string exceptions") |

971 | n/a | |

972 | n/a | # These should result in traceback-assignment |

973 | n/a | |

974 | n/a | def test_tb_1(self): |

975 | n/a | b = """def foo(): |

976 | n/a | raise Exception, 5, 6""" |

977 | n/a | a = """def foo(): |

978 | n/a | raise Exception(5).with_traceback(6)""" |

979 | n/a | self.check(b, a) |

980 | n/a | |

981 | n/a | def test_tb_2(self): |

982 | n/a | b = """def foo(): |

983 | n/a | a = 5 |

984 | n/a | raise Exception, 5, 6 |

985 | n/a | b = 6""" |

986 | n/a | a = """def foo(): |

987 | n/a | a = 5 |

988 | n/a | raise Exception(5).with_traceback(6) |

989 | n/a | b = 6""" |

990 | n/a | self.check(b, a) |

991 | n/a | |

992 | n/a | def test_tb_3(self): |

993 | n/a | b = """def foo(): |

994 | n/a | raise Exception,5,6""" |

995 | n/a | a = """def foo(): |

996 | n/a | raise Exception(5).with_traceback(6)""" |

997 | n/a | self.check(b, a) |

998 | n/a | |

999 | n/a | def test_tb_4(self): |

1000 | n/a | b = """def foo(): |

1001 | n/a | a = 5 |

1002 | n/a | raise Exception,5,6 |

1003 | n/a | b = 6""" |

1004 | n/a | a = """def foo(): |

1005 | n/a | a = 5 |

1006 | n/a | raise Exception(5).with_traceback(6) |

1007 | n/a | b = 6""" |

1008 | n/a | self.check(b, a) |

1009 | n/a | |

1010 | n/a | def test_tb_5(self): |

1011 | n/a | b = """def foo(): |

1012 | n/a | raise Exception, (5, 6, 7), 6""" |

1013 | n/a | a = """def foo(): |

1014 | n/a | raise Exception(5, 6, 7).with_traceback(6)""" |

1015 | n/a | self.check(b, a) |

1016 | n/a | |

1017 | n/a | def test_tb_6(self): |

1018 | n/a | b = """def foo(): |

1019 | n/a | a = 5 |

1020 | n/a | raise Exception, (5, 6, 7), 6 |

1021 | n/a | b = 6""" |

1022 | n/a | a = """def foo(): |

1023 | n/a | a = 5 |

1024 | n/a | raise Exception(5, 6, 7).with_traceback(6) |

1025 | n/a | b = 6""" |

1026 | n/a | self.check(b, a) |

1027 | n/a | |

1028 | n/a | class Test_throw(FixerTestCase): |

1029 | n/a | fixer = "throw" |

1030 | n/a | |

1031 | n/a | def test_1(self): |

1032 | n/a | b = """g.throw(Exception, 5)""" |

1033 | n/a | a = """g.throw(Exception(5))""" |

1034 | n/a | self.check(b, a) |

1035 | n/a | |

1036 | n/a | def test_2(self): |

1037 | n/a | b = """g.throw(Exception,5)""" |

1038 | n/a | a = """g.throw(Exception(5))""" |

1039 | n/a | self.check(b, a) |

1040 | n/a | |

1041 | n/a | def test_3(self): |

1042 | n/a | b = """g.throw(Exception, (5, 6, 7))""" |

1043 | n/a | a = """g.throw(Exception(5, 6, 7))""" |

1044 | n/a | self.check(b, a) |

1045 | n/a | |

1046 | n/a | def test_4(self): |

1047 | n/a | b = """5 + g.throw(Exception, 5)""" |

1048 | n/a | a = """5 + g.throw(Exception(5))""" |

1049 | n/a | self.check(b, a) |

1050 | n/a | |

1051 | n/a | # These should produce warnings |

1052 | n/a | |

1053 | n/a | def test_warn_1(self): |

1054 | n/a | s = """g.throw("foo")""" |

1055 | n/a | self.warns_unchanged(s, "Python 3 does not support string exceptions") |

1056 | n/a | |

1057 | n/a | def test_warn_2(self): |

1058 | n/a | s = """g.throw("foo", 5)""" |

1059 | n/a | self.warns_unchanged(s, "Python 3 does not support string exceptions") |

1060 | n/a | |

1061 | n/a | def test_warn_3(self): |

1062 | n/a | s = """g.throw("foo", 5, 6)""" |

1063 | n/a | self.warns_unchanged(s, "Python 3 does not support string exceptions") |

1064 | n/a | |

1065 | n/a | # These should not be touched |

1066 | n/a | |

1067 | n/a | def test_untouched_1(self): |

1068 | n/a | s = """g.throw(Exception)""" |

1069 | n/a | self.unchanged(s) |

1070 | n/a | |

1071 | n/a | def test_untouched_2(self): |

1072 | n/a | s = """g.throw(Exception(5, 6))""" |

1073 | n/a | self.unchanged(s) |

1074 | n/a | |

1075 | n/a | def test_untouched_3(self): |

1076 | n/a | s = """5 + g.throw(Exception(5, 6))""" |

1077 | n/a | self.unchanged(s) |

1078 | n/a | |

1079 | n/a | # These should result in traceback-assignment |

1080 | n/a | |

1081 | n/a | def test_tb_1(self): |

1082 | n/a | b = """def foo(): |

1083 | n/a | g.throw(Exception, 5, 6)""" |

1084 | n/a | a = """def foo(): |

1085 | n/a | g.throw(Exception(5).with_traceback(6))""" |

1086 | n/a | self.check(b, a) |

1087 | n/a | |

1088 | n/a | def test_tb_2(self): |

1089 | n/a | b = """def foo(): |

1090 | n/a | a = 5 |

1091 | n/a | g.throw(Exception, 5, 6) |

1092 | n/a | b = 6""" |

1093 | n/a | a = """def foo(): |

1094 | n/a | a = 5 |

1095 | n/a | g.throw(Exception(5).with_traceback(6)) |

1096 | n/a | b = 6""" |

1097 | n/a | self.check(b, a) |

1098 | n/a | |

1099 | n/a | def test_tb_3(self): |

1100 | n/a | b = """def foo(): |

1101 | n/a | g.throw(Exception,5,6)""" |

1102 | n/a | a = """def foo(): |

1103 | n/a | g.throw(Exception(5).with_traceback(6))""" |

1104 | n/a | self.check(b, a) |

1105 | n/a | |

1106 | n/a | def test_tb_4(self): |

1107 | n/a | b = """def foo(): |

1108 | n/a | a = 5 |

1109 | n/a | g.throw(Exception,5,6) |

1110 | n/a | b = 6""" |

1111 | n/a | a = """def foo(): |

1112 | n/a | a = 5 |

1113 | n/a | g.throw(Exception(5).with_traceback(6)) |

1114 | n/a | b = 6""" |

1115 | n/a | self.check(b, a) |

1116 | n/a | |

1117 | n/a | def test_tb_5(self): |

1118 | n/a | b = """def foo(): |

1119 | n/a | g.throw(Exception, (5, 6, 7), 6)""" |

1120 | n/a | a = """def foo(): |

1121 | n/a | g.throw(Exception(5, 6, 7).with_traceback(6))""" |

1122 | n/a | self.check(b, a) |

1123 | n/a | |

1124 | n/a | def test_tb_6(self): |

1125 | n/a | b = """def foo(): |

1126 | n/a | a = 5 |

1127 | n/a | g.throw(Exception, (5, 6, 7), 6) |

1128 | n/a | b = 6""" |

1129 | n/a | a = """def foo(): |

1130 | n/a | a = 5 |

1131 | n/a | g.throw(Exception(5, 6, 7).with_traceback(6)) |

1132 | n/a | b = 6""" |

1133 | n/a | self.check(b, a) |

1134 | n/a | |

1135 | n/a | def test_tb_7(self): |

1136 | n/a | b = """def foo(): |

1137 | n/a | a + g.throw(Exception, 5, 6)""" |

1138 | n/a | a = """def foo(): |

1139 | n/a | a + g.throw(Exception(5).with_traceback(6))""" |

1140 | n/a | self.check(b, a) |

1141 | n/a | |

1142 | n/a | def test_tb_8(self): |

1143 | n/a | b = """def foo(): |

1144 | n/a | a = 5 |

1145 | n/a | a + g.throw(Exception, 5, 6) |

1146 | n/a | b = 6""" |

1147 | n/a | a = """def foo(): |

1148 | n/a | a = 5 |

1149 | n/a | a + g.throw(Exception(5).with_traceback(6)) |

1150 | n/a | b = 6""" |

1151 | n/a | self.check(b, a) |

1152 | n/a | |

1153 | n/a | class Test_long(FixerTestCase): |

1154 | n/a | fixer = "long" |

1155 | n/a | |

1156 | n/a | def test_1(self): |

1157 | n/a | b = """x = long(x)""" |

1158 | n/a | a = """x = int(x)""" |

1159 | n/a | self.check(b, a) |

1160 | n/a | |

1161 | n/a | def test_2(self): |

1162 | n/a | b = """y = isinstance(x, long)""" |

1163 | n/a | a = """y = isinstance(x, int)""" |

1164 | n/a | self.check(b, a) |

1165 | n/a | |

1166 | n/a | def test_3(self): |

1167 | n/a | b = """z = type(x) in (int, long)""" |

1168 | n/a | a = """z = type(x) in (int, int)""" |

1169 | n/a | self.check(b, a) |

1170 | n/a | |

1171 | n/a | def test_unchanged(self): |

1172 | n/a | s = """long = True""" |

1173 | n/a | self.unchanged(s) |

1174 | n/a | |

1175 | n/a | s = """s.long = True""" |

1176 | n/a | self.unchanged(s) |

1177 | n/a | |

1178 | n/a | s = """def long(): pass""" |

1179 | n/a | self.unchanged(s) |

1180 | n/a | |

1181 | n/a | s = """class long(): pass""" |

1182 | n/a | self.unchanged(s) |

1183 | n/a | |

1184 | n/a | s = """def f(long): pass""" |

1185 | n/a | self.unchanged(s) |

1186 | n/a | |

1187 | n/a | s = """def f(g, long): pass""" |

1188 | n/a | self.unchanged(s) |

1189 | n/a | |

1190 | n/a | s = """def f(x, long=True): pass""" |

1191 | n/a | self.unchanged(s) |

1192 | n/a | |

1193 | n/a | def test_prefix_preservation(self): |

1194 | n/a | b = """x = long( x )""" |

1195 | n/a | a = """x = int( x )""" |

1196 | n/a | self.check(b, a) |

1197 | n/a | |

1198 | n/a | |

1199 | n/a | class Test_execfile(FixerTestCase): |

1200 | n/a | fixer = "execfile" |

1201 | n/a | |

1202 | n/a | def test_conversion(self): |

1203 | n/a | b = """execfile("fn")""" |

1204 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'))""" |

1205 | n/a | self.check(b, a) |

1206 | n/a | |

1207 | n/a | b = """execfile("fn", glob)""" |

1208 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)""" |

1209 | n/a | self.check(b, a) |

1210 | n/a | |

1211 | n/a | b = """execfile("fn", glob, loc)""" |

1212 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)""" |

1213 | n/a | self.check(b, a) |

1214 | n/a | |

1215 | n/a | b = """execfile("fn", globals=glob)""" |

1216 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)""" |

1217 | n/a | self.check(b, a) |

1218 | n/a | |

1219 | n/a | b = """execfile("fn", locals=loc)""" |

1220 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)""" |

1221 | n/a | self.check(b, a) |

1222 | n/a | |

1223 | n/a | b = """execfile("fn", globals=glob, locals=loc)""" |

1224 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)""" |

1225 | n/a | self.check(b, a) |

1226 | n/a | |

1227 | n/a | def test_spacing(self): |

1228 | n/a | b = """execfile( "fn" )""" |

1229 | n/a | a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))""" |

1230 | n/a | self.check(b, a) |

1231 | n/a | |

1232 | n/a | b = """execfile("fn", globals = glob)""" |

1233 | n/a | a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)""" |

1234 | n/a | self.check(b, a) |

1235 | n/a | |

1236 | n/a | |

1237 | n/a | class Test_isinstance(FixerTestCase): |

1238 | n/a | fixer = "isinstance" |

1239 | n/a | |

1240 | n/a | def test_remove_multiple_items(self): |

1241 | n/a | b = """isinstance(x, (int, int, int))""" |

1242 | n/a | a = """isinstance(x, int)""" |

1243 | n/a | self.check(b, a) |

1244 | n/a | |

1245 | n/a | b = """isinstance(x, (int, float, int, int, float))""" |

1246 | n/a | a = """isinstance(x, (int, float))""" |

1247 | n/a | self.check(b, a) |

1248 | n/a | |

1249 | n/a | b = """isinstance(x, (int, float, int, int, float, str))""" |

1250 | n/a | a = """isinstance(x, (int, float, str))""" |

1251 | n/a | self.check(b, a) |

1252 | n/a | |

1253 | n/a | b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))""" |

1254 | n/a | a = """isinstance(foo() + bar(), (x(), y(), x(), int))""" |

1255 | n/a | self.check(b, a) |

1256 | n/a | |

1257 | n/a | def test_prefix_preservation(self): |

1258 | n/a | b = """if isinstance( foo(), ( bar, bar, baz )) : pass""" |

1259 | n/a | a = """if isinstance( foo(), ( bar, baz )) : pass""" |

1260 | n/a | self.check(b, a) |

1261 | n/a | |

1262 | n/a | def test_unchanged(self): |

1263 | n/a | self.unchanged("isinstance(x, (str, int))") |

1264 | n/a | |

1265 | n/a | class Test_dict(FixerTestCase): |

1266 | n/a | fixer = "dict" |

1267 | n/a | |

1268 | n/a | def test_prefix_preservation(self): |

1269 | n/a | b = "if d. keys ( ) : pass" |

1270 | n/a | a = "if list(d. keys ( )) : pass" |

1271 | n/a | self.check(b, a) |

1272 | n/a | |

1273 | n/a | b = "if d. items ( ) : pass" |

1274 | n/a | a = "if list(d. items ( )) : pass" |

1275 | n/a | self.check(b, a) |

1276 | n/a | |

1277 | n/a | b = "if d. iterkeys ( ) : pass" |

1278 | n/a | a = "if iter(d. keys ( )) : pass" |

1279 | n/a | self.check(b, a) |

1280 | n/a | |

1281 | n/a | b = "[i for i in d. iterkeys( ) ]" |

1282 | n/a | a = "[i for i in d. keys( ) ]" |

1283 | n/a | self.check(b, a) |

1284 | n/a | |

1285 | n/a | b = "if d. viewkeys ( ) : pass" |

1286 | n/a | a = "if d. keys ( ) : pass" |

1287 | n/a | self.check(b, a) |

1288 | n/a | |

1289 | n/a | b = "[i for i in d. viewkeys( ) ]" |

1290 | n/a | a = "[i for i in d. keys( ) ]" |

1291 | n/a | self.check(b, a) |

1292 | n/a | |

1293 | n/a | def test_trailing_comment(self): |

1294 | n/a | b = "d.keys() # foo" |

1295 | n/a | a = "list(d.keys()) # foo" |

1296 | n/a | self.check(b, a) |

1297 | n/a | |

1298 | n/a | b = "d.items() # foo" |

1299 | n/a | a = "list(d.items()) # foo" |

1300 | n/a | self.check(b, a) |

1301 | n/a | |

1302 | n/a | b = "d.iterkeys() # foo" |

1303 | n/a | a = "iter(d.keys()) # foo" |

1304 | n/a | self.check(b, a) |

1305 | n/a | |

1306 | n/a | b = """[i for i in d.iterkeys() # foo |

1307 | n/a | ]""" |

1308 | n/a | a = """[i for i in d.keys() # foo |

1309 | n/a | ]""" |

1310 | n/a | self.check(b, a) |

1311 | n/a | |

1312 | n/a | b = """[i for i in d.iterkeys() # foo |

1313 | n/a | ]""" |

1314 | n/a | a = """[i for i in d.keys() # foo |

1315 | n/a | ]""" |

1316 | n/a | self.check(b, a) |

1317 | n/a | |

1318 | n/a | b = "d.viewitems() # foo" |

1319 | n/a | a = "d.items() # foo" |

1320 | n/a | self.check(b, a) |

1321 | n/a | |

1322 | n/a | def test_unchanged(self): |

1323 | n/a | for wrapper in fixer_util.consuming_calls: |

1324 | n/a | s = "s = %s(d.keys())" % wrapper |

1325 | n/a | self.unchanged(s) |

1326 | n/a | |

1327 | n/a | s = "s = %s(d.values())" % wrapper |

1328 | n/a | self.unchanged(s) |

1329 | n/a | |

1330 | n/a | s = "s = %s(d.items())" % wrapper |

1331 | n/a | self.unchanged(s) |

1332 | n/a | |

1333 | n/a | def test_01(self): |

1334 | n/a | b = "d.keys()" |

1335 | n/a | a = "list(d.keys())" |

1336 | n/a | self.check(b, a) |

1337 | n/a | |

1338 | n/a | b = "a[0].foo().keys()" |

1339 | n/a | a = "list(a[0].foo().keys())" |

1340 | n/a | self.check(b, a) |

1341 | n/a | |

1342 | n/a | def test_02(self): |

1343 | n/a | b = "d.items()" |

1344 | n/a | a = "list(d.items())" |

1345 | n/a | self.check(b, a) |

1346 | n/a | |

1347 | n/a | def test_03(self): |

1348 | n/a | b = "d.values()" |

1349 | n/a | a = "list(d.values())" |

1350 | n/a | self.check(b, a) |

1351 | n/a | |

1352 | n/a | def test_04(self): |

1353 | n/a | b = "d.iterkeys()" |

1354 | n/a | a = "iter(d.keys())" |

1355 | n/a | self.check(b, a) |

1356 | n/a | |

1357 | n/a | def test_05(self): |

1358 | n/a | b = "d.iteritems()" |

1359 | n/a | a = "iter(d.items())" |

1360 | n/a | self.check(b, a) |

1361 | n/a | |

1362 | n/a | def test_06(self): |

1363 | n/a | b = "d.itervalues()" |

1364 | n/a | a = "iter(d.values())" |

1365 | n/a | self.check(b, a) |

1366 | n/a | |

1367 | n/a | def test_07(self): |

1368 | n/a | s = "list(d.keys())" |

1369 | n/a | self.unchanged(s) |

1370 | n/a | |

1371 | n/a | def test_08(self): |

1372 | n/a | s = "sorted(d.keys())" |

1373 | n/a | self.unchanged(s) |

1374 | n/a | |

1375 | n/a | def test_09(self): |

1376 | n/a | b = "iter(d.keys())" |

1377 | n/a | a = "iter(list(d.keys()))" |

1378 | n/a | self.check(b, a) |

1379 | n/a | |

1380 | n/a | def test_10(self): |

1381 | n/a | b = "foo(d.keys())" |

1382 | n/a | a = "foo(list(d.keys()))" |

1383 | n/a | self.check(b, a) |

1384 | n/a | |

1385 | n/a | def test_11(self): |

1386 | n/a | b = "for i in d.keys(): print i" |

1387 | n/a | a = "for i in list(d.keys()): print i" |

1388 | n/a | self.check(b, a) |

1389 | n/a | |

1390 | n/a | def test_12(self): |

1391 | n/a | b = "for i in d.iterkeys(): print i" |

1392 | n/a | a = "for i in d.keys(): print i" |

1393 | n/a | self.check(b, a) |

1394 | n/a | |

1395 | n/a | def test_13(self): |

1396 | n/a | b = "[i for i in d.keys()]" |

1397 | n/a | a = "[i for i in list(d.keys())]" |

1398 | n/a | self.check(b, a) |

1399 | n/a | |

1400 | n/a | def test_14(self): |

1401 | n/a | b = "[i for i in d.iterkeys()]" |

1402 | n/a | a = "[i for i in d.keys()]" |

1403 | n/a | self.check(b, a) |

1404 | n/a | |

1405 | n/a | def test_15(self): |

1406 | n/a | b = "(i for i in d.keys())" |

1407 | n/a | a = "(i for i in list(d.keys()))" |

1408 | n/a | self.check(b, a) |

1409 | n/a | |

1410 | n/a | def test_16(self): |

1411 | n/a | b = "(i for i in d.iterkeys())" |

1412 | n/a | a = "(i for i in d.keys())" |

1413 | n/a | self.check(b, a) |

1414 | n/a | |

1415 | n/a | def test_17(self): |

1416 | n/a | b = "iter(d.iterkeys())" |

1417 | n/a | a = "iter(d.keys())" |

1418 | n/a | self.check(b, a) |

1419 | n/a | |

1420 | n/a | def test_18(self): |

1421 | n/a | b = "list(d.iterkeys())" |

1422 | n/a | a = "list(d.keys())" |

1423 | n/a | self.check(b, a) |

1424 | n/a | |

1425 | n/a | def test_19(self): |

1426 | n/a | b = "sorted(d.iterkeys())" |

1427 | n/a | a = "sorted(d.keys())" |

1428 | n/a | self.check(b, a) |

1429 | n/a | |

1430 | n/a | def test_20(self): |

1431 | n/a | b = "foo(d.iterkeys())" |

1432 | n/a | a = "foo(iter(d.keys()))" |

1433 | n/a | self.check(b, a) |

1434 | n/a | |

1435 | n/a | def test_21(self): |

1436 | n/a | b = "print h.iterkeys().next()" |

1437 | n/a | a = "print iter(h.keys()).next()" |

1438 | n/a | self.check(b, a) |

1439 | n/a | |

1440 | n/a | def test_22(self): |

1441 | n/a | b = "print h.keys()[0]" |

1442 | n/a | a = "print list(h.keys())[0]" |

1443 | n/a | self.check(b, a) |

1444 | n/a | |

1445 | n/a | def test_23(self): |

1446 | n/a | b = "print list(h.iterkeys().next())" |

1447 | n/a | a = "print list(iter(h.keys()).next())" |

1448 | n/a | self.check(b, a) |

1449 | n/a | |

1450 | n/a | def test_24(self): |

1451 | n/a | b = "for x in h.keys()[0]: print x" |

1452 | n/a | a = "for x in list(h.keys())[0]: print x" |

1453 | n/a | self.check(b, a) |

1454 | n/a | |

1455 | n/a | def test_25(self): |

1456 | n/a | b = "d.viewkeys()" |

1457 | n/a | a = "d.keys()" |

1458 | n/a | self.check(b, a) |

1459 | n/a | |

1460 | n/a | def test_26(self): |

1461 | n/a | b = "d.viewitems()" |

1462 | n/a | a = "d.items()" |

1463 | n/a | self.check(b, a) |

1464 | n/a | |

1465 | n/a | def test_27(self): |

1466 | n/a | b = "d.viewvalues()" |

1467 | n/a | a = "d.values()" |

1468 | n/a | self.check(b, a) |

1469 | n/a | |

1470 | n/a | def test_28(self): |

1471 | n/a | b = "[i for i in d.viewkeys()]" |

1472 | n/a | a = "[i for i in d.keys()]" |

1473 | n/a | self.check(b, a) |

1474 | n/a | |

1475 | n/a | def test_29(self): |

1476 | n/a | b = "(i for i in d.viewkeys())" |

1477 | n/a | a = "(i for i in d.keys())" |

1478 | n/a | self.check(b, a) |

1479 | n/a | |

1480 | n/a | def test_30(self): |

1481 | n/a | b = "iter(d.viewkeys())" |

1482 | n/a | a = "iter(d.keys())" |

1483 | n/a | self.check(b, a) |

1484 | n/a | |

1485 | n/a | def test_31(self): |

1486 | n/a | b = "list(d.viewkeys())" |

1487 | n/a | a = "list(d.keys())" |

1488 | n/a | self.check(b, a) |

1489 | n/a | |

1490 | n/a | def test_32(self): |

1491 | n/a | b = "sorted(d.viewkeys())" |

1492 | n/a | a = "sorted(d.keys())" |

1493 | n/a | self.check(b, a) |

1494 | n/a | |

1495 | n/a | class Test_xrange(FixerTestCase): |

1496 | n/a | fixer = "xrange" |

1497 | n/a | |

1498 | n/a | def test_prefix_preservation(self): |

1499 | n/a | b = """x = xrange( 10 )""" |

1500 | n/a | a = """x = range( 10 )""" |

1501 | n/a | self.check(b, a) |

1502 | n/a | |

1503 | n/a | b = """x = xrange( 1 , 10 )""" |

1504 | n/a | a = """x = range( 1 , 10 )""" |

1505 | n/a | self.check(b, a) |

1506 | n/a | |

1507 | n/a | b = """x = xrange( 0 , 10 , 2 )""" |

1508 | n/a | a = """x = range( 0 , 10 , 2 )""" |

1509 | n/a | self.check(b, a) |

1510 | n/a | |

1511 | n/a | def test_single_arg(self): |

1512 | n/a | b = """x = xrange(10)""" |

1513 | n/a | a = """x = range(10)""" |

1514 | n/a | self.check(b, a) |

1515 | n/a | |

1516 | n/a | def test_two_args(self): |

1517 | n/a | b = """x = xrange(1, 10)""" |

1518 | n/a | a = """x = range(1, 10)""" |

1519 | n/a | self.check(b, a) |

1520 | n/a | |

1521 | n/a | def test_three_args(self): |

1522 | n/a | b = """x = xrange(0, 10, 2)""" |

1523 | n/a | a = """x = range(0, 10, 2)""" |

1524 | n/a | self.check(b, a) |

1525 | n/a | |

1526 | n/a | def test_wrap_in_list(self): |

1527 | n/a | b = """x = range(10, 3, 9)""" |

1528 | n/a | a = """x = list(range(10, 3, 9))""" |

1529 | n/a | self.check(b, a) |

1530 | n/a | |

1531 | n/a | b = """x = foo(range(10, 3, 9))""" |

1532 | n/a | a = """x = foo(list(range(10, 3, 9)))""" |

1533 | n/a | self.check(b, a) |

1534 | n/a | |

1535 | n/a | b = """x = range(10, 3, 9) + [4]""" |

1536 | n/a | a = """x = list(range(10, 3, 9)) + [4]""" |

1537 | n/a | self.check(b, a) |

1538 | n/a | |

1539 | n/a | b = """x = range(10)[::-1]""" |

1540 | n/a | a = """x = list(range(10))[::-1]""" |

1541 | n/a | self.check(b, a) |

1542 | n/a | |

1543 | n/a | b = """x = range(10) [3]""" |

1544 | n/a | a = """x = list(range(10)) [3]""" |

1545 | n/a | self.check(b, a) |

1546 | n/a | |

1547 | n/a | def test_xrange_in_for(self): |

1548 | n/a | b = """for i in xrange(10):\n j=i""" |

1549 | n/a | a = """for i in range(10):\n j=i""" |

1550 | n/a | self.check(b, a) |

1551 | n/a | |

1552 | n/a | b = """[i for i in xrange(10)]""" |

1553 | n/a | a = """[i for i in range(10)]""" |

1554 | n/a | self.check(b, a) |

1555 | n/a | |

1556 | n/a | def test_range_in_for(self): |

1557 | n/a | self.unchanged("for i in range(10): pass") |

1558 | n/a | self.unchanged("[i for i in range(10)]") |

1559 | n/a | |

1560 | n/a | def test_in_contains_test(self): |

1561 | n/a | self.unchanged("x in range(10, 3, 9)") |

1562 | n/a | |

1563 | n/a | def test_in_consuming_context(self): |

1564 | n/a | for call in fixer_util.consuming_calls: |

1565 | n/a | self.unchanged("a = %s(range(10))" % call) |

1566 | n/a | |

1567 | n/a | class Test_xrange_with_reduce(FixerTestCase): |

1568 | n/a | |

1569 | n/a | def setUp(self): |

1570 | n/a | super(Test_xrange_with_reduce, self).setUp(["xrange", "reduce"]) |

1571 | n/a | |

1572 | n/a | def test_double_transform(self): |

1573 | n/a | b = """reduce(x, xrange(5))""" |

1574 | n/a | a = """from functools import reduce |

1575 | n/a | reduce(x, range(5))""" |

1576 | n/a | self.check(b, a) |

1577 | n/a | |

1578 | n/a | class Test_raw_input(FixerTestCase): |

1579 | n/a | fixer = "raw_input" |

1580 | n/a | |

1581 | n/a | def test_prefix_preservation(self): |

1582 | n/a | b = """x = raw_input( )""" |

1583 | n/a | a = """x = input( )""" |

1584 | n/a | self.check(b, a) |

1585 | n/a | |

1586 | n/a | b = """x = raw_input( '' )""" |

1587 | n/a | a = """x = input( '' )""" |

1588 | n/a | self.check(b, a) |

1589 | n/a | |

1590 | n/a | def test_1(self): |

1591 | n/a | b = """x = raw_input()""" |

1592 | n/a | a = """x = input()""" |

1593 | n/a | self.check(b, a) |

1594 | n/a | |

1595 | n/a | def test_2(self): |

1596 | n/a | b = """x = raw_input('')""" |

1597 | n/a | a = """x = input('')""" |

1598 | n/a | self.check(b, a) |

1599 | n/a | |

1600 | n/a | def test_3(self): |

1601 | n/a | b = """x = raw_input('prompt')""" |

1602 | n/a | a = """x = input('prompt')""" |

1603 | n/a | self.check(b, a) |

1604 | n/a | |

1605 | n/a | def test_4(self): |

1606 | n/a | b = """x = raw_input(foo(a) + 6)""" |

1607 | n/a | a = """x = input(foo(a) + 6)""" |

1608 | n/a | self.check(b, a) |

1609 | n/a | |

1610 | n/a | def test_5(self): |

1611 | n/a | b = """x = raw_input(invite).split()""" |

1612 | n/a | a = """x = input(invite).split()""" |

1613 | n/a | self.check(b, a) |

1614 | n/a | |

1615 | n/a | def test_6(self): |

1616 | n/a | b = """x = raw_input(invite) . split ()""" |

1617 | n/a | a = """x = input(invite) . split ()""" |

1618 | n/a | self.check(b, a) |

1619 | n/a | |

1620 | n/a | def test_8(self): |

1621 | n/a | b = "x = int(raw_input())" |

1622 | n/a | a = "x = int(input())" |

1623 | n/a | self.check(b, a) |

1624 | n/a | |

1625 | n/a | class Test_funcattrs(FixerTestCase): |

1626 | n/a | fixer = "funcattrs" |

1627 | n/a | |

1628 | n/a | attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"] |

1629 | n/a | |

1630 | n/a | def test(self): |

1631 | n/a | for attr in self.attrs: |

1632 | n/a | b = "a.func_%s" % attr |

1633 | n/a | a = "a.__%s__" % attr |

1634 | n/a | self.check(b, a) |

1635 | n/a | |

1636 | n/a | b = "self.foo.func_%s.foo_bar" % attr |

1637 | n/a | a = "self.foo.__%s__.foo_bar" % attr |

1638 | n/a | self.check(b, a) |

1639 | n/a | |

1640 | n/a | def test_unchanged(self): |

1641 | n/a | for attr in self.attrs: |

1642 | n/a | s = "foo(func_%s + 5)" % attr |

1643 | n/a | self.unchanged(s) |

1644 | n/a | |

1645 | n/a | s = "f(foo.__%s__)" % attr |

1646 | n/a | self.unchanged(s) |

1647 | n/a | |

1648 | n/a | s = "f(foo.__%s__.foo)" % attr |

1649 | n/a | self.unchanged(s) |

1650 | n/a | |

1651 | n/a | class Test_xreadlines(FixerTestCase): |

1652 | n/a | fixer = "xreadlines" |

1653 | n/a | |

1654 | n/a | def test_call(self): |

1655 | n/a | b = "for x in f.xreadlines(): pass" |

1656 | n/a | a = "for x in f: pass" |

1657 | n/a | self.check(b, a) |

1658 | n/a | |

1659 | n/a | b = "for x in foo().xreadlines(): pass" |

1660 | n/a | a = "for x in foo(): pass" |

1661 | n/a | self.check(b, a) |

1662 | n/a | |

1663 | n/a | b = "for x in (5 + foo()).xreadlines(): pass" |

1664 | n/a | a = "for x in (5 + foo()): pass" |

1665 | n/a | self.check(b, a) |

1666 | n/a | |

1667 | n/a | def test_attr_ref(self): |

1668 | n/a | b = "foo(f.xreadlines + 5)" |

1669 | n/a | a = "foo(f.__iter__ + 5)" |

1670 | n/a | self.check(b, a) |

1671 | n/a | |

1672 | n/a | b = "foo(f().xreadlines + 5)" |

1673 | n/a | a = "foo(f().__iter__ + 5)" |

1674 | n/a | self.check(b, a) |

1675 | n/a | |

1676 | n/a | b = "foo((5 + f()).xreadlines + 5)" |

1677 | n/a | a = "foo((5 + f()).__iter__ + 5)" |

1678 | n/a | self.check(b, a) |

1679 | n/a | |

1680 | n/a | def test_unchanged(self): |

1681 | n/a | s = "for x in f.xreadlines(5): pass" |

1682 | n/a | self.unchanged(s) |

1683 | n/a | |

1684 | n/a | s = "for x in f.xreadlines(k=5): pass" |

1685 | n/a | self.unchanged(s) |

1686 | n/a | |

1687 | n/a | s = "for x in f.xreadlines(*k, **v): pass" |

1688 | n/a | self.unchanged(s) |

1689 | n/a | |

1690 | n/a | s = "foo(xreadlines)" |

1691 | n/a | self.unchanged(s) |

1692 | n/a | |

1693 | n/a | |

1694 | n/a | class ImportsFixerTests: |

1695 | n/a | |

1696 | n/a | def test_import_module(self): |

1697 | n/a | for old, new in self.modules.items(): |

1698 | n/a | b = "import %s" % old |

1699 | n/a | a = "import %s" % new |

1700 | n/a | self.check(b, a) |

1701 | n/a | |

1702 | n/a | b = "import foo, %s, bar" % old |

1703 | n/a | a = "import foo, %s, bar" % new |

1704 | n/a | self.check(b, a) |

1705 | n/a | |

1706 | n/a | def test_import_from(self): |

1707 | n/a | for old, new in self.modules.items(): |

1708 | n/a | b = "from %s import foo" % old |

1709 | n/a | a = "from %s import foo" % new |

1710 | n/a | self.check(b, a) |

1711 | n/a | |

1712 | n/a | b = "from %s import foo, bar" % old |

1713 | n/a | a = "from %s import foo, bar" % new |

1714 | n/a | self.check(b, a) |

1715 | n/a | |

1716 | n/a | b = "from %s import (yes, no)" % old |

1717 | n/a | a = "from %s import (yes, no)" % new |

1718 | n/a | self.check(b, a) |

1719 | n/a | |

1720 | n/a | def test_import_module_as(self): |

1721 | n/a | for old, new in self.modules.items(): |

1722 | n/a | b = "import %s as foo_bar" % old |

1723 | n/a | a = "import %s as foo_bar" % new |

1724 | n/a | self.check(b, a) |

1725 | n/a | |

1726 | n/a | b = "import %s as foo_bar" % old |

1727 | n/a | a = "import %s as foo_bar" % new |

1728 | n/a | self.check(b, a) |

1729 | n/a | |

1730 | n/a | def test_import_from_as(self): |

1731 | n/a | for old, new in self.modules.items(): |

1732 | n/a | b = "from %s import foo as bar" % old |

1733 | n/a | a = "from %s import foo as bar" % new |

1734 | n/a | self.check(b, a) |

1735 | n/a | |

1736 | n/a | def test_star(self): |

1737 | n/a | for old, new in self.modules.items(): |

1738 | n/a | b = "from %s import *" % old |

1739 | n/a | a = "from %s import *" % new |

1740 | n/a | self.check(b, a) |

1741 | n/a | |

1742 | n/a | def test_import_module_usage(self): |

1743 | n/a | for old, new in self.modules.items(): |

1744 | n/a | b = """ |

1745 | n/a | import %s |

1746 | n/a | foo(%s.bar) |

1747 | n/a | """ % (old, old) |

1748 | n/a | a = """ |

1749 | n/a | import %s |

1750 | n/a | foo(%s.bar) |

1751 | n/a | """ % (new, new) |

1752 | n/a | self.check(b, a) |

1753 | n/a | |

1754 | n/a | b = """ |

1755 | n/a | from %s import x |

1756 | n/a | %s = 23 |

1757 | n/a | """ % (old, old) |

1758 | n/a | a = """ |

1759 | n/a | from %s import x |

1760 | n/a | %s = 23 |

1761 | n/a | """ % (new, old) |

1762 | n/a | self.check(b, a) |

1763 | n/a | |

1764 | n/a | s = """ |

1765 | n/a | def f(): |

1766 | n/a | %s.method() |

1767 | n/a | """ % (old,) |

1768 | n/a | self.unchanged(s) |

1769 | n/a | |

1770 | n/a | # test nested usage |

1771 | n/a | b = """ |

1772 | n/a | import %s |

1773 | n/a | %s.bar(%s.foo) |

1774 | n/a | """ % (old, old, old) |

1775 | n/a | a = """ |

1776 | n/a | import %s |

1777 | n/a | %s.bar(%s.foo) |

1778 | n/a | """ % (new, new, new) |

1779 | n/a | self.check(b, a) |

1780 | n/a | |

1781 | n/a | b = """ |

1782 | n/a | import %s |

1783 | n/a | x.%s |

1784 | n/a | """ % (old, old) |

1785 | n/a | a = """ |

1786 | n/a | import %s |

1787 | n/a | x.%s |

1788 | n/a | """ % (new, old) |

1789 | n/a | self.check(b, a) |

1790 | n/a | |

1791 | n/a | |

1792 | n/a | class Test_imports(FixerTestCase, ImportsFixerTests): |

1793 | n/a | fixer = "imports" |

1794 | n/a | from ..fixes.fix_imports import MAPPING as modules |

1795 | n/a | |

1796 | n/a | def test_multiple_imports(self): |

1797 | n/a | b = """import urlparse, cStringIO""" |

1798 | n/a | a = """import urllib.parse, io""" |

1799 | n/a | self.check(b, a) |

1800 | n/a | |

1801 | n/a | def test_multiple_imports_as(self): |

1802 | n/a | b = """ |

1803 | n/a | import copy_reg as bar, HTMLParser as foo, urlparse |

1804 | n/a | s = urlparse.spam(bar.foo()) |

1805 | n/a | """ |

1806 | n/a | a = """ |

1807 | n/a | import copyreg as bar, html.parser as foo, urllib.parse |

1808 | n/a | s = urllib.parse.spam(bar.foo()) |

1809 | n/a | """ |

1810 | n/a | self.check(b, a) |

1811 | n/a | |

1812 | n/a | |

1813 | n/a | class Test_imports2(FixerTestCase, ImportsFixerTests): |

1814 | n/a | fixer = "imports2" |

1815 | n/a | from ..fixes.fix_imports2 import MAPPING as modules |

1816 | n/a | |

1817 | n/a | |

1818 | n/a | class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests): |

1819 | n/a | |

1820 | n/a | def setUp(self): |

1821 | n/a | super(Test_imports_fixer_order, self).setUp(['imports', 'imports2']) |

1822 | n/a | from ..fixes.fix_imports2 import MAPPING as mapping2 |

1823 | n/a | self.modules = mapping2.copy() |

1824 | n/a | from ..fixes.fix_imports import MAPPING as mapping1 |

1825 | n/a | for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'): |

1826 | n/a | self.modules[key] = mapping1[key] |

1827 | n/a | |

1828 | n/a | def test_after_local_imports_refactoring(self): |

1829 | n/a | for fix in ("imports", "imports2"): |

1830 | n/a | self.fixer = fix |

1831 | n/a | self.assert_runs_after("import") |

1832 | n/a | |

1833 | n/a | |

1834 | n/a | class Test_urllib(FixerTestCase): |

1835 | n/a | fixer = "urllib" |

1836 | n/a | from ..fixes.fix_urllib import MAPPING as modules |

1837 | n/a | |

1838 | n/a | def test_import_module(self): |

1839 | n/a | for old, changes in self.modules.items(): |

1840 | n/a | b = "import %s" % old |

1841 | n/a | a = "import %s" % ", ".join(map(itemgetter(0), changes)) |

1842 | n/a | self.check(b, a) |

1843 | n/a | |

1844 | n/a | def test_import_from(self): |

1845 | n/a | for old, changes in self.modules.items(): |

1846 | n/a | all_members = [] |

1847 | n/a | for new, members in changes: |

1848 | n/a | for member in members: |

1849 | n/a | all_members.append(member) |

1850 | n/a | b = "from %s import %s" % (old, member) |

1851 | n/a | a = "from %s import %s" % (new, member) |

1852 | n/a | self.check(b, a) |

1853 | n/a | |

1854 | n/a | s = "from foo import %s" % member |

1855 | n/a | self.unchanged(s) |

1856 | n/a | |

1857 | n/a | b = "from %s import %s" % (old, ", ".join(members)) |

1858 | n/a | a = "from %s import %s" % (new, ", ".join(members)) |

1859 | n/a | self.check(b, a) |

1860 | n/a | |

1861 | n/a | s = "from foo import %s" % ", ".join(members) |

1862 | n/a | self.unchanged(s) |

1863 | n/a | |

1864 | n/a | # test the breaking of a module into multiple replacements |

1865 | n/a | b = "from %s import %s" % (old, ", ".join(all_members)) |

1866 | n/a | a = "\n".join(["from %s import %s" % (new, ", ".join(members)) |

1867 | n/a | for (new, members) in changes]) |

1868 | n/a | self.check(b, a) |

1869 | n/a | |

1870 | n/a | def test_import_module_as(self): |

1871 | n/a | for old in self.modules: |

1872 | n/a | s = "import %s as foo" % old |

1873 | n/a | self.warns_unchanged(s, "This module is now multiple modules") |

1874 | n/a | |

1875 | n/a | def test_import_from_as(self): |

1876 | n/a | for old, changes in self.modules.items(): |

1877 | n/a | for new, members in changes: |

1878 | n/a | for member in members: |

1879 | n/a | b = "from %s import %s as foo_bar" % (old, member) |

1880 | n/a | a = "from %s import %s as foo_bar" % (new, member) |

1881 | n/a | self.check(b, a) |

1882 | n/a | b = "from %s import %s as blah, %s" % (old, member, member) |

1883 | n/a | a = "from %s import %s as blah, %s" % (new, member, member) |

1884 | n/a | self.check(b, a) |

1885 | n/a | |

1886 | n/a | def test_star(self): |

1887 | n/a | for old in self.modules: |

1888 | n/a | s = "from %s import *" % old |

1889 | n/a | self.warns_unchanged(s, "Cannot handle star imports") |

1890 | n/a | |

1891 | n/a | def test_indented(self): |

1892 | n/a | b = """ |

1893 | n/a | def foo(): |

1894 | n/a | from urllib import urlencode, urlopen |

1895 | n/a | """ |

1896 | n/a | a = """ |

1897 | n/a | def foo(): |

1898 | n/a | from urllib.parse import urlencode |

1899 | n/a | from urllib.request import urlopen |

1900 | n/a | """ |

1901 | n/a | self.check(b, a) |

1902 | n/a | |

1903 | n/a | b = """ |

1904 | n/a | def foo(): |

1905 | n/a | other() |

1906 | n/a | from urllib import urlencode, urlopen |

1907 | n/a | """ |

1908 | n/a | a = """ |

1909 | n/a | def foo(): |

1910 | n/a | other() |

1911 | n/a | from urllib.parse import urlencode |

1912 | n/a | from urllib.request import urlopen |

1913 | n/a | """ |

1914 | n/a | self.check(b, a) |

1915 | n/a | |

1916 | n/a | |

1917 | n/a | |

1918 | n/a | def test_import_module_usage(self): |

1919 | n/a | for old, changes in self.modules.items(): |

1920 | n/a | for new, members in changes: |

1921 | n/a | for member in members: |

1922 | n/a | new_import = ", ".join([n for (n, mems) |

1923 | n/a | in self.modules[old]]) |

1924 | n/a | b = """ |

1925 | n/a | import %s |

1926 | n/a | foo(%s.%s) |

1927 | n/a | """ % (old, old, member) |

1928 | n/a | a = """ |

1929 | n/a | import %s |

1930 | n/a | foo(%s.%s) |

1931 | n/a | """ % (new_import, new, member) |

1932 | n/a | self.check(b, a) |

1933 | n/a | b = """ |

1934 | n/a | import %s |

1935 | n/a | %s.%s(%s.%s) |

1936 | n/a | """ % (old, old, member, old, member) |

1937 | n/a | a = """ |

1938 | n/a | import %s |

1939 | n/a | %s.%s(%s.%s) |

1940 | n/a | """ % (new_import, new, member, new, member) |

1941 | n/a | self.check(b, a) |

1942 | n/a | |

1943 | n/a | |

1944 | n/a | class Test_input(FixerTestCase): |

1945 | n/a | fixer = "input" |

1946 | n/a | |

1947 | n/a | def test_prefix_preservation(self): |

1948 | n/a | b = """x = input( )""" |

1949 | n/a | a = """x = eval(input( ))""" |

1950 | n/a | self.check(b, a) |

1951 | n/a | |

1952 | n/a | b = """x = input( '' )""" |

1953 | n/a | a = """x = eval(input( '' ))""" |

1954 | n/a | self.check(b, a) |

1955 | n/a | |

1956 | n/a | def test_trailing_comment(self): |

1957 | n/a | b = """x = input() # foo""" |

1958 | n/a | a = """x = eval(input()) # foo""" |

1959 | n/a | self.check(b, a) |

1960 | n/a | |

1961 | n/a | def test_idempotency(self): |

1962 | n/a | s = """x = eval(input())""" |

1963 | n/a | self.unchanged(s) |

1964 | n/a | |

1965 | n/a | s = """x = eval(input(''))""" |

1966 | n/a | self.unchanged(s) |

1967 | n/a | |

1968 | n/a | s = """x = eval(input(foo(5) + 9))""" |

1969 | n/a | self.unchanged(s) |

1970 | n/a | |

1971 | n/a | def test_1(self): |

1972 | n/a | b = """x = input()""" |

1973 | n/a | a = """x = eval(input())""" |

1974 | n/a | self.check(b, a) |

1975 | n/a | |

1976 | n/a | def test_2(self): |

1977 | n/a | b = """x = input('')""" |

1978 | n/a | a = """x = eval(input(''))""" |

1979 | n/a | self.check(b, a) |

1980 | n/a | |

1981 | n/a | def test_3(self): |

1982 | n/a | b = """x = input('prompt')""" |

1983 | n/a | a = """x = eval(input('prompt'))""" |

1984 | n/a | self.check(b, a) |

1985 | n/a | |

1986 | n/a | def test_4(self): |

1987 | n/a | b = """x = input(foo(5) + 9)""" |

1988 | n/a | a = """x = eval(input(foo(5) + 9))""" |

1989 | n/a | self.check(b, a) |

1990 | n/a | |

1991 | n/a | class Test_tuple_params(FixerTestCase): |

1992 | n/a | fixer = "tuple_params" |

1993 | n/a | |

1994 | n/a | def test_unchanged_1(self): |

1995 | n/a | s = """def foo(): pass""" |

1996 | n/a | self.unchanged(s) |

1997 | n/a | |

1998 | n/a | def test_unchanged_2(self): |

1999 | n/a | s = """def foo(a, b, c): pass""" |

2000 | n/a | self.unchanged(s) |

2001 | n/a | |

2002 | n/a | def test_unchanged_3(self): |

2003 | n/a | s = """def foo(a=3, b=4, c=5): pass""" |

2004 | n/a | self.unchanged(s) |

2005 | n/a | |

2006 | n/a | def test_1(self): |

2007 | n/a | b = """ |

2008 | n/a | def foo(((a, b), c)): |

2009 | n/a | x = 5""" |

2010 | n/a | |

2011 | n/a | a = """ |

2012 | n/a | def foo(xxx_todo_changeme): |

2013 | n/a | ((a, b), c) = xxx_todo_changeme |

2014 | n/a | x = 5""" |

2015 | n/a | self.check(b, a) |

2016 | n/a | |

2017 | n/a | def test_2(self): |

2018 | n/a | b = """ |

2019 | n/a | def foo(((a, b), c), d): |

2020 | n/a | x = 5""" |

2021 | n/a | |

2022 | n/a | a = """ |

2023 | n/a | def foo(xxx_todo_changeme, d): |

2024 | n/a | ((a, b), c) = xxx_todo_changeme |

2025 | n/a | x = 5""" |

2026 | n/a | self.check(b, a) |

2027 | n/a | |

2028 | n/a | def test_3(self): |

2029 | n/a | b = """ |

2030 | n/a | def foo(((a, b), c), d) -> e: |

2031 | n/a | x = 5""" |

2032 | n/a | |

2033 | n/a | a = """ |

2034 | n/a | def foo(xxx_todo_changeme, d) -> e: |

2035 | n/a | ((a, b), c) = xxx_todo_changeme |

2036 | n/a | x = 5""" |

2037 | n/a | self.check(b, a) |

2038 | n/a | |

2039 | n/a | def test_semicolon(self): |

2040 | n/a | b = """ |

2041 | n/a | def foo(((a, b), c)): x = 5; y = 7""" |

2042 | n/a | |

2043 | n/a | a = """ |

2044 | n/a | def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7""" |

2045 | n/a | self.check(b, a) |

2046 | n/a | |

2047 | n/a | def test_keywords(self): |

2048 | n/a | b = """ |

2049 | n/a | def foo(((a, b), c), d, e=5) -> z: |

2050 | n/a | x = 5""" |

2051 | n/a | |

2052 | n/a | a = """ |

2053 | n/a | def foo(xxx_todo_changeme, d, e=5) -> z: |

2054 | n/a | ((a, b), c) = xxx_todo_changeme |

2055 | n/a | x = 5""" |

2056 | n/a | self.check(b, a) |

2057 | n/a | |

2058 | n/a | def test_varargs(self): |

2059 | n/a | b = """ |

2060 | n/a | def foo(((a, b), c), d, *vargs, **kwargs) -> z: |

2061 | n/a | x = 5""" |

2062 | n/a | |

2063 | n/a | a = """ |

2064 | n/a | def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z: |

2065 | n/a | ((a, b), c) = xxx_todo_changeme |

2066 | n/a | x = 5""" |

2067 | n/a | self.check(b, a) |

2068 | n/a | |

2069 | n/a | def test_multi_1(self): |

2070 | n/a | b = """ |

2071 | n/a | def foo(((a, b), c), (d, e, f)) -> z: |

2072 | n/a | x = 5""" |

2073 | n/a | |

2074 | n/a | a = """ |

2075 | n/a | def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z: |

2076 | n/a | ((a, b), c) = xxx_todo_changeme |

2077 | n/a | (d, e, f) = xxx_todo_changeme1 |

2078 | n/a | x = 5""" |

2079 | n/a | self.check(b, a) |

2080 | n/a | |

2081 | n/a | def test_multi_2(self): |

2082 | n/a | b = """ |

2083 | n/a | def foo(x, ((a, b), c), d, (e, f, g), y) -> z: |

2084 | n/a | x = 5""" |

2085 | n/a | |

2086 | n/a | a = """ |

2087 | n/a | def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z: |

2088 | n/a | ((a, b), c) = xxx_todo_changeme |

2089 | n/a | (e, f, g) = xxx_todo_changeme1 |

2090 | n/a | x = 5""" |

2091 | n/a | self.check(b, a) |

2092 | n/a | |

2093 | n/a | def test_docstring(self): |

2094 | n/a | b = """ |

2095 | n/a | def foo(((a, b), c), (d, e, f)) -> z: |

2096 | n/a | "foo foo foo foo" |

2097 | n/a | x = 5""" |

2098 | n/a | |

2099 | n/a | a = """ |

2100 | n/a | def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z: |

2101 | n/a | "foo foo foo foo" |

2102 | n/a | ((a, b), c) = xxx_todo_changeme |

2103 | n/a | (d, e, f) = xxx_todo_changeme1 |

2104 | n/a | x = 5""" |

2105 | n/a | self.check(b, a) |

2106 | n/a | |

2107 | n/a | def test_lambda_no_change(self): |

2108 | n/a | s = """lambda x: x + 5""" |

2109 | n/a | self.unchanged(s) |

2110 | n/a | |

2111 | n/a | def test_lambda_parens_single_arg(self): |

2112 | n/a | b = """lambda (x): x + 5""" |

2113 | n/a | a = """lambda x: x + 5""" |

2114 | n/a | self.check(b, a) |

2115 | n/a | |

2116 | n/a | b = """lambda(x): x + 5""" |

2117 | n/a | a = """lambda x: x + 5""" |

2118 | n/a | self.check(b, a) |

2119 | n/a | |

2120 | n/a | b = """lambda ((((x)))): x + 5""" |

2121 | n/a | a = """lambda x: x + 5""" |

2122 | n/a | self.check(b, a) |

2123 | n/a | |

2124 | n/a | b = """lambda((((x)))): x + 5""" |

2125 | n/a | a = """lambda x: x + 5""" |

2126 | n/a | self.check(b, a) |

2127 | n/a | |

2128 | n/a | def test_lambda_simple(self): |

2129 | n/a | b = """lambda (x, y): x + f(y)""" |

2130 | n/a | a = """lambda x_y: x_y[0] + f(x_y[1])""" |

2131 | n/a | self.check(b, a) |

2132 | n/a | |

2133 | n/a | b = """lambda(x, y): x + f(y)""" |

2134 | n/a | a = """lambda x_y: x_y[0] + f(x_y[1])""" |

2135 | n/a | self.check(b, a) |

2136 | n/a | |

2137 | n/a | b = """lambda (((x, y))): x + f(y)""" |

2138 | n/a | a = """lambda x_y: x_y[0] + f(x_y[1])""" |

2139 | n/a | self.check(b, a) |

2140 | n/a | |

2141 | n/a | b = """lambda(((x, y))): x + f(y)""" |

2142 | n/a | a = """lambda x_y: x_y[0] + f(x_y[1])""" |

2143 | n/a | self.check(b, a) |

2144 | n/a | |

2145 | n/a | def test_lambda_one_tuple(self): |

2146 | n/a | b = """lambda (x,): x + f(x)""" |

2147 | n/a | a = """lambda x1: x1[0] + f(x1[0])""" |

2148 | n/a | self.check(b, a) |

2149 | n/a | |

2150 | n/a | b = """lambda (((x,))): x + f(x)""" |

2151 | n/a | a = """lambda x1: x1[0] + f(x1[0])""" |

2152 | n/a | self.check(b, a) |

2153 | n/a | |

2154 | n/a | def test_lambda_simple_multi_use(self): |

2155 | n/a | b = """lambda (x, y): x + x + f(x) + x""" |

2156 | n/a | a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]""" |

2157 | n/a | self.check(b, a) |

2158 | n/a | |

2159 | n/a | def test_lambda_simple_reverse(self): |

2160 | n/a | b = """lambda (x, y): y + x""" |

2161 | n/a | a = """lambda x_y: x_y[1] + x_y[0]""" |

2162 | n/a | self.check(b, a) |

2163 | n/a | |

2164 | n/a | def test_lambda_nested(self): |

2165 | n/a | b = """lambda (x, (y, z)): x + y + z""" |

2166 | n/a | a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]""" |

2167 | n/a | self.check(b, a) |

2168 | n/a | |

2169 | n/a | b = """lambda (((x, (y, z)))): x + y + z""" |

2170 | n/a | a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]""" |

2171 | n/a | self.check(b, a) |

2172 | n/a | |

2173 | n/a | def test_lambda_nested_multi_use(self): |

2174 | n/a | b = """lambda (x, (y, z)): x + y + f(y)""" |

2175 | n/a | a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])""" |

2176 | n/a | self.check(b, a) |

2177 | n/a | |

2178 | n/a | class Test_methodattrs(FixerTestCase): |

2179 | n/a | fixer = "methodattrs" |

2180 | n/a | |

2181 | n/a | attrs = ["func", "self", "class"] |

2182 | n/a | |

2183 | n/a | def test(self): |

2184 | n/a | for attr in self.attrs: |

2185 | n/a | b = "a.im_%s" % attr |

2186 | n/a | if attr == "class": |

2187 | n/a | a = "a.__self__.__class__" |

2188 | n/a | else: |

2189 | n/a | a = "a.__%s__" % attr |

2190 | n/a | self.check(b, a) |

2191 | n/a | |

2192 | n/a | b = "self.foo.im_%s.foo_bar" % attr |

2193 | n/a | if attr == "class": |

2194 | n/a | a = "self.foo.__self__.__class__.foo_bar" |

2195 | n/a | else: |

2196 | n/a | a = "self.foo.__%s__.foo_bar" % attr |

2197 | n/a | self.check(b, a) |

2198 | n/a | |

2199 | n/a | def test_unchanged(self): |

2200 | n/a | for attr in self.attrs: |

2201 | n/a | s = "foo(im_%s + 5)" % attr |

2202 | n/a | self.unchanged(s) |

2203 | n/a | |

2204 | n/a | s = "f(foo.__%s__)" % attr |

2205 | n/a | self.unchanged(s) |

2206 | n/a | |

2207 | n/a | s = "f(foo.__%s__.foo)" % attr |

2208 | n/a | self.unchanged(s) |

2209 | n/a | |

2210 | n/a | class Test_next(FixerTestCase): |

2211 | n/a | fixer = "next" |

2212 | n/a | |

2213 | n/a | def test_1(self): |

2214 | n/a | b = """it.next()""" |

2215 | n/a | a = """next(it)""" |

2216 | n/a | self.check(b, a) |

2217 | n/a | |

2218 | n/a | def test_2(self): |

2219 | n/a | b = """a.b.c.d.next()""" |

2220 | n/a | a = """next(a.b.c.d)""" |

2221 | n/a | self.check(b, a) |

2222 | n/a | |

2223 | n/a | def test_3(self): |

2224 | n/a | b = """(a + b).next()""" |

2225 | n/a | a = """next((a + b))""" |

2226 | n/a | self.check(b, a) |

2227 | n/a | |

2228 | n/a | def test_4(self): |

2229 | n/a | b = """a().next()""" |

2230 | n/a | a = """next(a())""" |

2231 | n/a | self.check(b, a) |

2232 | n/a | |

2233 | n/a | def test_5(self): |

2234 | n/a | b = """a().next() + b""" |

2235 | n/a | a = """next(a()) + b""" |

2236 | n/a | self.check(b, a) |

2237 | n/a | |

2238 | n/a | def test_6(self): |

2239 | n/a | b = """c( a().next() + b)""" |

2240 | n/a | a = """c( next(a()) + b)""" |

2241 | n/a | self.check(b, a) |

2242 | n/a | |

2243 | n/a | def test_prefix_preservation_1(self): |

2244 | n/a | b = """ |

2245 | n/a | for a in b: |

2246 | n/a | foo(a) |

2247 | n/a | a.next() |

2248 | n/a | """ |

2249 | n/a | a = """ |

2250 | n/a | for a in b: |

2251 | n/a | foo(a) |

2252 | n/a | next(a) |

2253 | n/a | """ |

2254 | n/a | self.check(b, a) |

2255 | n/a | |

2256 | n/a | def test_prefix_preservation_2(self): |

2257 | n/a | b = """ |

2258 | n/a | for a in b: |

2259 | n/a | foo(a) # abc |

2260 | n/a | # def |

2261 | n/a | a.next() |

2262 | n/a | """ |

2263 | n/a | a = """ |

2264 | n/a | for a in b: |

2265 | n/a | foo(a) # abc |

2266 | n/a | # def |

2267 | n/a | next(a) |

2268 | n/a | """ |

2269 | n/a | self.check(b, a) |

2270 | n/a | |

2271 | n/a | def test_prefix_preservation_3(self): |

2272 | n/a | b = """ |

2273 | n/a | next = 5 |

2274 | n/a | for a in b: |

2275 | n/a | foo(a) |

2276 | n/a | a.next() |

2277 | n/a | """ |

2278 | n/a | a = """ |

2279 | n/a | next = 5 |

2280 | n/a | for a in b: |

2281 | n/a | foo(a) |

2282 | n/a | a.__next__() |

2283 | n/a | """ |

2284 | n/a | self.check(b, a, ignore_warnings=True) |

2285 | n/a | |

2286 | n/a | def test_prefix_preservation_4(self): |

2287 | n/a | b = """ |

2288 | n/a | next = 5 |

2289 | n/a | for a in b: |

2290 | n/a | foo(a) # abc |

2291 | n/a | # def |

2292 | n/a | a.next() |

2293 | n/a | """ |

2294 | n/a | a = """ |

2295 | n/a | next = 5 |

2296 | n/a | for a in b: |

2297 | n/a | foo(a) # abc |

2298 | n/a | # def |

2299 | n/a | a.__next__() |

2300 | n/a | """ |

2301 | n/a | self.check(b, a, ignore_warnings=True) |

2302 | n/a | |

2303 | n/a | def test_prefix_preservation_5(self): |

2304 | n/a | b = """ |

2305 | n/a | next = 5 |

2306 | n/a | for a in b: |

2307 | n/a | foo(foo(a), # abc |

2308 | n/a | a.next()) |

2309 | n/a | """ |

2310 | n/a | a = """ |

2311 | n/a | next = 5 |

2312 | n/a | for a in b: |

2313 | n/a | foo(foo(a), # abc |

2314 | n/a | a.__next__()) |

2315 | n/a | """ |

2316 | n/a | self.check(b, a, ignore_warnings=True) |

2317 | n/a | |

2318 | n/a | def test_prefix_preservation_6(self): |

2319 | n/a | b = """ |

2320 | n/a | for a in b: |

2321 | n/a | foo(foo(a), # abc |

2322 | n/a | a.next()) |

2323 | n/a | """ |

2324 | n/a | a = """ |

2325 | n/a | for a in b: |

2326 | n/a | foo(foo(a), # abc |

2327 | n/a | next(a)) |

2328 | n/a | """ |

2329 | n/a | self.check(b, a) |

2330 | n/a | |

2331 | n/a | def test_method_1(self): |

2332 | n/a | b = """ |

2333 | n/a | class A: |

2334 | n/a | def next(self): |

2335 | n/a | pass |

2336 | n/a | """ |

2337 | n/a | a = """ |

2338 | n/a | class A: |

2339 | n/a | def __next__(self): |

2340 | n/a | pass |

2341 | n/a | """ |

2342 | n/a | self.check(b, a) |

2343 | n/a | |

2344 | n/a | def test_method_2(self): |

2345 | n/a | b = """ |

2346 | n/a | class A(object): |

2347 | n/a | def next(self): |

2348 | n/a | pass |

2349 | n/a | """ |

2350 | n/a | a = """ |

2351 | n/a | class A(object): |

2352 | n/a | def __next__(self): |

2353 | n/a | pass |

2354 | n/a | """ |

2355 | n/a | self.check(b, a) |

2356 | n/a | |

2357 | n/a | def test_method_3(self): |

2358 | n/a | b = """ |

2359 | n/a | class A: |

2360 | n/a | def next(x): |

2361 | n/a | pass |

2362 | n/a | """ |

2363 | n/a | a = """ |

2364 | n/a | class A: |

2365 | n/a | def __next__(x): |

2366 | n/a | pass |

2367 | n/a | """ |

2368 | n/a | self.check(b, a) |

2369 | n/a | |

2370 | n/a | def test_method_4(self): |

2371 | n/a | b = """ |

2372 | n/a | class A: |

2373 | n/a | def __init__(self, foo): |

2374 | n/a | self.foo = foo |

2375 | n/a | |

2376 | n/a | def next(self): |

2377 | n/a | pass |

2378 | n/a | |

2379 | n/a | def __iter__(self): |

2380 | n/a | return self |

2381 | n/a | """ |

2382 | n/a | a = """ |

2383 | n/a | class A: |

2384 | n/a | def __init__(self, foo): |

2385 | n/a | self.foo = foo |

2386 | n/a | |

2387 | n/a | def __next__(self): |

2388 | n/a | pass |

2389 | n/a | |

2390 | n/a | def __iter__(self): |

2391 | n/a | return self |

2392 | n/a | """ |

2393 | n/a | self.check(b, a) |

2394 | n/a | |

2395 | n/a | def test_method_unchanged(self): |

2396 | n/a | s = """ |

2397 | n/a | class A: |

2398 | n/a | def next(self, a, b): |

2399 | n/a | pass |

2400 | n/a | """ |

2401 | n/a | self.unchanged(s) |

2402 | n/a | |

2403 | n/a | def test_shadowing_assign_simple(self): |

2404 | n/a | s = """ |

2405 | n/a | next = foo |

2406 | n/a | |

2407 | n/a | class A: |

2408 | n/a | def next(self, a, b): |

2409 | n/a | pass |

2410 | n/a | """ |

2411 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2412 | n/a | |

2413 | n/a | def test_shadowing_assign_tuple_1(self): |

2414 | n/a | s = """ |

2415 | n/a | (next, a) = foo |

2416 | n/a | |

2417 | n/a | class A: |

2418 | n/a | def next(self, a, b): |

2419 | n/a | pass |

2420 | n/a | """ |

2421 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2422 | n/a | |

2423 | n/a | def test_shadowing_assign_tuple_2(self): |

2424 | n/a | s = """ |

2425 | n/a | (a, (b, (next, c)), a) = foo |

2426 | n/a | |

2427 | n/a | class A: |

2428 | n/a | def next(self, a, b): |

2429 | n/a | pass |

2430 | n/a | """ |

2431 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2432 | n/a | |

2433 | n/a | def test_shadowing_assign_list_1(self): |

2434 | n/a | s = """ |

2435 | n/a | [next, a] = foo |

2436 | n/a | |

2437 | n/a | class A: |

2438 | n/a | def next(self, a, b): |

2439 | n/a | pass |

2440 | n/a | """ |

2441 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2442 | n/a | |

2443 | n/a | def test_shadowing_assign_list_2(self): |

2444 | n/a | s = """ |

2445 | n/a | [a, [b, [next, c]], a] = foo |

2446 | n/a | |

2447 | n/a | class A: |

2448 | n/a | def next(self, a, b): |

2449 | n/a | pass |

2450 | n/a | """ |

2451 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2452 | n/a | |

2453 | n/a | def test_builtin_assign(self): |

2454 | n/a | s = """ |

2455 | n/a | def foo(): |

2456 | n/a | __builtin__.next = foo |

2457 | n/a | |

2458 | n/a | class A: |

2459 | n/a | def next(self, a, b): |

2460 | n/a | pass |

2461 | n/a | """ |

2462 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2463 | n/a | |

2464 | n/a | def test_builtin_assign_in_tuple(self): |

2465 | n/a | s = """ |

2466 | n/a | def foo(): |

2467 | n/a | (a, __builtin__.next) = foo |

2468 | n/a | |

2469 | n/a | class A: |

2470 | n/a | def next(self, a, b): |

2471 | n/a | pass |

2472 | n/a | """ |

2473 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2474 | n/a | |

2475 | n/a | def test_builtin_assign_in_list(self): |

2476 | n/a | s = """ |

2477 | n/a | def foo(): |

2478 | n/a | [a, __builtin__.next] = foo |

2479 | n/a | |

2480 | n/a | class A: |

2481 | n/a | def next(self, a, b): |

2482 | n/a | pass |

2483 | n/a | """ |

2484 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2485 | n/a | |

2486 | n/a | def test_assign_to_next(self): |

2487 | n/a | s = """ |

2488 | n/a | def foo(): |

2489 | n/a | A.next = foo |

2490 | n/a | |

2491 | n/a | class A: |

2492 | n/a | def next(self, a, b): |

2493 | n/a | pass |

2494 | n/a | """ |

2495 | n/a | self.unchanged(s) |

2496 | n/a | |

2497 | n/a | def test_assign_to_next_in_tuple(self): |

2498 | n/a | s = """ |

2499 | n/a | def foo(): |

2500 | n/a | (a, A.next) = foo |

2501 | n/a | |

2502 | n/a | class A: |

2503 | n/a | def next(self, a, b): |

2504 | n/a | pass |

2505 | n/a | """ |

2506 | n/a | self.unchanged(s) |

2507 | n/a | |

2508 | n/a | def test_assign_to_next_in_list(self): |

2509 | n/a | s = """ |

2510 | n/a | def foo(): |

2511 | n/a | [a, A.next] = foo |

2512 | n/a | |

2513 | n/a | class A: |

2514 | n/a | def next(self, a, b): |

2515 | n/a | pass |

2516 | n/a | """ |

2517 | n/a | self.unchanged(s) |

2518 | n/a | |

2519 | n/a | def test_shadowing_import_1(self): |

2520 | n/a | s = """ |

2521 | n/a | import foo.bar as next |

2522 | n/a | |

2523 | n/a | class A: |

2524 | n/a | def next(self, a, b): |

2525 | n/a | pass |

2526 | n/a | """ |

2527 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2528 | n/a | |

2529 | n/a | def test_shadowing_import_2(self): |

2530 | n/a | s = """ |

2531 | n/a | import bar, bar.foo as next |

2532 | n/a | |

2533 | n/a | class A: |

2534 | n/a | def next(self, a, b): |

2535 | n/a | pass |

2536 | n/a | """ |

2537 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2538 | n/a | |

2539 | n/a | def test_shadowing_import_3(self): |

2540 | n/a | s = """ |

2541 | n/a | import bar, bar.foo as next, baz |

2542 | n/a | |

2543 | n/a | class A: |

2544 | n/a | def next(self, a, b): |

2545 | n/a | pass |

2546 | n/a | """ |

2547 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2548 | n/a | |

2549 | n/a | def test_shadowing_import_from_1(self): |

2550 | n/a | s = """ |

2551 | n/a | from x import next |

2552 | n/a | |

2553 | n/a | class A: |

2554 | n/a | def next(self, a, b): |

2555 | n/a | pass |

2556 | n/a | """ |

2557 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2558 | n/a | |

2559 | n/a | def test_shadowing_import_from_2(self): |

2560 | n/a | s = """ |

2561 | n/a | from x.a import next |

2562 | n/a | |

2563 | n/a | class A: |

2564 | n/a | def next(self, a, b): |

2565 | n/a | pass |

2566 | n/a | """ |

2567 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2568 | n/a | |

2569 | n/a | def test_shadowing_import_from_3(self): |

2570 | n/a | s = """ |

2571 | n/a | from x import a, next, b |

2572 | n/a | |

2573 | n/a | class A: |

2574 | n/a | def next(self, a, b): |

2575 | n/a | pass |

2576 | n/a | """ |

2577 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2578 | n/a | |

2579 | n/a | def test_shadowing_import_from_4(self): |

2580 | n/a | s = """ |

2581 | n/a | from x.a import a, next, b |

2582 | n/a | |

2583 | n/a | class A: |

2584 | n/a | def next(self, a, b): |

2585 | n/a | pass |

2586 | n/a | """ |

2587 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2588 | n/a | |

2589 | n/a | def test_shadowing_funcdef_1(self): |

2590 | n/a | s = """ |

2591 | n/a | def next(a): |

2592 | n/a | pass |

2593 | n/a | |

2594 | n/a | class A: |

2595 | n/a | def next(self, a, b): |

2596 | n/a | pass |

2597 | n/a | """ |

2598 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2599 | n/a | |

2600 | n/a | def test_shadowing_funcdef_2(self): |

2601 | n/a | b = """ |

2602 | n/a | def next(a): |

2603 | n/a | pass |

2604 | n/a | |

2605 | n/a | class A: |

2606 | n/a | def next(self): |

2607 | n/a | pass |

2608 | n/a | |

2609 | n/a | it.next() |

2610 | n/a | """ |

2611 | n/a | a = """ |

2612 | n/a | def next(a): |

2613 | n/a | pass |

2614 | n/a | |

2615 | n/a | class A: |

2616 | n/a | def __next__(self): |

2617 | n/a | pass |

2618 | n/a | |

2619 | n/a | it.__next__() |

2620 | n/a | """ |

2621 | n/a | self.warns(b, a, "Calls to builtin next() possibly shadowed") |

2622 | n/a | |

2623 | n/a | def test_shadowing_global_1(self): |

2624 | n/a | s = """ |

2625 | n/a | def f(): |

2626 | n/a | global next |

2627 | n/a | next = 5 |

2628 | n/a | """ |

2629 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2630 | n/a | |

2631 | n/a | def test_shadowing_global_2(self): |

2632 | n/a | s = """ |

2633 | n/a | def f(): |

2634 | n/a | global a, next, b |

2635 | n/a | next = 5 |

2636 | n/a | """ |

2637 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2638 | n/a | |

2639 | n/a | def test_shadowing_for_simple(self): |

2640 | n/a | s = """ |

2641 | n/a | for next in it(): |

2642 | n/a | pass |

2643 | n/a | |

2644 | n/a | b = 5 |

2645 | n/a | c = 6 |

2646 | n/a | """ |

2647 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2648 | n/a | |

2649 | n/a | def test_shadowing_for_tuple_1(self): |

2650 | n/a | s = """ |

2651 | n/a | for next, b in it(): |

2652 | n/a | pass |

2653 | n/a | |

2654 | n/a | b = 5 |

2655 | n/a | c = 6 |

2656 | n/a | """ |

2657 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2658 | n/a | |

2659 | n/a | def test_shadowing_for_tuple_2(self): |

2660 | n/a | s = """ |

2661 | n/a | for a, (next, c), b in it(): |

2662 | n/a | pass |

2663 | n/a | |

2664 | n/a | b = 5 |

2665 | n/a | c = 6 |

2666 | n/a | """ |

2667 | n/a | self.warns_unchanged(s, "Calls to builtin next() possibly shadowed") |

2668 | n/a | |

2669 | n/a | def test_noncall_access_1(self): |

2670 | n/a | b = """gnext = g.next""" |

2671 | n/a | a = """gnext = g.__next__""" |

2672 | n/a | self.check(b, a) |

2673 | n/a | |

2674 | n/a | def test_noncall_access_2(self): |

2675 | n/a | b = """f(g.next + 5)""" |

2676 | n/a | a = """f(g.__next__ + 5)""" |

2677 | n/a | self.check(b, a) |

2678 | n/a | |

2679 | n/a | def test_noncall_access_3(self): |

2680 | n/a | b = """f(g().next + 5)""" |

2681 | n/a | a = """f(g().__next__ + 5)""" |

2682 | n/a | self.check(b, a) |

2683 | n/a | |

2684 | n/a | class Test_nonzero(FixerTestCase): |

2685 | n/a | fixer = "nonzero" |

2686 | n/a | |

2687 | n/a | def test_1(self): |

2688 | n/a | b = """ |

2689 | n/a | class A: |

2690 | n/a | def __nonzero__(self): |

2691 | n/a | pass |

2692 | n/a | """ |

2693 | n/a | a = """ |

2694 | n/a | class A: |

2695 | n/a | def __bool__(self): |

2696 | n/a | pass |

2697 | n/a | """ |

2698 | n/a | self.check(b, a) |

2699 | n/a | |

2700 | n/a | def test_2(self): |

2701 | n/a | b = """ |

2702 | n/a | class A(object): |

2703 | n/a | def __nonzero__(self): |

2704 | n/a | pass |

2705 | n/a | """ |

2706 | n/a | a = """ |

2707 | n/a | class A(object): |

2708 | n/a | def __bool__(self): |

2709 | n/a | pass |

2710 | n/a | """ |

2711 | n/a | self.check(b, a) |

2712 | n/a | |

2713 | n/a | def test_unchanged_1(self): |

2714 | n/a | s = """ |

2715 | n/a | class A(object): |

2716 | n/a | def __bool__(self): |

2717 | n/a | pass |

2718 | n/a | """ |

2719 | n/a | self.unchanged(s) |

2720 | n/a | |

2721 | n/a | def test_unchanged_2(self): |

2722 | n/a | s = """ |

2723 | n/a | class A(object): |

2724 | n/a | def __nonzero__(self, a): |

2725 | n/a | pass |

2726 | n/a | """ |

2727 | n/a | self.unchanged(s) |

2728 | n/a | |

2729 | n/a | def test_unchanged_func(self): |

2730 | n/a | s = """ |

2731 | n/a | def __nonzero__(self): |

2732 | n/a | pass |

2733 | n/a | """ |

2734 | n/a | self.unchanged(s) |

2735 | n/a | |

2736 | n/a | class Test_numliterals(FixerTestCase): |

2737 | n/a | fixer = "numliterals" |

2738 | n/a | |

2739 | n/a | def test_octal_1(self): |

2740 | n/a | b = """0755""" |

2741 | n/a | a = """0o755""" |

2742 | n/a | self.check(b, a) |

2743 | n/a | |

2744 | n/a | def test_long_int_1(self): |

2745 | n/a | b = """a = 12L""" |

2746 | n/a | a = """a = 12""" |

2747 | n/a | self.check(b, a) |

2748 | n/a | |

2749 | n/a | def test_long_int_2(self): |

2750 | n/a | b = """a = 12l""" |

2751 | n/a | a = """a = 12""" |

2752 | n/a | self.check(b, a) |

2753 | n/a | |

2754 | n/a | def test_long_hex(self): |

2755 | n/a | b = """b = 0x12l""" |

2756 | n/a | a = """b = 0x12""" |

2757 | n/a | self.check(b, a) |

2758 | n/a | |

2759 | n/a | def test_comments_and_spacing(self): |

2760 | n/a | b = """b = 0x12L""" |

2761 | n/a | a = """b = 0x12""" |

2762 | n/a | self.check(b, a) |

2763 | n/a | |

2764 | n/a | b = """b = 0755 # spam""" |

2765 | n/a | a = """b = 0o755 # spam""" |

2766 | n/a | self.check(b, a) |

2767 | n/a | |

2768 | n/a | def test_unchanged_int(self): |

2769 | n/a | s = """5""" |

2770 | n/a | self.unchanged(s) |

2771 | n/a | |

2772 | n/a | def test_unchanged_float(self): |

2773 | n/a | s = """5.0""" |

2774 | n/a | self.unchanged(s) |

2775 | n/a | |

2776 | n/a | def test_unchanged_octal(self): |

2777 | n/a | s = """0o755""" |

2778 | n/a | self.unchanged(s) |

2779 | n/a | |

2780 | n/a | def test_unchanged_hex(self): |

2781 | n/a | s = """0xABC""" |

2782 | n/a | self.unchanged(s) |

2783 | n/a | |

2784 | n/a | def test_unchanged_exp(self): |

2785 | n/a | s = """5.0e10""" |

2786 | n/a | self.unchanged(s) |

2787 | n/a | |

2788 | n/a | def test_unchanged_complex_int(self): |

2789 | n/a | s = """5 + 4j""" |

2790 | n/a | self.unchanged(s) |

2791 | n/a | |

2792 | n/a | def test_unchanged_complex_float(self): |

2793 | n/a | s = """5.4 + 4.9j""" |

2794 | n/a | self.unchanged(s) |

2795 | n/a | |

2796 | n/a | def test_unchanged_complex_bare(self): |

2797 | n/a | s = """4j""" |

2798 | n/a | self.unchanged(s) |

2799 | n/a | s = """4.4j""" |

2800 | n/a | self.unchanged(s) |

2801 | n/a | |

2802 | n/a | class Test_renames(FixerTestCase): |

2803 | n/a | fixer = "renames" |

2804 | n/a | |

2805 | n/a | modules = {"sys": ("maxint", "maxsize"), |

2806 | n/a | } |

2807 | n/a | |

2808 | n/a | def test_import_from(self): |

2809 | n/a | for mod, (old, new) in list(self.modules.items()): |

2810 | n/a | b = "from %s import %s" % (mod, old) |

2811 | n/a | a = "from %s import %s" % (mod, new) |

2812 | n/a | self.check(b, a) |

2813 | n/a | |

2814 | n/a | s = "from foo import %s" % old |

2815 | n/a | self.unchanged(s) |

2816 | n/a | |

2817 | n/a | def test_import_from_as(self): |

2818 | n/a | for mod, (old, new) in list(self.modules.items()): |

2819 | n/a | b = "from %s import %s as foo_bar" % (mod, old) |

2820 | n/a | a = "from %s import %s as foo_bar" % (mod, new) |

2821 | n/a | self.check(b, a) |

2822 | n/a | |

2823 | n/a | def test_import_module_usage(self): |

2824 | n/a | for mod, (old, new) in list(self.modules.items()): |

2825 | n/a | b = """ |

2826 | n/a | import %s |

2827 | n/a | foo(%s, %s.%s) |

2828 | n/a | """ % (mod, mod, mod, old) |

2829 | n/a | a = """ |

2830 | n/a | import %s |

2831 | n/a | foo(%s, %s.%s) |

2832 | n/a | """ % (mod, mod, mod, new) |

2833 | n/a | self.check(b, a) |

2834 | n/a | |

2835 | n/a | def XXX_test_from_import_usage(self): |

2836 | n/a | # not implemented yet |

2837 | n/a | for mod, (old, new) in list(self.modules.items()): |

2838 | n/a | b = """ |

2839 | n/a | from %s import %s |

2840 | n/a | foo(%s, %s) |

2841 | n/a | """ % (mod, old, mod, old) |

2842 | n/a | a = """ |

2843 | n/a | from %s import %s |

2844 | n/a | foo(%s, %s) |

2845 | n/a | """ % (mod, new, mod, new) |

2846 | n/a | self.check(b, a) |

2847 | n/a | |

2848 | n/a | class Test_unicode(FixerTestCase): |

2849 | n/a | fixer = "unicode" |

2850 | n/a | |

2851 | n/a | def test_whitespace(self): |

2852 | n/a | b = """unicode( x)""" |

2853 | n/a | a = """str( x)""" |

2854 | n/a | self.check(b, a) |

2855 | n/a | |

2856 | n/a | b = """ unicode(x )""" |

2857 | n/a | a = """ str(x )""" |

2858 | n/a | self.check(b, a) |

2859 | n/a | |

2860 | n/a | b = """ u'h'""" |

2861 | n/a | a = """ 'h'""" |

2862 | n/a | self.check(b, a) |

2863 | n/a | |

2864 | n/a | def test_unicode_call(self): |

2865 | n/a | b = """unicode(x, y, z)""" |

2866 | n/a | a = """str(x, y, z)""" |

2867 | n/a | self.check(b, a) |

2868 | n/a | |

2869 | n/a | def test_unichr(self): |

2870 | n/a | b = """unichr(u'h')""" |

2871 | n/a | a = """chr('h')""" |

2872 | n/a | self.check(b, a) |

2873 | n/a | |

2874 | n/a | def test_unicode_literal_1(self): |

2875 | n/a | b = '''u"x"''' |

2876 | n/a | a = '''"x"''' |

2877 | n/a | self.check(b, a) |

2878 | n/a | |

2879 | n/a | def test_unicode_literal_2(self): |

2880 | n/a | b = """ur'x'""" |

2881 | n/a | a = """r'x'""" |

2882 | n/a | self.check(b, a) |

2883 | n/a | |

2884 | n/a | def test_unicode_literal_3(self): |

2885 | n/a | b = """UR'''x''' """ |

2886 | n/a | a = """R'''x''' """ |

2887 | n/a | self.check(b, a) |

2888 | n/a | |

2889 | n/a | def test_native_literal_escape_u(self): |

2890 | n/a | b = r"""'\\\u20ac\U0001d121\\u20ac'""" |

2891 | n/a | a = r"""'\\\\u20ac\\U0001d121\\u20ac'""" |

2892 | n/a | self.check(b, a) |

2893 | n/a | |

2894 | n/a | b = r"""r'\\\u20ac\U0001d121\\u20ac'""" |

2895 | n/a | a = r"""r'\\\u20ac\U0001d121\\u20ac'""" |

2896 | n/a | self.check(b, a) |

2897 | n/a | |

2898 | n/a | def test_bytes_literal_escape_u(self): |

2899 | n/a | b = r"""b'\\\u20ac\U0001d121\\u20ac'""" |

2900 | n/a | a = r"""b'\\\u20ac\U0001d121\\u20ac'""" |

2901 | n/a | self.check(b, a) |

2902 | n/a | |

2903 | n/a | b = r"""br'\\\u20ac\U0001d121\\u20ac'""" |

2904 | n/a | a = r"""br'\\\u20ac\U0001d121\\u20ac'""" |

2905 | n/a | self.check(b, a) |

2906 | n/a | |

2907 | n/a | def test_unicode_literal_escape_u(self): |

2908 | n/a | b = r"""u'\\\u20ac\U0001d121\\u20ac'""" |

2909 | n/a | a = r"""'\\\u20ac\U0001d121\\u20ac'""" |

2910 | n/a | self.check(b, a) |

2911 | n/a | |

2912 | n/a | b = r"""ur'\\\u20ac\U0001d121\\u20ac'""" |

2913 | n/a | a = r"""r'\\\u20ac\U0001d121\\u20ac'""" |

2914 | n/a | self.check(b, a) |

2915 | n/a | |

2916 | n/a | def test_native_unicode_literal_escape_u(self): |

2917 | n/a | f = 'from __future__ import unicode_literals\n' |

2918 | n/a | b = f + r"""'\\\u20ac\U0001d121\\u20ac'""" |

2919 | n/a | a = f + r"""'\\\u20ac\U0001d121\\u20ac'""" |

2920 | n/a | self.check(b, a) |

2921 | n/a | |

2922 | n/a | b = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" |

2923 | n/a | a = f + r"""r'\\\u20ac\U0001d121\\u20ac'""" |

2924 | n/a | self.check(b, a) |

2925 | n/a | |

2926 | n/a | |

2927 | n/a | class Test_filter(FixerTestCase): |

2928 | n/a | fixer = "filter" |

2929 | n/a | |

2930 | n/a | def test_prefix_preservation(self): |

2931 | n/a | b = """x = filter( foo, 'abc' )""" |

2932 | n/a | a = """x = list(filter( foo, 'abc' ))""" |

2933 | n/a | self.check(b, a) |

2934 | n/a | |

2935 | n/a | b = """x = filter( None , 'abc' )""" |

2936 | n/a | a = """x = [_f for _f in 'abc' if _f]""" |

2937 | n/a | self.check(b, a) |

2938 | n/a | |

2939 | n/a | def test_filter_basic(self): |

2940 | n/a | b = """x = filter(None, 'abc')""" |

2941 | n/a | a = """x = [_f for _f in 'abc' if _f]""" |

2942 | n/a | self.check(b, a) |

2943 | n/a | |

2944 | n/a | b = """x = len(filter(f, 'abc'))""" |

2945 | n/a | a = """x = len(list(filter(f, 'abc')))""" |

2946 | n/a | self.check(b, a) |

2947 | n/a | |

2948 | n/a | b = """x = filter(lambda x: x%2 == 0, range(10))""" |

2949 | n/a | a = """x = [x for x in range(10) if x%2 == 0]""" |

2950 | n/a | self.check(b, a) |

2951 | n/a | |

2952 | n/a | # Note the parens around x |

2953 | n/a | b = """x = filter(lambda (x): x%2 == 0, range(10))""" |

2954 | n/a | a = """x = [x for x in range(10) if x%2 == 0]""" |

2955 | n/a | self.check(b, a) |

2956 | n/a | |

2957 | n/a | # XXX This (rare) case is not supported |

2958 | n/a | ## b = """x = filter(f, 'abc')[0]""" |

2959 | n/a | ## a = """x = list(filter(f, 'abc'))[0]""" |

2960 | n/a | ## self.check(b, a) |

2961 | n/a | |

2962 | n/a | def test_filter_nochange(self): |

2963 | n/a | a = """b.join(filter(f, 'abc'))""" |

2964 | n/a | self.unchanged(a) |

2965 | n/a | a = """(a + foo(5)).join(filter(f, 'abc'))""" |

2966 | n/a | self.unchanged(a) |

2967 | n/a | a = """iter(filter(f, 'abc'))""" |

2968 | n/a | self.unchanged(a) |

2969 | n/a | a = """list(filter(f, 'abc'))""" |

2970 | n/a | self.unchanged(a) |

2971 | n/a | a = """list(filter(f, 'abc'))[0]""" |

2972 | n/a | self.unchanged(a) |

2973 | n/a | a = """set(filter(f, 'abc'))""" |

2974 | n/a | self.unchanged(a) |

2975 | n/a | a = """set(filter(f, 'abc')).pop()""" |

2976 | n/a | self.unchanged(a) |

2977 | n/a | a = """tuple(filter(f, 'abc'))""" |

2978 | n/a | self.unchanged(a) |

2979 | n/a | a = """any(filter(f, 'abc'))""" |

2980 | n/a | self.unchanged(a) |

2981 | n/a | a = """all(filter(f, 'abc'))""" |

2982 | n/a | self.unchanged(a) |

2983 | n/a | a = """sum(filter(f, 'abc'))""" |

2984 | n/a | self.unchanged(a) |

2985 | n/a | a = """sorted(filter(f, 'abc'))""" |

2986 | n/a | self.unchanged(a) |

2987 | n/a | a = """sorted(filter(f, 'abc'), key=blah)""" |

2988 | n/a | self.unchanged(a) |

2989 | n/a | a = """sorted(filter(f, 'abc'), key=blah)[0]""" |

2990 | n/a | self.unchanged(a) |

2991 | n/a | a = """enumerate(filter(f, 'abc'))""" |

2992 | n/a | self.unchanged(a) |

2993 | n/a | a = """enumerate(filter(f, 'abc'), start=1)""" |

2994 | n/a | self.unchanged(a) |

2995 | n/a | a = """for i in filter(f, 'abc'): pass""" |

2996 | n/a | self.unchanged(a) |

2997 | n/a | a = """[x for x in filter(f, 'abc')]""" |

2998 | n/a | self.unchanged(a) |

2999 | n/a | a = """(x for x in filter(f, 'abc'))""" |

3000 | n/a | self.unchanged(a) |

3001 | n/a | |

3002 | n/a | def test_future_builtins(self): |

3003 | n/a | a = "from future_builtins import spam, filter; filter(f, 'ham')" |

3004 | n/a | self.unchanged(a) |

3005 | n/a | |

3006 | n/a | b = """from future_builtins import spam; x = filter(f, 'abc')""" |

3007 | n/a | a = """from future_builtins import spam; x = list(filter(f, 'abc'))""" |

3008 | n/a | self.check(b, a) |

3009 | n/a | |

3010 | n/a | a = "from future_builtins import *; filter(f, 'ham')" |

3011 | n/a | self.unchanged(a) |

3012 | n/a | |

3013 | n/a | class Test_map(FixerTestCase): |

3014 | n/a | fixer = "map" |

3015 | n/a | |

3016 | n/a | def check(self, b, a): |

3017 | n/a | self.unchanged("from future_builtins import map; " + b, a) |

3018 | n/a | super(Test_map, self).check(b, a) |

3019 | n/a | |

3020 | n/a | def test_prefix_preservation(self): |

3021 | n/a | b = """x = map( f, 'abc' )""" |

3022 | n/a | a = """x = list(map( f, 'abc' ))""" |

3023 | n/a | self.check(b, a) |

3024 | n/a | |

3025 | n/a | def test_trailing_comment(self): |

3026 | n/a | b = """x = map(f, 'abc') # foo""" |

3027 | n/a | a = """x = list(map(f, 'abc')) # foo""" |

3028 | n/a | self.check(b, a) |

3029 | n/a | |

3030 | n/a | def test_None_with_multiple_arguments(self): |

3031 | n/a | s = """x = map(None, a, b, c)""" |

3032 | n/a | self.warns_unchanged(s, "cannot convert map(None, ...) with " |

3033 | n/a | "multiple arguments") |

3034 | n/a | |

3035 | n/a | def test_map_basic(self): |

3036 | n/a | b = """x = map(f, 'abc')""" |

3037 | n/a | a = """x = list(map(f, 'abc'))""" |

3038 | n/a | self.check(b, a) |

3039 | n/a | |

3040 | n/a | b = """x = len(map(f, 'abc', 'def'))""" |

3041 | n/a | a = """x = len(list(map(f, 'abc', 'def')))""" |

3042 | n/a | self.check(b, a) |

3043 | n/a | |

3044 | n/a | b = """x = map(None, 'abc')""" |

3045 | n/a | a = """x = list('abc')""" |

3046 | n/a | self.check(b, a) |

3047 | n/a | |

3048 | n/a | b = """x = map(lambda x: x+1, range(4))""" |

3049 | n/a | a = """x = [x+1 for x in range(4)]""" |

3050 | n/a | self.check(b, a) |

3051 | n/a | |

3052 | n/a | # Note the parens around x |

3053 | n/a | b = """x = map(lambda (x): x+1, range(4))""" |

3054 | n/a | a = """x = [x+1 for x in range(4)]""" |

3055 | n/a | self.check(b, a) |

3056 | n/a | |

3057 | n/a | b = """ |

3058 | n/a | foo() |

3059 | n/a | # foo |

3060 | n/a | map(f, x) |

3061 | n/a | """ |

3062 | n/a | a = """ |

3063 | n/a | foo() |

3064 | n/a | # foo |

3065 | n/a | list(map(f, x)) |

3066 | n/a | """ |

3067 | n/a | self.warns(b, a, "You should use a for loop here") |

3068 | n/a | |

3069 | n/a | # XXX This (rare) case is not supported |

3070 | n/a | ## b = """x = map(f, 'abc')[0]""" |

3071 | n/a | ## a = """x = list(map(f, 'abc'))[0]""" |

3072 | n/a | ## self.check(b, a) |

3073 | n/a | |

3074 | n/a | def test_map_nochange(self): |

3075 | n/a | a = """b.join(map(f, 'abc'))""" |

3076 | n/a | self.unchanged(a) |

3077 | n/a | a = """(a + foo(5)).join(map(f, 'abc'))""" |

3078 | n/a | self.unchanged(a) |

3079 | n/a | a = """iter(map(f, 'abc'))""" |

3080 | n/a | self.unchanged(a) |

3081 | n/a | a = """list(map(f, 'abc'))""" |

3082 | n/a | self.unchanged(a) |

3083 | n/a | a = """list(map(f, 'abc'))[0]""" |

3084 | n/a | self.unchanged(a) |

3085 | n/a | a = """set(map(f, 'abc'))""" |

3086 | n/a | self.unchanged(a) |

3087 | n/a | a = """set(map(f, 'abc')).pop()""" |

3088 | n/a | self.unchanged(a) |

3089 | n/a | a = """tuple(map(f, 'abc'))""" |

3090 | n/a | self.unchanged(a) |

3091 | n/a | a = """any(map(f, 'abc'))""" |

3092 | n/a | self.unchanged(a) |

3093 | n/a | a = """all(map(f, 'abc'))""" |

3094 | n/a | self.unchanged(a) |

3095 | n/a | a = """sum(map(f, 'abc'))""" |

3096 | n/a | self.unchanged(a) |

3097 | n/a | a = """sorted(map(f, 'abc'))""" |

3098 | n/a | self.unchanged(a) |

3099 | n/a | a = """sorted(map(f, 'abc'), key=blah)""" |

3100 | n/a | self.unchanged(a) |

3101 | n/a | a = """sorted(map(f, 'abc'), key=blah)[0]""" |

3102 | n/a | self.unchanged(a) |

3103 | n/a | a = """enumerate(map(f, 'abc'))""" |

3104 | n/a | self.unchanged(a) |

3105 | n/a | a = """enumerate(map(f, 'abc'), start=1)""" |

3106 | n/a | self.unchanged(a) |

3107 | n/a | a = """for i in map(f, 'abc'): pass""" |

3108 | n/a | self.unchanged(a) |

3109 | n/a | a = """[x for x in map(f, 'abc')]""" |

3110 | n/a | self.unchanged(a) |

3111 | n/a | a = """(x for x in map(f, 'abc'))""" |

3112 | n/a | self.unchanged(a) |

3113 | n/a | |

3114 | n/a | def test_future_builtins(self): |

3115 | n/a | a = "from future_builtins import spam, map, eggs; map(f, 'ham')" |

3116 | n/a | self.unchanged(a) |

3117 | n/a | |

3118 | n/a | b = """from future_builtins import spam, eggs; x = map(f, 'abc')""" |

3119 | n/a | a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))""" |

3120 | n/a | self.check(b, a) |

3121 | n/a | |

3122 | n/a | a = "from future_builtins import *; map(f, 'ham')" |

3123 | n/a | self.unchanged(a) |

3124 | n/a | |

3125 | n/a | class Test_zip(FixerTestCase): |

3126 | n/a | fixer = "zip" |

3127 | n/a | |

3128 | n/a | def check(self, b, a): |

3129 | n/a | self.unchanged("from future_builtins import zip; " + b, a) |

3130 | n/a | super(Test_zip, self).check(b, a) |

3131 | n/a | |

3132 | n/a | def test_zip_basic(self): |

3133 | n/a | b = """x = zip(a, b, c)""" |

3134 | n/a | a = """x = list(zip(a, b, c))""" |

3135 | n/a | self.check(b, a) |

3136 | n/a | |

3137 | n/a | b = """x = len(zip(a, b))""" |

3138 | n/a | a = """x = len(list(zip(a, b)))""" |

3139 | n/a | self.check(b, a) |

3140 | n/a | |

3141 | n/a | def test_zip_nochange(self): |

3142 | n/a | a = """b.join(zip(a, b))""" |

3143 | n/a | self.unchanged(a) |

3144 | n/a | a = """(a + foo(5)).join(zip(a, b))""" |

3145 | n/a | self.unchanged(a) |

3146 | n/a | a = """iter(zip(a, b))""" |

3147 | n/a | self.unchanged(a) |

3148 | n/a | a = """list(zip(a, b))""" |

3149 | n/a | self.unchanged(a) |

3150 | n/a | a = """list(zip(a, b))[0]""" |

3151 | n/a | self.unchanged(a) |

3152 | n/a | a = """set(zip(a, b))""" |

3153 | n/a | self.unchanged(a) |

3154 | n/a | a = """set(zip(a, b)).pop()""" |

3155 | n/a | self.unchanged(a) |

3156 | n/a | a = """tuple(zip(a, b))""" |

3157 | n/a | self.unchanged(a) |

3158 | n/a | a = """any(zip(a, b))""" |

3159 | n/a | self.unchanged(a) |

3160 | n/a | a = """all(zip(a, b))""" |

3161 | n/a | self.unchanged(a) |

3162 | n/a | a = """sum(zip(a, b))""" |

3163 | n/a | self.unchanged(a) |

3164 | n/a | a = """sorted(zip(a, b))""" |

3165 | n/a | self.unchanged(a) |

3166 | n/a | a = """sorted(zip(a, b), key=blah)""" |

3167 | n/a | self.unchanged(a) |

3168 | n/a | a = """sorted(zip(a, b), key=blah)[0]""" |

3169 | n/a | self.unchanged(a) |

3170 | n/a | a = """enumerate(zip(a, b))""" |

3171 | n/a | self.unchanged(a) |

3172 | n/a | a = """enumerate(zip(a, b), start=1)""" |

3173 | n/a | self.unchanged(a) |

3174 | n/a | a = """for i in zip(a, b): pass""" |

3175 | n/a | self.unchanged(a) |

3176 | n/a | a = """[x for x in zip(a, b)]""" |

3177 | n/a | self.unchanged(a) |

3178 | n/a | a = """(x for x in zip(a, b))""" |

3179 | n/a | self.unchanged(a) |

3180 | n/a | |

3181 | n/a | def test_future_builtins(self): |

3182 | n/a | a = "from future_builtins import spam, zip, eggs; zip(a, b)" |

3183 | n/a | self.unchanged(a) |

3184 | n/a | |

3185 | n/a | b = """from future_builtins import spam, eggs; x = zip(a, b)""" |

3186 | n/a | a = """from future_builtins import spam, eggs; x = list(zip(a, b))""" |

3187 | n/a | self.check(b, a) |

3188 | n/a | |

3189 | n/a | a = "from future_builtins import *; zip(a, b)" |

3190 | n/a | self.unchanged(a) |

3191 | n/a | |

3192 | n/a | class Test_standarderror(FixerTestCase): |

3193 | n/a | fixer = "standarderror" |

3194 | n/a | |

3195 | n/a | def test(self): |

3196 | n/a | b = """x = StandardError()""" |

3197 | n/a | a = """x = Exception()""" |

3198 | n/a | self.check(b, a) |

3199 | n/a | |

3200 | n/a | b = """x = StandardError(a, b, c)""" |

3201 | n/a | a = """x = Exception(a, b, c)""" |

3202 | n/a | self.check(b, a) |

3203 | n/a | |

3204 | n/a | b = """f(2 + StandardError(a, b, c))""" |

3205 | n/a | a = """f(2 + Exception(a, b, c))""" |

3206 | n/a | self.check(b, a) |

3207 | n/a | |

3208 | n/a | class Test_types(FixerTestCase): |

3209 | n/a | fixer = "types" |

3210 | n/a | |

3211 | n/a | def test_basic_types_convert(self): |

3212 | n/a | b = """types.StringType""" |

3213 | n/a | a = """bytes""" |

3214 | n/a | self.check(b, a) |

3215 | n/a | |

3216 | n/a | b = """types.DictType""" |

3217 | n/a | a = """dict""" |

3218 | n/a | self.check(b, a) |

3219 | n/a | |

3220 | n/a | b = """types . IntType""" |

3221 | n/a | a = """int""" |

3222 | n/a | self.check(b, a) |

3223 | n/a | |

3224 | n/a | b = """types.ListType""" |

3225 | n/a | a = """list""" |

3226 | n/a | self.check(b, a) |

3227 | n/a | |

3228 | n/a | b = """types.LongType""" |

3229 | n/a | a = """int""" |

3230 | n/a | self.check(b, a) |

3231 | n/a | |

3232 | n/a | b = """types.NoneType""" |

3233 | n/a | a = """type(None)""" |

3234 | n/a | self.check(b, a) |

3235 | n/a | |

3236 | n/a | b = "types.StringTypes" |

3237 | n/a | a = "(str,)" |

3238 | n/a | self.check(b, a) |

3239 | n/a | |

3240 | n/a | class Test_idioms(FixerTestCase): |

3241 | n/a | fixer = "idioms" |

3242 | n/a | |

3243 | n/a | def test_while(self): |

3244 | n/a | b = """while 1: foo()""" |

3245 | n/a | a = """while True: foo()""" |

3246 | n/a | self.check(b, a) |

3247 | n/a | |

3248 | n/a | b = """while 1: foo()""" |

3249 | n/a | a = """while True: foo()""" |

3250 | n/a | self.check(b, a) |

3251 | n/a | |

3252 | n/a | b = """ |

3253 | n/a | while 1: |

3254 | n/a | foo() |

3255 | n/a | """ |

3256 | n/a | a = """ |

3257 | n/a | while True: |

3258 | n/a | foo() |

3259 | n/a | """ |

3260 | n/a | self.check(b, a) |

3261 | n/a | |

3262 | n/a | def test_while_unchanged(self): |

3263 | n/a | s = """while 11: foo()""" |

3264 | n/a | self.unchanged(s) |

3265 | n/a | |

3266 | n/a | s = """while 0: foo()""" |

3267 | n/a | self.unchanged(s) |

3268 | n/a | |

3269 | n/a | s = """while foo(): foo()""" |

3270 | n/a | self.unchanged(s) |

3271 | n/a | |

3272 | n/a | s = """while []: foo()""" |

3273 | n/a | self.unchanged(s) |

3274 | n/a | |

3275 | n/a | def test_eq_simple(self): |

3276 | n/a | b = """type(x) == T""" |

3277 | n/a | a = """isinstance(x, T)""" |

3278 | n/a | self.check(b, a) |

3279 | n/a | |

3280 | n/a | b = """if type(x) == T: pass""" |

3281 | n/a | a = """if isinstance(x, T): pass""" |

3282 | n/a | self.check(b, a) |

3283 | n/a | |

3284 | n/a | def test_eq_reverse(self): |

3285 | n/a | b = """T == type(x)""" |

3286 | n/a | a = """isinstance(x, T)""" |

3287 | n/a | self.check(b, a) |

3288 | n/a | |

3289 | n/a | b = """if T == type(x): pass""" |

3290 | n/a | a = """if isinstance(x, T): pass""" |

3291 | n/a | self.check(b, a) |

3292 | n/a | |

3293 | n/a | def test_eq_expression(self): |

3294 | n/a | b = """type(x+y) == d.get('T')""" |

3295 | n/a | a = """isinstance(x+y, d.get('T'))""" |

3296 | n/a | self.check(b, a) |

3297 | n/a | |

3298 | n/a | b = """type( x + y) == d.get('T')""" |

3299 | n/a | a = """isinstance(x + y, d.get('T'))""" |

3300 | n/a | self.check(b, a) |

3301 | n/a | |

3302 | n/a | def test_is_simple(self): |

3303 | n/a | b = """type(x) is T""" |

3304 | n/a | a = """isinstance(x, T)""" |

3305 | n/a | self.check(b, a) |

3306 | n/a | |

3307 | n/a | b = """if type(x) is T: pass""" |

3308 | n/a | a = """if isinstance(x, T): pass""" |

3309 | n/a | self.check(b, a) |

3310 | n/a | |

3311 | n/a | def test_is_reverse(self): |

3312 | n/a | b = """T is type(x)""" |

3313 | n/a | a = """isinstance(x, T)""" |

3314 | n/a | self.check(b, a) |

3315 | n/a | |

3316 | n/a | b = """if T is type(x): pass""" |

3317 | n/a | a = """if isinstance(x, T): pass""" |

3318 | n/a | self.check(b, a) |

3319 | n/a | |

3320 | n/a | def test_is_expression(self): |

3321 | n/a | b = """type(x+y) is d.get('T')""" |

3322 | n/a | a = """isinstance(x+y, d.get('T'))""" |

3323 | n/a | self.check(b, a) |

3324 | n/a | |

3325 | n/a | b = """type( x + y) is d.get('T')""" |

3326 | n/a | a = """isinstance(x + y, d.get('T'))""" |

3327 | n/a | self.check(b, a) |

3328 | n/a | |

3329 | n/a | def test_is_not_simple(self): |

3330 | n/a | b = """type(x) is not T""" |

3331 | n/a | a = """not isinstance(x, T)""" |

3332 | n/a | self.check(b, a) |

3333 | n/a | |

3334 | n/a | b = """if type(x) is not T: pass""" |

3335 | n/a | a = """if not isinstance(x, T): pass""" |

3336 | n/a | self.check(b, a) |

3337 | n/a | |

3338 | n/a | def test_is_not_reverse(self): |

3339 | n/a | b = """T is not type(x)""" |

3340 | n/a | a = """not isinstance(x, T)""" |

3341 | n/a | self.check(b, a) |

3342 | n/a | |

3343 | n/a | b = """if T is not type(x): pass""" |

3344 | n/a | a = """if not isinstance(x, T): pass""" |

3345 | n/a | self.check(b, a) |

3346 | n/a | |

3347 | n/a | def test_is_not_expression(self): |

3348 | n/a | b = """type(x+y) is not d.get('T')""" |

3349 | n/a | a = """not isinstance(x+y, d.get('T'))""" |

3350 | n/a | self.check(b, a) |

3351 | n/a | |

3352 | n/a | b = """type( x + y) is not d.get('T')""" |

3353 | n/a | a = """not isinstance(x + y, d.get('T'))""" |

3354 | n/a | self.check(b, a) |

3355 | n/a | |

3356 | n/a | def test_ne_simple(self): |

3357 | n/a | b = """type(x) != T""" |

3358 | n/a | a = """not isinstance(x, T)""" |

3359 | n/a | self.check(b, a) |

3360 | n/a | |

3361 | n/a | b = """if type(x) != T: pass""" |

3362 | n/a | a = """if not isinstance(x, T): pass""" |

3363 | n/a | self.check(b, a) |

3364 | n/a | |

3365 | n/a | def test_ne_reverse(self): |

3366 | n/a | b = """T != type(x)""" |

3367 | n/a | a = """not isinstance(x, T)""" |

3368 | n/a | self.check(b, a) |

3369 | n/a | |

3370 | n/a | b = """if T != type(x): pass""" |

3371 | n/a | a = """if not isinstance(x, T): pass""" |

3372 | n/a | self.check(b, a) |

3373 | n/a | |

3374 | n/a | def test_ne_expression(self): |

3375 | n/a | b = """type(x+y) != d.get('T')""" |

3376 | n/a | a = """not isinstance(x+y, d.get('T'))""" |

3377 | n/a | self.check(b, a) |

3378 | n/a | |

3379 | n/a | b = """type( x + y) != d.get('T')""" |

3380 | n/a | a = """not isinstance(x + y, d.get('T'))""" |

3381 | n/a | self.check(b, a) |

3382 | n/a | |

3383 | n/a | def test_type_unchanged(self): |

3384 | n/a | a = """type(x).__name__""" |

3385 | n/a | self.unchanged(a) |

3386 | n/a | |

3387 | n/a | def test_sort_list_call(self): |

3388 | n/a | b = """ |

3389 | n/a | v = list(t) |

3390 | n/a | v.sort() |

3391 | n/a | foo(v) |

3392 | n/a | """ |

3393 | n/a | a = """ |

3394 | n/a | v = sorted(t) |

3395 | n/a | foo(v) |

3396 | n/a | """ |

3397 | n/a | self.check(b, a) |

3398 | n/a | |

3399 | n/a | b = """ |

3400 | n/a | v = list(foo(b) + d) |

3401 | n/a | v.sort() |

3402 | n/a | foo(v) |

3403 | n/a | """ |

3404 | n/a | a = """ |

3405 | n/a | v = sorted(foo(b) + d) |

3406 | n/a | foo(v) |

3407 | n/a | """ |

3408 | n/a | self.check(b, a) |

3409 | n/a | |

3410 | n/a | b = """ |

3411 | n/a | while x: |

3412 | n/a | v = list(t) |

3413 | n/a | v.sort() |

3414 | n/a | foo(v) |

3415 | n/a | """ |

3416 | n/a | a = """ |

3417 | n/a | while x: |

3418 | n/a | v = sorted(t) |

3419 | n/a | foo(v) |

3420 | n/a | """ |

3421 | n/a | self.check(b, a) |

3422 | n/a | |

3423 | n/a | b = """ |

3424 | n/a | v = list(t) |

3425 | n/a | # foo |

3426 | n/a | v.sort() |

3427 | n/a | foo(v) |

3428 | n/a | """ |

3429 | n/a | a = """ |

3430 | n/a | v = sorted(t) |

3431 | n/a | # foo |

3432 | n/a | foo(v) |

3433 | n/a | """ |

3434 | n/a | self.check(b, a) |

3435 | n/a | |

3436 | n/a | b = r""" |

3437 | n/a | v = list( t) |

3438 | n/a | v.sort() |

3439 | n/a | foo(v) |

3440 | n/a | """ |

3441 | n/a | a = r""" |

3442 | n/a | v = sorted( t) |

3443 | n/a | foo(v) |

3444 | n/a | """ |

3445 | n/a | self.check(b, a) |

3446 | n/a | |

3447 | n/a | b = r""" |

3448 | n/a | try: |

3449 | n/a | m = list(s) |

3450 | n/a | m.sort() |

3451 | n/a | except: pass |

3452 | n/a | """ |

3453 | n/a | |

3454 | n/a | a = r""" |

3455 | n/a | try: |

3456 | n/a | m = sorted(s) |

3457 | n/a | except: pass |

3458 | n/a | """ |

3459 | n/a | self.check(b, a) |

3460 | n/a | |

3461 | n/a | b = r""" |

3462 | n/a | try: |

3463 | n/a | m = list(s) |

3464 | n/a | # foo |

3465 | n/a | m.sort() |

3466 | n/a | except: pass |

3467 | n/a | """ |

3468 | n/a | |

3469 | n/a | a = r""" |

3470 | n/a | try: |

3471 | n/a | m = sorted(s) |

3472 | n/a | # foo |

3473 | n/a | except: pass |

3474 | n/a | """ |

3475 | n/a | self.check(b, a) |

3476 | n/a | |

3477 | n/a | b = r""" |

3478 | n/a | m = list(s) |

3479 | n/a | # more comments |

3480 | n/a | m.sort()""" |

3481 | n/a | |

3482 | n/a | a = r""" |

3483 | n/a | m = sorted(s) |

3484 | n/a | # more comments""" |

3485 | n/a | self.check(b, a) |

3486 | n/a | |

3487 | n/a | def test_sort_simple_expr(self): |

3488 | n/a | b = """ |

3489 | n/a | v = t |

3490 | n/a | v.sort() |

3491 | n/a | foo(v) |

3492 | n/a | """ |

3493 | n/a | a = """ |

3494 | n/a | v = sorted(t) |

3495 | n/a | foo(v) |

3496 | n/a | """ |

3497 | n/a | self.check(b, a) |

3498 | n/a | |

3499 | n/a | b = """ |

3500 | n/a | v = foo(b) |

3501 | n/a | v.sort() |

3502 | n/a | foo(v) |

3503 | n/a | """ |

3504 | n/a | a = """ |

3505 | n/a | v = sorted(foo(b)) |

3506 | n/a | foo(v) |

3507 | n/a | """ |

3508 | n/a | self.check(b, a) |

3509 | n/a | |

3510 | n/a | b = """ |

3511 | n/a | v = b.keys() |

3512 | n/a | v.sort() |

3513 | n/a | foo(v) |

3514 | n/a | """ |

3515 | n/a | a = """ |

3516 | n/a | v = sorted(b.keys()) |

3517 | n/a | foo(v) |

3518 | n/a | """ |

3519 | n/a | self.check(b, a) |

3520 | n/a | |

3521 | n/a | b = """ |

3522 | n/a | v = foo(b) + d |

3523 | n/a | v.sort() |

3524 | n/a | foo(v) |

3525 | n/a | """ |

3526 | n/a | a = """ |

3527 | n/a | v = sorted(foo(b) + d) |

3528 | n/a | foo(v) |

3529 | n/a | """ |

3530 | n/a | self.check(b, a) |

3531 | n/a | |

3532 | n/a | b = """ |

3533 | n/a | while x: |

3534 | n/a | v = t |

3535 | n/a | v.sort() |

3536 | n/a | foo(v) |

3537 | n/a | """ |

3538 | n/a | a = """ |

3539 | n/a | while x: |

3540 | n/a | v = sorted(t) |

3541 | n/a | foo(v) |

3542 | n/a | """ |

3543 | n/a | self.check(b, a) |

3544 | n/a | |

3545 | n/a | b = """ |

3546 | n/a | v = t |

3547 | n/a | # foo |

3548 | n/a | v.sort() |

3549 | n/a | foo(v) |

3550 | n/a | """ |

3551 | n/a | a = """ |

3552 | n/a | v = sorted(t) |

3553 | n/a | # foo |

3554 | n/a | foo(v) |

3555 | n/a | """ |

3556 | n/a | self.check(b, a) |

3557 | n/a | |

3558 | n/a | b = r""" |

3559 | n/a | v = t |

3560 | n/a | v.sort() |

3561 | n/a | foo(v) |

3562 | n/a | """ |

3563 | n/a | a = r""" |

3564 | n/a | v = sorted(t) |

3565 | n/a | foo(v) |

3566 | n/a | """ |

3567 | n/a | self.check(b, a) |

3568 | n/a | |

3569 | n/a | def test_sort_unchanged(self): |

3570 | n/a | s = """ |

3571 | n/a | v = list(t) |

3572 | n/a | w.sort() |

3573 | n/a | foo(w) |

3574 | n/a | """ |

3575 | n/a | self.unchanged(s) |

3576 | n/a | |

3577 | n/a | s = """ |

3578 | n/a | v = list(t) |

3579 | n/a | v.sort(u) |

3580 | n/a | foo(v) |

3581 | n/a | """ |

3582 | n/a | self.unchanged(s) |

3583 | n/a | |

3584 | n/a | class Test_basestring(FixerTestCase): |

3585 | n/a | fixer = "basestring" |

3586 | n/a | |

3587 | n/a | def test_basestring(self): |

3588 | n/a | b = """isinstance(x, basestring)""" |

3589 | n/a | a = """isinstance(x, str)""" |

3590 | n/a | self.check(b, a) |

3591 | n/a | |

3592 | n/a | class Test_buffer(FixerTestCase): |

3593 | n/a | fixer = "buffer" |

3594 | n/a | |

3595 | n/a | def test_buffer(self): |

3596 | n/a | b = """x = buffer(y)""" |

3597 | n/a | a = """x = memoryview(y)""" |

3598 | n/a | self.check(b, a) |

3599 | n/a | |

3600 | n/a | def test_slicing(self): |

3601 | n/a | b = """buffer(y)[4:5]""" |

3602 | n/a | a = """memoryview(y)[4:5]""" |

3603 | n/a | self.check(b, a) |

3604 | n/a | |

3605 | n/a | class Test_future(FixerTestCase): |

3606 | n/a | fixer = "future" |

3607 | n/a | |

3608 | n/a | def test_future(self): |

3609 | n/a | b = """from __future__ import braces""" |

3610 | n/a | a = """""" |

3611 | n/a | self.check(b, a) |

3612 | n/a | |

3613 | n/a | b = """# comment\nfrom __future__ import braces""" |

3614 | n/a | a = """# comment\n""" |

3615 | n/a | self.check(b, a) |

3616 | n/a | |

3617 | n/a | b = """from __future__ import braces\n# comment""" |

3618 | n/a | a = """\n# comment""" |

3619 | n/a | self.check(b, a) |

3620 | n/a | |

3621 | n/a | def test_run_order(self): |

3622 | n/a | self.assert_runs_after('print') |

3623 | n/a | |

3624 | n/a | class Test_itertools(FixerTestCase): |

3625 | n/a | fixer = "itertools" |

3626 | n/a | |

3627 | n/a | def checkall(self, before, after): |

3628 | n/a | # Because we need to check with and without the itertools prefix |

3629 | n/a | # and on each of the three functions, these loops make it all |

3630 | n/a | # much easier |

3631 | n/a | for i in ('itertools.', ''): |

3632 | n/a | for f in ('map', 'filter', 'zip'): |

3633 | n/a | b = before %(i+'i'+f) |

3634 | n/a | a = after %(f) |

3635 | n/a | self.check(b, a) |

3636 | n/a | |

3637 | n/a | def test_0(self): |

3638 | n/a | # A simple example -- test_1 covers exactly the same thing, |

3639 | n/a | # but it's not quite as clear. |

3640 | n/a | b = "itertools.izip(a, b)" |

3641 | n/a | a = "zip(a, b)" |

3642 | n/a | self.check(b, a) |

3643 | n/a | |

3644 | n/a | def test_1(self): |

3645 | n/a | b = """%s(f, a)""" |

3646 | n/a | a = """%s(f, a)""" |

3647 | n/a | self.checkall(b, a) |

3648 | n/a | |

3649 | n/a | def test_qualified(self): |

3650 | n/a | b = """itertools.ifilterfalse(a, b)""" |

3651 | n/a | a = """itertools.filterfalse(a, b)""" |

3652 | n/a | self.check(b, a) |

3653 | n/a | |

3654 | n/a | b = """itertools.izip_longest(a, b)""" |

3655 | n/a | a = """itertools.zip_longest(a, b)""" |

3656 | n/a | self.check(b, a) |

3657 | n/a | |

3658 | n/a | def test_2(self): |

3659 | n/a | b = """ifilterfalse(a, b)""" |

3660 | n/a | a = """filterfalse(a, b)""" |

3661 | n/a | self.check(b, a) |

3662 | n/a | |

3663 | n/a | b = """izip_longest(a, b)""" |

3664 | n/a | a = """zip_longest(a, b)""" |

3665 | n/a | self.check(b, a) |

3666 | n/a | |

3667 | n/a | def test_space_1(self): |

3668 | n/a | b = """ %s(f, a)""" |

3669 | n/a | a = """ %s(f, a)""" |

3670 | n/a | self.checkall(b, a) |

3671 | n/a | |

3672 | n/a | def test_space_2(self): |

3673 | n/a | b = """ itertools.ifilterfalse(a, b)""" |

3674 | n/a | a = """ itertools.filterfalse(a, b)""" |

3675 | n/a | self.check(b, a) |

3676 | n/a | |

3677 | n/a | b = """ itertools.izip_longest(a, b)""" |

3678 | n/a | a = """ itertools.zip_longest(a, b)""" |

3679 | n/a | self.check(b, a) |

3680 | n/a | |

3681 | n/a | def test_run_order(self): |

3682 | n/a | self.assert_runs_after('map', 'zip', 'filter') |

3683 | n/a | |

3684 | n/a | |

3685 | n/a | class Test_itertools_imports(FixerTestCase): |

3686 | n/a | fixer = 'itertools_imports' |

3687 | n/a | |

3688 | n/a | def test_reduced(self): |

3689 | n/a | b = "from itertools import imap, izip, foo" |

3690 | n/a | a = "from itertools import foo" |

3691 | n/a | self.check(b, a) |

3692 | n/a | |

3693 | n/a | b = "from itertools import bar, imap, izip, foo" |

3694 | n/a | a = "from itertools import bar, foo" |

3695 | n/a | self.check(b, a) |

3696 | n/a | |

3697 | n/a | b = "from itertools import chain, imap, izip" |

3698 | n/a | a = "from itertools import chain" |

3699 | n/a | self.check(b, a) |

3700 | n/a | |

3701 | n/a | def test_comments(self): |

3702 | n/a | b = "#foo\nfrom itertools import imap, izip" |

3703 | n/a | a = "#foo\n" |

3704 | n/a | self.check(b, a) |

3705 | n/a | |

3706 | n/a | def test_none(self): |

3707 | n/a | b = "from itertools import imap, izip" |

3708 | n/a | a = "" |

3709 | n/a | self.check(b, a) |

3710 | n/a | |

3711 | n/a | b = "from itertools import izip" |

3712 | n/a | a = "" |

3713 | n/a | self.check(b, a) |

3714 | n/a | |

3715 | n/a | def test_import_as(self): |

3716 | n/a | b = "from itertools import izip, bar as bang, imap" |

3717 | n/a | a = "from itertools import bar as bang" |

3718 | n/a | self.check(b, a) |

3719 | n/a | |

3720 | n/a | b = "from itertools import izip as _zip, imap, bar" |

3721 | n/a | a = "from itertools import bar" |

3722 | n/a | self.check(b, a) |

3723 | n/a | |

3724 | n/a | b = "from itertools import imap as _map" |

3725 | n/a | a = "" |

3726 | n/a | self.check(b, a) |

3727 | n/a | |

3728 | n/a | b = "from itertools import imap as _map, izip as _zip" |

3729 | n/a | a = "" |

3730 | n/a | self.check(b, a) |

3731 | n/a | |

3732 | n/a | s = "from itertools import bar as bang" |

3733 | n/a | self.unchanged(s) |

3734 | n/a | |

3735 | n/a | def test_ifilter_and_zip_longest(self): |

3736 | n/a | for name in "filterfalse", "zip_longest": |

3737 | n/a | b = "from itertools import i%s" % (name,) |

3738 | n/a | a = "from itertools import %s" % (name,) |

3739 | n/a | self.check(b, a) |

3740 | n/a | |

3741 | n/a | b = "from itertools import imap, i%s, foo" % (name,) |

3742 | n/a | a = "from itertools import %s, foo" % (name,) |

3743 | n/a | self.check(b, a) |

3744 | n/a | |

3745 | n/a | b = "from itertools import bar, i%s, foo" % (name,) |

3746 | n/a | a = "from itertools import bar, %s, foo" % (name,) |

3747 | n/a | self.check(b, a) |

3748 | n/a | |

3749 | n/a | def test_import_star(self): |

3750 | n/a | s = "from itertools import *" |

3751 | n/a | self.unchanged(s) |

3752 | n/a | |

3753 | n/a | |

3754 | n/a | def test_unchanged(self): |

3755 | n/a | s = "from itertools import foo" |

3756 | n/a | self.unchanged(s) |

3757 | n/a | |

3758 | n/a | |

3759 | n/a | class Test_import(FixerTestCase): |

3760 | n/a | fixer = "import" |

3761 | n/a | |

3762 | n/a | def setUp(self): |

3763 | n/a | super(Test_import, self).setUp() |

3764 | n/a | # Need to replace fix_import's exists method |

3765 | n/a | # so we can check that it's doing the right thing |

3766 | n/a | self.files_checked = [] |

3767 | n/a | self.present_files = set() |

3768 | n/a | self.always_exists = True |

3769 | n/a | def fake_exists(name): |

3770 | n/a | self.files_checked.append(name) |

3771 | n/a | return self.always_exists or (name in self.present_files) |

3772 | n/a | |

3773 | n/a | from lib2to3.fixes import fix_import |

3774 | n/a | fix_import.exists = fake_exists |

3775 | n/a | |

3776 | n/a | def tearDown(self): |

3777 | n/a | from lib2to3.fixes import fix_import |

3778 | n/a | fix_import.exists = os.path.exists |

3779 | n/a | |

3780 | n/a | def check_both(self, b, a): |

3781 | n/a | self.always_exists = True |

3782 | n/a | super(Test_import, self).check(b, a) |

3783 | n/a | self.always_exists = False |

3784 | n/a | super(Test_import, self).unchanged(b) |

3785 | n/a | |

3786 | n/a | def test_files_checked(self): |

3787 | n/a | def p(path): |

3788 | n/a | # Takes a unix path and returns a path with correct separators |

3789 | n/a | return os.path.pathsep.join(path.split("/")) |

3790 | n/a | |

3791 | n/a | self.always_exists = False |

3792 | n/a | self.present_files = set(['__init__.py']) |

3793 | n/a | expected_extensions = ('.py', os.path.sep, '.pyc', '.so', '.sl', '.pyd') |

3794 | n/a | names_to_test = (p("/spam/eggs.py"), "ni.py", p("../../shrubbery.py")) |

3795 | n/a | |

3796 | n/a | for name in names_to_test: |

3797 | n/a | self.files_checked = [] |

3798 | n/a | self.filename = name |

3799 | n/a | self.unchanged("import jam") |

3800 | n/a | |

3801 | n/a | if os.path.dirname(name): |

3802 | n/a | name = os.path.dirname(name) + '/jam' |

3803 | n/a | else: |

3804 | n/a | name = 'jam' |

3805 | n/a | expected_checks = set(name + ext for ext in expected_extensions) |

3806 | n/a | expected_checks.add("__init__.py") |

3807 | n/a | |

3808 | n/a | self.assertEqual(set(self.files_checked), expected_checks) |

3809 | n/a | |

3810 | n/a | def test_not_in_package(self): |

3811 | n/a | s = "import bar" |

3812 | n/a | self.always_exists = False |

3813 | n/a | self.present_files = set(["bar.py"]) |

3814 | n/a | self.unchanged(s) |

3815 | n/a | |

3816 | n/a | def test_with_absolute_import_enabled(self): |

3817 | n/a | s = "from __future__ import absolute_import\nimport bar" |

3818 | n/a | self.always_exists = False |

3819 | n/a | self.present_files = set(["__init__.py", "bar.py"]) |

3820 | n/a | self.unchanged(s) |

3821 | n/a | |

3822 | n/a | def test_in_package(self): |

3823 | n/a | b = "import bar" |

3824 | n/a | a = "from . import bar" |

3825 | n/a | self.always_exists = False |

3826 | n/a | self.present_files = set(["__init__.py", "bar.py"]) |

3827 | n/a | self.check(b, a) |

3828 | n/a | |

3829 | n/a | def test_import_from_package(self): |

3830 | n/a | b = "import bar" |

3831 | n/a | a = "from . import bar" |

3832 | n/a | self.always_exists = False |

3833 | n/a | self.present_files = set(["__init__.py", "bar" + os.path.sep]) |

3834 | n/a | self.check(b, a) |

3835 | n/a | |

3836 | n/a | def test_already_relative_import(self): |

3837 | n/a | s = "from . import bar" |

3838 | n/a | self.unchanged(s) |

3839 | n/a | |

3840 | n/a | def test_comments_and_indent(self): |

3841 | n/a | b = "import bar # Foo" |

3842 | n/a | a = "from . import bar # Foo" |

3843 | n/a | self.check(b, a) |

3844 | n/a | |

3845 | n/a | def test_from(self): |

3846 | n/a | b = "from foo import bar, baz" |

3847 | n/a | a = "from .foo import bar, baz" |

3848 | n/a | self.check_both(b, a) |

3849 | n/a | |

3850 | n/a | b = "from foo import bar" |

3851 | n/a | a = "from .foo import bar" |

3852 | n/a | self.check_both(b, a) |

3853 | n/a | |

3854 | n/a | b = "from foo import (bar, baz)" |

3855 | n/a | a = "from .foo import (bar, baz)" |

3856 | n/a | self.check_both(b, a) |

3857 | n/a | |

3858 | n/a | def test_dotted_from(self): |

3859 | n/a | b = "from green.eggs import ham" |

3860 | n/a | a = "from .green.eggs import ham" |

3861 | n/a | self.check_both(b, a) |

3862 | n/a | |

3863 | n/a | def test_from_as(self): |

3864 | n/a | b = "from green.eggs import ham as spam" |

3865 | n/a | a = "from .green.eggs import ham as spam" |

3866 | n/a | self.check_both(b, a) |

3867 | n/a | |

3868 | n/a | def test_import(self): |

3869 | n/a | b = "import foo" |

3870 | n/a | a = "from . import foo" |

3871 | n/a | self.check_both(b, a) |

3872 | n/a | |

3873 | n/a | b = "import foo, bar" |

3874 | n/a | a = "from . import foo, bar" |

3875 | n/a | self.check_both(b, a) |

3876 | n/a | |

3877 | n/a | b = "import foo, bar, x" |

3878 | n/a | a = "from . import foo, bar, x" |

3879 | n/a | self.check_both(b, a) |

3880 | n/a | |

3881 | n/a | b = "import x, y, z" |

3882 | n/a | a = "from . import x, y, z" |

3883 | n/a | self.check_both(b, a) |

3884 | n/a | |

3885 | n/a | def test_import_as(self): |

3886 | n/a | b = "import foo as x" |

3887 | n/a | a = "from . import foo as x" |

3888 | n/a | self.check_both(b, a) |

3889 | n/a | |

3890 | n/a | b = "import a as b, b as c, c as d" |

3891 | n/a | a = "from . import a as b, b as c, c as d" |

3892 | n/a | self.check_both(b, a) |

3893 | n/a | |

3894 | n/a | def test_local_and_absolute(self): |

3895 | n/a | self.always_exists = False |

3896 | n/a | self.present_files = set(["foo.py", "__init__.py"]) |

3897 | n/a | |

3898 | n/a | s = "import foo, bar" |

3899 | n/a | self.warns_unchanged(s, "absolute and local imports together") |

3900 | n/a | |

3901 | n/a | def test_dotted_import(self): |

3902 | n/a | b = "import foo.bar" |

3903 | n/a | a = "from . import foo.bar" |

3904 | n/a | self.check_both(b, a) |

3905 | n/a | |

3906 | n/a | def test_dotted_import_as(self): |

3907 | n/a | b = "import foo.bar as bang" |

3908 | n/a | a = "from . import foo.bar as bang" |

3909 | n/a | self.check_both(b, a) |

3910 | n/a | |

3911 | n/a | def test_prefix(self): |

3912 | n/a | b = """ |

3913 | n/a | # prefix |

3914 | n/a | import foo.bar |

3915 | n/a | """ |

3916 | n/a | a = """ |

3917 | n/a | # prefix |

3918 | n/a | from . import foo.bar |

3919 | n/a | """ |

3920 | n/a | self.check_both(b, a) |

3921 | n/a | |

3922 | n/a | |

3923 | n/a | class Test_set_literal(FixerTestCase): |

3924 | n/a | |

3925 | n/a | fixer = "set_literal" |

3926 | n/a | |

3927 | n/a | def test_basic(self): |

3928 | n/a | b = """set([1, 2, 3])""" |

3929 | n/a | a = """{1, 2, 3}""" |

3930 | n/a | self.check(b, a) |

3931 | n/a | |

3932 | n/a | b = """set((1, 2, 3))""" |

3933 | n/a | a = """{1, 2, 3}""" |

3934 | n/a | self.check(b, a) |

3935 | n/a | |

3936 | n/a | b = """set((1,))""" |

3937 | n/a | a = """{1}""" |

3938 | n/a | self.check(b, a) |

3939 | n/a | |

3940 | n/a | b = """set([1])""" |

3941 | n/a | self.check(b, a) |

3942 | n/a | |

3943 | n/a | b = """set((a, b))""" |

3944 | n/a | a = """{a, b}""" |

3945 | n/a | self.check(b, a) |

3946 | n/a | |

3947 | n/a | b = """set([a, b])""" |

3948 | n/a | self.check(b, a) |

3949 | n/a | |

3950 | n/a | b = """set((a*234, f(args=23)))""" |

3951 | n/a | a = """{a*234, f(args=23)}""" |

3952 | n/a | self.check(b, a) |

3953 | n/a | |

3954 | n/a | b = """set([a*23, f(23)])""" |

3955 | n/a | a = """{a*23, f(23)}""" |

3956 | n/a | self.check(b, a) |

3957 | n/a | |

3958 | n/a | b = """set([a-234**23])""" |

3959 | n/a | a = """{a-234**23}""" |

3960 | n/a | self.check(b, a) |

3961 | n/a | |

3962 | n/a | def test_listcomps(self): |

3963 | n/a | b = """set([x for x in y])""" |

3964 | n/a | a = """{x for x in y}""" |

3965 | n/a | self.check(b, a) |

3966 | n/a | |

3967 | n/a | b = """set([x for x in y if x == m])""" |

3968 | n/a | a = """{x for x in y if x == m}""" |

3969 | n/a | self.check(b, a) |

3970 | n/a | |

3971 | n/a | b = """set([x for x in y for a in b])""" |

3972 | n/a | a = """{x for x in y for a in b}""" |

3973 | n/a | self.check(b, a) |

3974 | n/a | |

3975 | n/a | b = """set([f(x) - 23 for x in y])""" |

3976 | n/a | a = """{f(x) - 23 for x in y}""" |

3977 | n/a | self.check(b, a) |

3978 | n/a | |

3979 | n/a | def test_whitespace(self): |

3980 | n/a | b = """set( [1, 2])""" |

3981 | n/a | a = """{1, 2}""" |

3982 | n/a | self.check(b, a) |

3983 | n/a | |

3984 | n/a | b = """set([1 , 2])""" |

3985 | n/a | a = """{1 , 2}""" |

3986 | n/a | self.check(b, a) |

3987 | n/a | |

3988 | n/a | b = """set([ 1 ])""" |

3989 | n/a | a = """{ 1 }""" |

3990 | n/a | self.check(b, a) |

3991 | n/a | |

3992 | n/a | b = """set( [1] )""" |

3993 | n/a | a = """{1}""" |

3994 | n/a | self.check(b, a) |

3995 | n/a | |

3996 | n/a | b = """set([ 1, 2 ])""" |

3997 | n/a | a = """{ 1, 2 }""" |

3998 | n/a | self.check(b, a) |

3999 | n/a | |

4000 | n/a | b = """set([x for x in y ])""" |

4001 | n/a | a = """{x for x in y }""" |

4002 | n/a | self.check(b, a) |

4003 | n/a | |

4004 | n/a | b = """set( |

4005 | n/a | [1, 2] |

4006 | n/a | ) |

4007 | n/a | """ |

4008 | n/a | a = """{1, 2}\n""" |

4009 | n/a | self.check(b, a) |

4010 | n/a | |

4011 | n/a | def test_comments(self): |

4012 | n/a | b = """set((1, 2)) # Hi""" |

4013 | n/a | a = """{1, 2} # Hi""" |

4014 | n/a | self.check(b, a) |

4015 | n/a | |

4016 | n/a | # This isn't optimal behavior, but the fixer is optional. |

4017 | n/a | b = """ |

4018 | n/a | # Foo |

4019 | n/a | set( # Bar |

4020 | n/a | (1, 2) |

4021 | n/a | ) |

4022 | n/a | """ |

4023 | n/a | a = """ |

4024 | n/a | # Foo |

4025 | n/a | {1, 2} |

4026 | n/a | """ |

4027 | n/a | self.check(b, a) |

4028 | n/a | |

4029 | n/a | def test_unchanged(self): |

4030 | n/a | s = """set()""" |

4031 | n/a | self.unchanged(s) |

4032 | n/a | |

4033 | n/a | s = """set(a)""" |

4034 | n/a | self.unchanged(s) |

4035 | n/a | |

4036 | n/a | s = """set(a, b, c)""" |

4037 | n/a | self.unchanged(s) |

4038 | n/a | |

4039 | n/a | # Don't transform generators because they might have to be lazy. |

4040 | n/a | s = """set(x for x in y)""" |

4041 | n/a | self.unchanged(s) |

4042 | n/a | |

4043 | n/a | s = """set(x for x in y if z)""" |

4044 | n/a | self.unchanged(s) |

4045 | n/a | |

4046 | n/a | s = """set(a*823-23**2 + f(23))""" |

4047 | n/a | self.unchanged(s) |

4048 | n/a | |

4049 | n/a | |

4050 | n/a | class Test_sys_exc(FixerTestCase): |

4051 | n/a | fixer = "sys_exc" |

4052 | n/a | |

4053 | n/a | def test_0(self): |

4054 | n/a | b = "sys.exc_type" |

4055 | n/a | a = "sys.exc_info()[0]" |

4056 | n/a | self.check(b, a) |

4057 | n/a | |

4058 | n/a | def test_1(self): |

4059 | n/a | b = "sys.exc_value" |

4060 | n/a | a = "sys.exc_info()[1]" |

4061 | n/a | self.check(b, a) |

4062 | n/a | |

4063 | n/a | def test_2(self): |

4064 | n/a | b = "sys.exc_traceback" |

4065 | n/a | a = "sys.exc_info()[2]" |

4066 | n/a | self.check(b, a) |

4067 | n/a | |

4068 | n/a | def test_3(self): |

4069 | n/a | b = "sys.exc_type # Foo" |

4070 | n/a | a = "sys.exc_info()[0] # Foo" |

4071 | n/a | self.check(b, a) |

4072 | n/a | |

4073 | n/a | def test_4(self): |

4074 | n/a | b = "sys. exc_type" |

4075 | n/a | a = "sys. exc_info()[0]" |

4076 | n/a | self.check(b, a) |

4077 | n/a | |

4078 | n/a | def test_5(self): |

4079 | n/a | b = "sys .exc_type" |

4080 | n/a | a = "sys .exc_info()[0]" |

4081 | n/a | self.check(b, a) |

4082 | n/a | |

4083 | n/a | |

4084 | n/a | class Test_paren(FixerTestCase): |

4085 | n/a | fixer = "paren" |

4086 | n/a | |

4087 | n/a | def test_0(self): |

4088 | n/a | b = """[i for i in 1, 2 ]""" |

4089 | n/a | a = """[i for i in (1, 2) ]""" |

4090 | n/a | self.check(b, a) |

4091 | n/a | |

4092 | n/a | def test_1(self): |

4093 | n/a | b = """[i for i in 1, 2, ]""" |

4094 | n/a | a = """[i for i in (1, 2,) ]""" |

4095 | n/a | self.check(b, a) |

4096 | n/a | |

4097 | n/a | def test_2(self): |

4098 | n/a | b = """[i for i in 1, 2 ]""" |

4099 | n/a | a = """[i for i in (1, 2) ]""" |

4100 | n/a | self.check(b, a) |

4101 | n/a | |

4102 | n/a | def test_3(self): |

4103 | n/a | b = """[i for i in 1, 2 if i]""" |

4104 | n/a | a = """[i for i in (1, 2) if i]""" |

4105 | n/a | self.check(b, a) |

4106 | n/a | |

4107 | n/a | def test_4(self): |

4108 | n/a | b = """[i for i in 1, 2 ]""" |

4109 | n/a | a = """[i for i in (1, 2) ]""" |

4110 | n/a | self.check(b, a) |

4111 | n/a | |

4112 | n/a | def test_5(self): |

4113 | n/a | b = """(i for i in 1, 2)""" |

4114 | n/a | a = """(i for i in (1, 2))""" |

4115 | n/a | self.check(b, a) |

4116 | n/a | |

4117 | n/a | def test_6(self): |

4118 | n/a | b = """(i for i in 1 ,2 if i)""" |

4119 | n/a | a = """(i for i in (1 ,2) if i)""" |

4120 | n/a | self.check(b, a) |

4121 | n/a | |

4122 | n/a | def test_unchanged_0(self): |

4123 | n/a | s = """[i for i in (1, 2)]""" |

4124 | n/a | self.unchanged(s) |

4125 | n/a | |

4126 | n/a | def test_unchanged_1(self): |

4127 | n/a | s = """[i for i in foo()]""" |

4128 | n/a | self.unchanged(s) |

4129 | n/a | |

4130 | n/a | def test_unchanged_2(self): |

4131 | n/a | s = """[i for i in (1, 2) if nothing]""" |

4132 | n/a | self.unchanged(s) |

4133 | n/a | |

4134 | n/a | def test_unchanged_3(self): |

4135 | n/a | s = """(i for i in (1, 2))""" |

4136 | n/a | self.unchanged(s) |

4137 | n/a | |

4138 | n/a | def test_unchanged_4(self): |

4139 | n/a | s = """[i for i in m]""" |

4140 | n/a | self.unchanged(s) |

4141 | n/a | |

4142 | n/a | class Test_metaclass(FixerTestCase): |

4143 | n/a | |

4144 | n/a | fixer = 'metaclass' |

4145 | n/a | |

4146 | n/a | def test_unchanged(self): |

4147 | n/a | self.unchanged("class X(): pass") |

4148 | n/a | self.unchanged("class X(object): pass") |

4149 | n/a | self.unchanged("class X(object1, object2): pass") |

4150 | n/a | self.unchanged("class X(object1, object2, object3): pass") |

4151 | n/a | self.unchanged("class X(metaclass=Meta): pass") |

4152 | n/a | self.unchanged("class X(b, arg=23, metclass=Meta): pass") |

4153 | n/a | self.unchanged("class X(b, arg=23, metaclass=Meta, other=42): pass") |

4154 | n/a | |

4155 | n/a | s = """ |

4156 | n/a | class X: |

4157 | n/a | def __metaclass__(self): pass |

4158 | n/a | """ |

4159 | n/a | self.unchanged(s) |

4160 | n/a | |

4161 | n/a | s = """ |

4162 | n/a | class X: |

4163 | n/a | a[23] = 74 |

4164 | n/a | """ |

4165 | n/a | self.unchanged(s) |

4166 | n/a | |

4167 | n/a | def test_comments(self): |

4168 | n/a | b = """ |

4169 | n/a | class X: |

4170 | n/a | # hi |

4171 | n/a | __metaclass__ = AppleMeta |

4172 | n/a | """ |

4173 | n/a | a = """ |

4174 | n/a | class X(metaclass=AppleMeta): |

4175 | n/a | # hi |

4176 | n/a | pass |

4177 | n/a | """ |

4178 | n/a | self.check(b, a) |

4179 | n/a | |

4180 | n/a | b = """ |

4181 | n/a | class X: |

4182 | n/a | __metaclass__ = Meta |

4183 | n/a | # Bedtime! |

4184 | n/a | """ |

4185 | n/a | a = """ |

4186 | n/a | class X(metaclass=Meta): |

4187 | n/a | pass |

4188 | n/a | # Bedtime! |

4189 | n/a | """ |

4190 | n/a | self.check(b, a) |

4191 | n/a | |

4192 | n/a | def test_meta(self): |

4193 | n/a | # no-parent class, odd body |

4194 | n/a | b = """ |

4195 | n/a | class X(): |

4196 | n/a | __metaclass__ = Q |

4197 | n/a | pass |

4198 | n/a | """ |

4199 | n/a | a = """ |

4200 | n/a | class X(metaclass=Q): |

4201 | n/a | pass |

4202 | n/a | """ |

4203 | n/a | self.check(b, a) |

4204 | n/a | |

4205 | n/a | # one parent class, no body |

4206 | n/a | b = """class X(object): __metaclass__ = Q""" |

4207 | n/a | a = """class X(object, metaclass=Q): pass""" |

4208 | n/a | self.check(b, a) |

4209 | n/a | |

4210 | n/a | |

4211 | n/a | # one parent, simple body |

4212 | n/a | b = """ |

4213 | n/a | class X(object): |

4214 | n/a | __metaclass__ = Meta |

4215 | n/a | bar = 7 |

4216 | n/a | """ |

4217 | n/a | a = """ |

4218 | n/a | class X(object, metaclass=Meta): |

4219 | n/a | bar = 7 |

4220 | n/a | """ |

4221 | n/a | self.check(b, a) |

4222 | n/a | |

4223 | n/a | b = """ |

4224 | n/a | class X: |

4225 | n/a | __metaclass__ = Meta; x = 4; g = 23 |

4226 | n/a | """ |

4227 | n/a | a = """ |

4228 | n/a | class X(metaclass=Meta): |

4229 | n/a | x = 4; g = 23 |

4230 | n/a | """ |

4231 | n/a | self.check(b, a) |

4232 | n/a | |

4233 | n/a | # one parent, simple body, __metaclass__ last |

4234 | n/a | b = """ |

4235 | n/a | class X(object): |

4236 | n/a | bar = 7 |

4237 | n/a | __metaclass__ = Meta |

4238 | n/a | """ |

4239 | n/a | a = """ |

4240 | n/a | class X(object, metaclass=Meta): |

4241 | n/a | bar = 7 |

4242 | n/a | """ |

4243 | n/a | self.check(b, a) |

4244 | n/a | |

4245 | n/a | # redefining __metaclass__ |

4246 | n/a | b = """ |

4247 | n/a | class X(): |

4248 | n/a | __metaclass__ = A |

4249 | n/a | __metaclass__ = B |

4250 | n/a | bar = 7 |

4251 | n/a | """ |

4252 | n/a | a = """ |

4253 | n/a | class X(metaclass=B): |

4254 | n/a | bar = 7 |

4255 | n/a | """ |

4256 | n/a | self.check(b, a) |

4257 | n/a | |

4258 | n/a | # multiple inheritance, simple body |

4259 | n/a | b = """ |

4260 | n/a | class X(clsA, clsB): |

4261 | n/a | __metaclass__ = Meta |

4262 | n/a | bar = 7 |

4263 | n/a | """ |

4264 | n/a | a = """ |

4265 | n/a | class X(clsA, clsB, metaclass=Meta): |

4266 | n/a | bar = 7 |

4267 | n/a | """ |

4268 | n/a | self.check(b, a) |

4269 | n/a | |

4270 | n/a | # keywords in the class statement |

4271 | n/a | b = """class m(a, arg=23): __metaclass__ = Meta""" |

4272 | n/a | a = """class m(a, arg=23, metaclass=Meta): pass""" |

4273 | n/a | self.check(b, a) |

4274 | n/a | |

4275 | n/a | b = """ |

4276 | n/a | class X(expression(2 + 4)): |

4277 | n/a | __metaclass__ = Meta |

4278 | n/a | """ |

4279 | n/a | a = """ |

4280 | n/a | class X(expression(2 + 4), metaclass=Meta): |

4281 | n/a | pass |

4282 | n/a | """ |

4283 | n/a | self.check(b, a) |

4284 | n/a | |

4285 | n/a | b = """ |

4286 | n/a | class X(expression(2 + 4), x**4): |

4287 | n/a | __metaclass__ = Meta |

4288 | n/a | """ |

4289 | n/a | a = """ |

4290 | n/a | class X(expression(2 + 4), x**4, metaclass=Meta): |

4291 | n/a | pass |

4292 | n/a | """ |

4293 | n/a | self.check(b, a) |

4294 | n/a | |

4295 | n/a | b = """ |

4296 | n/a | class X: |

4297 | n/a | __metaclass__ = Meta |

4298 | n/a | save.py = 23 |

4299 | n/a | """ |

4300 | n/a | a = """ |

4301 | n/a | class X(metaclass=Meta): |

4302 | n/a | save.py = 23 |

4303 | n/a | """ |

4304 | n/a | self.check(b, a) |

4305 | n/a | |

4306 | n/a | |

4307 | n/a | class Test_getcwdu(FixerTestCase): |

4308 | n/a | |

4309 | n/a | fixer = 'getcwdu' |

4310 | n/a | |

4311 | n/a | def test_basic(self): |

4312 | n/a | b = """os.getcwdu""" |

4313 | n/a | a = """os.getcwd""" |

4314 | n/a | self.check(b, a) |

4315 | n/a | |

4316 | n/a | b = """os.getcwdu()""" |

4317 | n/a | a = """os.getcwd()""" |

4318 | n/a | self.check(b, a) |

4319 | n/a | |

4320 | n/a | b = """meth = os.getcwdu""" |

4321 | n/a | a = """meth = os.getcwd""" |

4322 | n/a | self.check(b, a) |

4323 | n/a | |

4324 | n/a | b = """os.getcwdu(args)""" |

4325 | n/a | a = """os.getcwd(args)""" |

4326 | n/a | self.check(b, a) |

4327 | n/a | |

4328 | n/a | def test_comment(self): |

4329 | n/a | b = """os.getcwdu() # Foo""" |

4330 | n/a | a = """os.getcwd() # Foo""" |

4331 | n/a | self.check(b, a) |

4332 | n/a | |

4333 | n/a | def test_unchanged(self): |

4334 | n/a | s = """os.getcwd()""" |

4335 | n/a | self.unchanged(s) |

4336 | n/a | |

4337 | n/a | s = """getcwdu()""" |

4338 | n/a | self.unchanged(s) |

4339 | n/a | |

4340 | n/a | s = """os.getcwdb()""" |

4341 | n/a | self.unchanged(s) |

4342 | n/a | |

4343 | n/a | def test_indentation(self): |

4344 | n/a | b = """ |

4345 | n/a | if 1: |

4346 | n/a | os.getcwdu() |

4347 | n/a | """ |

4348 | n/a | a = """ |

4349 | n/a | if 1: |

4350 | n/a | os.getcwd() |

4351 | n/a | """ |

4352 | n/a | self.check(b, a) |

4353 | n/a | |

4354 | n/a | def test_multilation(self): |

4355 | n/a | b = """os .getcwdu()""" |

4356 | n/a | a = """os .getcwd()""" |

4357 | n/a | self.check(b, a) |

4358 | n/a | |

4359 | n/a | b = """os. getcwdu""" |

4360 | n/a | a = """os. getcwd""" |

4361 | n/a | self.check(b, a) |

4362 | n/a | |

4363 | n/a | b = """os.getcwdu ( )""" |

4364 | n/a | a = """os.getcwd ( )""" |

4365 | n/a | self.check(b, a) |

4366 | n/a | |

4367 | n/a | |

4368 | n/a | class Test_operator(FixerTestCase): |

4369 | n/a | |

4370 | n/a | fixer = "operator" |

4371 | n/a | |

4372 | n/a | def test_operator_isCallable(self): |

4373 | n/a | b = "operator.isCallable(x)" |

4374 | n/a | a = "hasattr(x, '__call__')" |

4375 | n/a | self.check(b, a) |

4376 | n/a | |

4377 | n/a | def test_operator_sequenceIncludes(self): |

4378 | n/a | b = "operator.sequenceIncludes(x, y)" |

4379 | n/a | a = "operator.contains(x, y)" |

4380 | n/a | self.check(b, a) |

4381 | n/a | |

4382 | n/a | b = "operator .sequenceIncludes(x, y)" |

4383 | n/a | a = "operator .contains(x, y)" |

4384 | n/a | self.check(b, a) |

4385 | n/a | |

4386 | n/a | b = "operator. sequenceIncludes(x, y)" |

4387 | n/a | a = "operator. contains(x, y)" |

4388 | n/a | self.check(b, a) |

4389 | n/a | |

4390 | n/a | def test_operator_isSequenceType(self): |

4391 | n/a | b = "operator.isSequenceType(x)" |

4392 | n/a | a = "import collections\nisinstance(x, collections.Sequence)" |

4393 | n/a | self.check(b, a) |

4394 | n/a | |

4395 | n/a | def test_operator_isMappingType(self): |

4396 | n/a | b = "operator.isMappingType(x)" |

4397 | n/a | a = "import collections\nisinstance(x, collections.Mapping)" |

4398 | n/a | self.check(b, a) |

4399 | n/a | |

4400 | n/a | def test_operator_isNumberType(self): |

4401 | n/a | b = "operator.isNumberType(x)" |

4402 | n/a | a = "import numbers\nisinstance(x, numbers.Number)" |

4403 | n/a | self.check(b, a) |

4404 | n/a | |

4405 | n/a | def test_operator_repeat(self): |

4406 | n/a | b = "operator.repeat(x, n)" |

4407 | n/a | a = "operator.mul(x, n)" |

4408 | n/a | self.check(b, a) |

4409 | n/a | |

4410 | n/a | b = "operator .repeat(x, n)" |

4411 | n/a | a = "operator .mul(x, n)" |

4412 | n/a | self.check(b, a) |

4413 | n/a | |

4414 | n/a | b = "operator. repeat(x, n)" |

4415 | n/a | a = "operator. mul(x, n)" |

4416 | n/a | self.check(b, a) |

4417 | n/a | |

4418 | n/a | def test_operator_irepeat(self): |

4419 | n/a | b = "operator.irepeat(x, n)" |

4420 | n/a | a = "operator.imul(x, n)" |

4421 | n/a | self.check(b, a) |

4422 | n/a | |

4423 | n/a | b = "operator .irepeat(x, n)" |

4424 | n/a | a = "operator .imul(x, n)" |

4425 | n/a | self.check(b, a) |

4426 | n/a | |

4427 | n/a | b = "operator. irepeat(x, n)" |

4428 | n/a | a = "operator. imul(x, n)" |

4429 | n/a | self.check(b, a) |

4430 | n/a | |

4431 | n/a | def test_bare_isCallable(self): |

4432 | n/a | s = "isCallable(x)" |

4433 | n/a | t = "You should use 'hasattr(x, '__call__')' here." |

4434 | n/a | self.warns_unchanged(s, t) |

4435 | n/a | |

4436 | n/a | def test_bare_sequenceIncludes(self): |

4437 | n/a | s = "sequenceIncludes(x, y)" |

4438 | n/a | t = "You should use 'operator.contains(x, y)' here." |

4439 | n/a | self.warns_unchanged(s, t) |

4440 | n/a | |

4441 | n/a | def test_bare_operator_isSequenceType(self): |

4442 | n/a | s = "isSequenceType(z)" |

4443 | n/a | t = "You should use 'isinstance(z, collections.Sequence)' here." |

4444 | n/a | self.warns_unchanged(s, t) |

4445 | n/a | |

4446 | n/a | def test_bare_operator_isMappingType(self): |

4447 | n/a | s = "isMappingType(x)" |

4448 | n/a | t = "You should use 'isinstance(x, collections.Mapping)' here." |

4449 | n/a | self.warns_unchanged(s, t) |

4450 | n/a | |

4451 | n/a | def test_bare_operator_isNumberType(self): |

4452 | n/a | s = "isNumberType(y)" |

4453 | n/a | t = "You should use 'isinstance(y, numbers.Number)' here." |

4454 | n/a | self.warns_unchanged(s, t) |

4455 | n/a | |

4456 | n/a | def test_bare_operator_repeat(self): |

4457 | n/a | s = "repeat(x, n)" |

4458 | n/a | t = "You should use 'operator.mul(x, n)' here." |

4459 | n/a | self.warns_unchanged(s, t) |

4460 | n/a | |

4461 | n/a | def test_bare_operator_irepeat(self): |

4462 | n/a | s = "irepeat(y, 187)" |

4463 | n/a | t = "You should use 'operator.imul(y, 187)' here." |

4464 | n/a | self.warns_unchanged(s, t) |

4465 | n/a | |

4466 | n/a | |

4467 | n/a | class Test_exitfunc(FixerTestCase): |

4468 | n/a | |

4469 | n/a | fixer = "exitfunc" |

4470 | n/a | |

4471 | n/a | def test_simple(self): |

4472 | n/a | b = """ |

4473 | n/a | import sys |

4474 | n/a | sys.exitfunc = my_atexit |

4475 | n/a | """ |

4476 | n/a | a = """ |

4477 | n/a | import sys |

4478 | n/a | import atexit |

4479 | n/a | atexit.register(my_atexit) |

4480 | n/a | """ |

4481 | n/a | self.check(b, a) |

4482 | n/a | |

4483 | n/a | def test_names_import(self): |

4484 | n/a | b = """ |

4485 | n/a | import sys, crumbs |

4486 | n/a | sys.exitfunc = my_func |

4487 | n/a | """ |

4488 | n/a | a = """ |

4489 | n/a | import sys, crumbs, atexit |

4490 | n/a | atexit.register(my_func) |

4491 | n/a | """ |

4492 | n/a | self.check(b, a) |

4493 | n/a | |

4494 | n/a | def test_complex_expression(self): |

4495 | n/a | b = """ |

4496 | n/a | import sys |

4497 | n/a | sys.exitfunc = do(d)/a()+complex(f=23, g=23)*expression |

4498 | n/a | """ |

4499 | n/a | a = """ |

4500 | n/a | import sys |

4501 | n/a | import atexit |

4502 | n/a | atexit.register(do(d)/a()+complex(f=23, g=23)*expression) |

4503 | n/a | """ |

4504 | n/a | self.check(b, a) |

4505 | n/a | |

4506 | n/a | def test_comments(self): |

4507 | n/a | b = """ |

4508 | n/a | import sys # Foo |

4509 | n/a | sys.exitfunc = f # Blah |

4510 | n/a | """ |

4511 | n/a | a = """ |

4512 | n/a | import sys |

4513 | n/a | import atexit # Foo |

4514 | n/a | atexit.register(f) # Blah |

4515 | n/a | """ |

4516 | n/a | self.check(b, a) |

4517 | n/a | |

4518 | n/a | b = """ |

4519 | n/a | import apples, sys, crumbs, larry # Pleasant comments |

4520 | n/a | sys.exitfunc = func |

4521 | n/a | """ |

4522 | n/a | a = """ |

4523 | n/a | import apples, sys, crumbs, larry, atexit # Pleasant comments |

4524 | n/a | atexit.register(func) |

4525 | n/a | """ |

4526 | n/a | self.check(b, a) |

4527 | n/a | |

4528 | n/a | def test_in_a_function(self): |

4529 | n/a | b = """ |

4530 | n/a | import sys |

4531 | n/a | def f(): |

4532 | n/a | sys.exitfunc = func |

4533 | n/a | """ |

4534 | n/a | a = """ |

4535 | n/a | import sys |

4536 | n/a | import atexit |

4537 | n/a | def f(): |

4538 | n/a | atexit.register(func) |

4539 | n/a | """ |

4540 | n/a | self.check(b, a) |

4541 | n/a | |

4542 | n/a | def test_no_sys_import(self): |

4543 | n/a | b = """sys.exitfunc = f""" |

4544 | n/a | a = """atexit.register(f)""" |

4545 | n/a | msg = ("Can't find sys import; Please add an atexit import at the " |

4546 | n/a | "top of your file.") |

4547 | n/a | self.warns(b, a, msg) |

4548 | n/a | |

4549 | n/a | |

4550 | n/a | def test_unchanged(self): |

4551 | n/a | s = """f(sys.exitfunc)""" |

4552 | n/a | self.unchanged(s) |

4553 | n/a | |

4554 | n/a | |

4555 | n/a | class Test_asserts(FixerTestCase): |

4556 | n/a | |

4557 | n/a | fixer = "asserts" |

4558 | n/a | |

4559 | n/a | def test_deprecated_names(self): |

4560 | n/a | tests = [ |

4561 | n/a | ('self.assert_(True)', 'self.assertTrue(True)'), |

4562 | n/a | ('self.assertEquals(2, 2)', 'self.assertEqual(2, 2)'), |

4563 | n/a | ('self.assertNotEquals(2, 3)', 'self.assertNotEqual(2, 3)'), |

4564 | n/a | ('self.assertAlmostEquals(2, 3)', 'self.assertAlmostEqual(2, 3)'), |

4565 | n/a | ('self.assertNotAlmostEquals(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), |

4566 | n/a | ('self.failUnlessEqual(2, 2)', 'self.assertEqual(2, 2)'), |

4567 | n/a | ('self.failIfEqual(2, 3)', 'self.assertNotEqual(2, 3)'), |

4568 | n/a | ('self.failUnlessAlmostEqual(2, 3)', 'self.assertAlmostEqual(2, 3)'), |

4569 | n/a | ('self.failIfAlmostEqual(2, 8)', 'self.assertNotAlmostEqual(2, 8)'), |

4570 | n/a | ('self.failUnless(True)', 'self.assertTrue(True)'), |

4571 | n/a | ('self.failUnlessRaises(foo)', 'self.assertRaises(foo)'), |

4572 | n/a | ('self.failIf(False)', 'self.assertFalse(False)'), |

4573 | n/a | ] |

4574 | n/a | for b, a in tests: |

4575 | n/a | self.check(b, a) |

4576 | n/a | |

4577 | n/a | def test_variants(self): |

4578 | n/a | b = 'eq = self.assertEquals' |

4579 | n/a | a = 'eq = self.assertEqual' |

4580 | n/a | self.check(b, a) |

4581 | n/a | b = 'self.assertEquals(2, 3, msg="fail")' |

4582 | n/a | a = 'self.assertEqual(2, 3, msg="fail")' |

4583 | n/a | self.check(b, a) |

4584 | n/a | b = 'self.assertEquals(2, 3, msg="fail") # foo' |

4585 | n/a | a = 'self.assertEqual(2, 3, msg="fail") # foo' |

4586 | n/a | self.check(b, a) |

4587 | n/a | b = 'self.assertEquals (2, 3)' |

4588 | n/a | a = 'self.assertEqual (2, 3)' |

4589 | n/a | self.check(b, a) |

4590 | n/a | b = ' self.assertEquals (2, 3)' |

4591 | n/a | a = ' self.assertEqual (2, 3)' |

4592 | n/a | self.check(b, a) |

4593 | n/a | b = 'with self.failUnlessRaises(Explosion): explode()' |

4594 | n/a | a = 'with self.assertRaises(Explosion): explode()' |

4595 | n/a | self.check(b, a) |

4596 | n/a | b = 'with self.failUnlessRaises(Explosion) as cm: explode()' |

4597 | n/a | a = 'with self.assertRaises(Explosion) as cm: explode()' |

4598 | n/a | self.check(b, a) |

4599 | n/a | |

4600 | n/a | def test_unchanged(self): |

4601 | n/a | self.unchanged('self.assertEqualsOnSaturday') |

4602 | n/a | self.unchanged('self.assertEqualsOnSaturday(3, 5)') |