-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
six_test.py
444 lines (403 loc) · 12 KB
/
six_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import sys
import pytest
from pyupgrade._data import Settings
from pyupgrade._main import _fix_plugins
@pytest.mark.parametrize(
's',
(
# syntax error
'x = (',
# unrelated
'from os import path',
'from six import moves',
'a[0]()',
# unrelated decorator
'@mydec\n'
'class C: pass',
# don't rewrite things that would become `raise` in non-statements
'print(six.raise_from(exc, exc_from))',
# intentionally not handling this case due to it being a bug (?)
'class C(six.with_metaclass(Meta, B), D): pass',
# cannot determine args to rewrite them
'six.reraise(*err)', 'six.u(*a)',
'class C(six.with_metaclass(*a)): pass',
'@six.add_metaclass(*a)\n'
'class C: pass\n',
# next is shadowed
'next()',
('traceback.format_exc(*sys.exc_info())'),
pytest.param('six.iteritems()', id='wrong argument count'),
pytest.param(
'from six import iteritems as items\n'
'items(foo)\n',
id='ignore as renaming',
),
),
)
def test_fix_six_noop(s):
assert _fix_plugins(s, settings=Settings(min_version=(3,))) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
(
'six.byte2int(b"f")',
'b"f"[0]',
),
(
'six.get_unbound_function(meth)\n',
'meth\n',
),
(
'from six import get_unbound_function\n'
'get_unbound_function(meth)\n',
'from six import get_unbound_function\n'
'meth\n',
),
(
'six.indexbytes(bs, i)\n',
'bs[i]\n',
),
(
'six.assertCountEqual(\n'
' self,\n'
' arg1,\n'
' arg2,\n'
')',
'self.assertCountEqual(\n'
' arg1,\n'
' arg2,\n'
')',
),
(
'six.assertCountEqual(\n'
' self,\\\n'
' arg1,\n'
' arg2,\n'
')',
'self.assertCountEqual(\\\n'
' arg1,\n'
' arg2,\n'
')',
),
(
'six.assertCountEqual(\n'
' self, # hello\n'
' arg1,\n'
' arg2,\n'
')',
'self.assertCountEqual(\n'
' arg1,\n'
' arg2,\n'
')',
),
(
'six.assertCountEqual(\n'
' self,\n'
' arg1,\n'
' (1, 2, 3),\n'
')',
'self.assertCountEqual(\n'
' arg1,\n'
' (1, 2, 3),\n'
')',
),
pytest.param(
'six.u ("bar")',
'"bar"',
id='weird spacing six.u',
),
pytest.param(
'from six import u\nu ("bar")',
'from six import u\n"bar"',
id='weird spacing u',
),
(
'six.raise_from(exc, exc_from)\n',
'raise exc from exc_from\n',
),
(
'six.reraise(tp, exc, tb)\n',
'raise exc.with_traceback(tb)\n',
),
(
'six.reraise(tp, exc)\n',
'raise exc.with_traceback(None)\n',
),
(
'six.reraise(*sys.exc_info())\n',
'raise\n',
),
(
'from sys import exc_info\n'
'six.reraise(*exc_info())\n',
'from sys import exc_info\n'
'raise\n',
),
(
'from six import raise_from\n'
'raise_from(exc, exc_from)\n',
'from six import raise_from\n'
'raise exc from exc_from\n',
),
(
'six.reraise(\n'
' tp,\n'
' exc,\n'
' tb,\n'
')\n',
'raise exc.with_traceback(tb)\n',
),
pytest.param(
'six.raise_from (exc, exc_from)',
'raise exc from exc_from',
id='weird spacing six.raise_from',
),
pytest.param(
'from six import raise_from\nraise_from (exc, exc_from)',
'from six import raise_from\nraise exc from exc_from',
id='weird spacing raise_from',
),
(
'class C(six.with_metaclass(M)): pass',
'class C(metaclass=M): pass',
),
(
'class C(six.with_metaclass(M, B)): pass',
'class C(B, metaclass=M): pass',
),
(
'class C(six.with_metaclass(M, B1, B2)): pass',
'class C(B1, B2, metaclass=M): pass',
),
(
'from six import with_metaclass\n'
'class C(with_metaclass(M, B)): pass\n',
'from six import with_metaclass\n'
'class C(B, metaclass=M): pass\n',
),
pytest.param(
'class C(six.with_metaclass (M, B)): pass',
'class C(B, metaclass=M): pass',
id='weird spacing six.with_metaclass',
),
pytest.param(
'from six import with_metaclass\n'
'class C(with_metaclass (M, B)): pass',
'from six import with_metaclass\n'
'class C(B, metaclass=M): pass',
id='weird spacing with_metaclass',
),
pytest.param(
'from six import with_metaclass\n'
'class C(with_metaclass(M, object)): pass',
'from six import with_metaclass\n'
'class C(metaclass=M): pass',
id='elide object base in with_metaclass',
),
pytest.param(
'class C(six.with_metaclass(M, B,)): pass',
'class C(B, metaclass=M): pass',
id='with_metaclass and trailing comma',
),
pytest.param(
'@six.add_metaclass(M)\n'
'class C: pass\n',
'class C(metaclass=M): pass\n',
id='basic six.add_metaclass',
),
pytest.param(
'from six import add_metaclass\n'
'@add_metaclass(M)\n'
'class C: pass\n',
'from six import add_metaclass\n'
'class C(metaclass=M): pass\n',
id='basic add_metaclass',
),
pytest.param(
'@six.add_metaclass(M)\n'
'class C(): pass\n',
'class C(metaclass=M): pass\n',
id='basic six.add_metaclass, no bases but parens',
),
pytest.param(
'@six.add_metaclass(M)\n'
'class C(A): pass\n',
'class C(A, metaclass=M): pass\n',
id='add_metaclass, one base',
),
pytest.param(
'@six.add_metaclass(M)\n'
'class C(\n'
' A,\n'
'): pass\n',
'class C(\n'
' A, metaclass=M,\n'
'): pass\n',
id='add_metaclass, base with trailing comma',
),
pytest.param(
'x = (object,)\n'
'@six.add_metaclass(M)\n'
'class C(x[:][0]): pass\n',
'x = (object,)\n'
'class C(x[:][0], metaclass=M): pass\n',
id='add_metaclass, weird base that contains a :',
),
pytest.param(
'if True:\n'
' @six.add_metaclass(M)\n'
' class C: pass\n',
'if True:\n'
' class C(metaclass=M): pass\n',
id='add_metaclass, indented',
),
pytest.param(
'print(six.itervalues({1:2}))\n',
'print({1:2}.values())\n',
id='six.itervalues',
),
pytest.param(
'print(next(six.itervalues({1:2})))\n',
'print(next(iter({1:2}.values())))\n',
id='six.itervalues inside next(...)',
),
pytest.param(
'for _ in six.itervalues({} or y): pass',
'for _ in ({} or y).values(): pass',
id='needs parenthesizing for BoolOp',
),
pytest.param(
'for _ in six.itervalues({} | y): pass',
'for _ in ({} | y).values(): pass',
id='needs parenthesizing for BinOp',
),
pytest.param(
'six.int2byte(x | y)',
'bytes((x | y,))',
id='no parenthesize for int2byte BinOP',
),
pytest.param(
'six.iteritems(+weird_dct)',
'(+weird_dct).items()',
id='needs parenthesizing for UnaryOp',
),
pytest.param(
'x = six.get_method_function(lambda: x)',
'x = (lambda: x).__func__',
id='needs parenthesizing for Lambda',
),
pytest.param(
'for _ in six.itervalues(x if 1 else y): pass',
'for _ in (x if 1 else y).values(): pass',
id='needs parenthesizing for IfExp',
),
# this one is bogus / impossible, but parenthesize it anyway
pytest.param(
'six.itervalues(x for x in y)',
'(x for x in y).values()',
id='needs parentehsizing for GeneratorExp',
),
pytest.param(
'async def f():\n'
' return six.iteritems(await y)\n',
'async def f():\n'
' return (await y).items()\n',
id='needs parenthesizing for Await',
),
# this one is bogus / impossible, but parenthesize it anyway
pytest.param(
'six.itervalues(x < y)',
'(x < y).values()',
id='needs parentehsizing for Compare',
),
pytest.param(
'x = six.itervalues(\n'
' # comment\n'
' x\n'
')',
'x = (\n'
' # comment\n'
' x\n'
').values()',
id='multiline first argument with comment',
),
pytest.param(
'x = six.itervalues(\n'
' # comment\n'
' x,\n'
')',
# TODO: ideally this would preserve whitespace better
'x = (\n'
' # comment\n'
' x).values()',
id='multiline first argument with comment, trailing comma',
),
),
)
def test_fix_six(s, expected):
ret = _fix_plugins(s, settings=Settings(min_version=(3,)))
assert ret == expected
@pytest.mark.xfail(sys.version_info < (3, 8), reason='walrus')
@pytest.mark.parametrize(
('s', 'expected'),
(
pytest.param(
'for _ in six.itervalues(x := y): pass',
'for _ in (x := y).values(): pass',
id='needs parenthesizing for NamedExpr',
),
),
)
def test_fix_six_py38_plus(s, expected):
ret = _fix_plugins(s, settings=Settings(min_version=(3,)))
assert ret == expected
@pytest.mark.parametrize(
('s', 'expected'),
(
(
'import six\n\nclass C(six.Iterator): pass',
'import six\n\nclass C: pass',
),
(
'from six import Iterator\n'
'\n'
'class C(Iterator): pass',
'from six import Iterator\n'
'\n'
'class C: pass',
),
(
'import six\n'
'\n'
'class C(\n'
' six.Iterator,\n'
'): pass',
'import six\n'
'\n'
'class C: pass',
),
(
'class C(object, six.Iterator): pass',
'class C: pass',
),
(
'class C(six.Iterator, metaclass=ABCMeta): pass',
'class C(metaclass=ABCMeta): pass',
),
(
'class C(six.Iterator, object, metaclass=ABCMeta): pass',
'class C(metaclass=ABCMeta): pass',
),
(
'from six import Iterator\n'
'\n'
'class C(Iterator, metaclass=ABCMeta): pass',
'from six import Iterator\n'
'\n'
'class C(metaclass=ABCMeta): pass',
),
),
)
def test_fix_base_classes(s, expected):
ret = _fix_plugins(s, settings=Settings(min_version=(3,)))
assert ret == expected