-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest_rename_locals.py
More file actions
419 lines (332 loc) · 9.11 KB
/
test_rename_locals.py
File metadata and controls
419 lines (332 loc) · 9.11 KB
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
"""
Test for renaming of local names
This assumes the standard NameAssigner and name_generator
"""
import ast
import sys
import pytest
from python_minifier import unparse
from python_minifier.ast_annotation import add_parent
from python_minifier.ast_compare import CompareError, compare_ast
from python_minifier.rename import add_namespace, allow_rename_globals, allow_rename_locals, bind_names, rename, resolve_names
def rename_locals(source):
# This will raise if the source file can't be parsed
module = ast.parse(source, 'test_rename_locals')
add_parent(module)
add_namespace(module)
bind_names(module)
resolve_names(module)
allow_rename_locals(module, rename_locals=True)
allow_rename_globals(module, rename_globals=False)
rename(module)
return module
def assert_code(expected_ast, actual_ast):
try:
compare_ast(expected_ast, actual_ast)
except CompareError as e:
print(e)
print(unparse(actual_ast))
raise
def test_rename_variable_single_scope():
source = '''
def a():
a = 2
'''
expected = '''
def a():
A = 2
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_multiple_definitions():
source = '''
def inner(my_name):
import collections as my_name
def my_name(): pass
my_name = 12
'''
expected = '''
def inner(my_name):
A=my_name
import collections as A
def A(): pass
A = 12
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_self_cls_in_place():
source = '''
class TestClass():
def mymethod(self): return self
def mymethod2(qwertyuiop): return qwertyuiop
def mymethod3(): return 'No Self'
def mymethod4(self, a): return self, a
@staticmethod
def mystatic(cls): return cls
@staticmethod
def mystatic(asdfghjkl): return asdfghjkl
@staticmethod
def mystatic(): return 'No cls'
@classmethod
def mystatic(cls): return cls
@classmethod
def mystatic(asdfghjkl): return asdfghjkl
@classmethod
def mystatic(): return 'No cls'
@unknown_decorator
def unknown(self): return self
@unknown_decorator
def unknown(qwertyuiop): return qwertyuiop
@unknown_decorator
def unknown(self, arg): return self, arg
@unknown_decorator
def unknown(): return 'No arg'
'''
expected = '''
class TestClass():
def mymethod(A): return A
def mymethod2(A): return A
def mymethod3(): return 'No Self'
def mymethod4(A, a): return A, a
@staticmethod
def mystatic(cls): return cls
@staticmethod
def mystatic(asdfghjkl): return asdfghjkl
@staticmethod
def mystatic(): return 'No cls'
@classmethod
def mystatic(A): return A
@classmethod
def mystatic(A): return A
@classmethod
def mystatic(): return 'No cls'
@unknown_decorator
def unknown(self): return self
@unknown_decorator
def unknown(qwertyuiop): return qwertyuiop
@unknown_decorator
def unknown(self, arg): return self, arg
@unknown_decorator
def unknown(): return 'No arg'
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_arg_kwargs_in_place():
source = '''
def test(arg, arg2, *args, **kwargs): return args, kwargs
def samename(arg, arg2, *asd, **asd): return asd
class TestClass():
def mymethod(self, arg, *args, **kwargs): return args, kwargs
@classmethod
def mymethod(cls, arg, *args, **kwargs): return args, kwargs
'''
expected = '''
def test(arg, arg2, *A, **B): return A, B
def samename(arg, arg2, *A, **A): return A
class TestClass():
def mymethod(C, arg, *A, **B): return A, B
@classmethod
def mymethod(C, arg, *A, **B): return A, B
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_posargs_in_place():
if sys.version_info < (3, 8):
pytest.skip('No posonlyargs in python < 3.8')
source = '''
def test(arg, /, arg2): return arg, arg2
class TestClass():
def mymethod(self, arg, /, arg2): return arg, arg2
@classmethod
def mymethod(cls, arg, /, arg2): return arg, arg2
'''
expected = '''
def test(A,/,arg2):return A,arg2
class TestClass:
def mymethod(B,A,/,arg2):return A,arg2
@classmethod
def mymethod(B,A,/,arg2):return A,arg2
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_kwargonlyargs_in_place():
if sys.version_info < (3, 0):
pytest.skip('No kwonlyargs in python < 3.0')
source = '''
def test(arg, arg2, *, arg3): return arg, arg2, arg3
class TestClass():
def mymethod(self, arg, arg2, *, arg3): return arg, arg2, arg3
@classmethod
def mymethod(cls, arg, arg2, *, arg3): return arg, arg2, arg3
'''
expected = '''
def test(arg,arg2,*,arg3):return arg,arg2,arg3
class TestClass:
def mymethod(A,arg,arg2,*,arg3):return arg,arg2,arg3
@classmethod
def mymethod(A,arg,arg2,*,arg3):return arg,arg2,arg3
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_posargs_kwargonlyargs_in_place():
if sys.version_info < (3, 8):
pytest.skip('No posonlyargs in python < 3.8')
source = '''
def test(arg, /, arg2, *, arg3): return arg, arg2, arg3
class TestClass():
def mymethod(self, arg, /, arg2, *, arg3): return arg, arg2, arg3
@classmethod
def mymethod(cls, arg, /, arg2, *, arg3): return arg, arg2, arg3
'''
expected = '''
def test(A,/,arg2,*,arg3):return A,arg2,arg3
class TestClass:
def mymethod(B,A,/,arg2,*,arg3):return A,arg2,arg3
@classmethod
def mymethod(B,A,/,arg2,*,arg3):return A,arg2,arg3
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_no_rename_long_arg():
source = '''
def f(this_is_my_long_argument_name):
print(this_is_my_long_argument_name)
'''
expected_ast = ast.parse(source)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_long_arg():
source = '''
def f(this_is_my_long_argument_name):
print(this_is_my_long_argument_name)
print(this_is_my_long_argument_name)
'''
expected = '''
def f(this_is_my_long_argument_name):
A = this_is_my_long_argument_name
print(A)
print(A)
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_no_rename_single_char_arg():
source = '''
def f(a):
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
print(a)
'''
expected_ast = ast.parse(source)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_arg():
source = '''
def f(aa):
print(aa)
print(aa)
print(aa)
print(aa)
print(aa)
'''
expected = '''
def f(aa):
A = aa
print(A)
print(A)
print(A)
print(A)
print(A)
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_no_rename_lambda_arg():
source = '''
lambda my_argument: f(my_argument + my_argument + my_argument)
'''
expected_ast = ast.parse(source)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_rename_lambda_stararg():
source = '''
lambda *args, **kwargs: f(args, kwargs)
'''
expected = '''
lambda *A, **B: f(A, B)
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_python3_listcomp_scope():
if sys.version_info < (3, 0):
pytest.skip('No list comprehension scope in python < 3.0')
source = '''
[a for a in mylist]
'''
expected = '''
[A for A in mylist]
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_python2_listcomp_scope():
if sys.version_info >= (3, 0):
pytest.skip('list comprehension scope in python >= 3.0')
source = '''
[a for a in mylist]
def t():
a = True
[a for a in mylist]
'''
expected = '''
[a for a in mylist]
def t():
A = True
[A for A in mylist]
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_arg_rename():
source = '''
def f(*B,**A):pass
'''
expected = '''
def f(*A,**B):pass
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)
def test_multiple_args():
source = '''
def f(hello, hello):
print(hello + hello)
'''
expected = '''
def f(hello, hello):
A=hello
print(A + A)
'''
expected_ast = ast.parse(expected)
actual_ast = rename_locals(source)
assert_code(expected_ast, actual_ast)