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