ยปCore Development>Code coverage>Lib/lib2to3/tests/test_fixers.py

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

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