-
Notifications
You must be signed in to change notification settings - Fork 183
/
test_convert_type_comments.py
481 lines (405 loc) · 13.6 KB
/
test_convert_type_comments.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import sys
from typing import Any
from libcst.codemod import CodemodTest
from libcst.codemod.commands.convert_type_comments import ConvertTypeComments
class TestConvertTypeCommentsBase(CodemodTest):
maxDiff = 1500
TRANSFORM = ConvertTypeComments
def assertCodemod39Plus(self, before: str, after: str, **kwargs: Any) -> None:
"""
Assert that the codemod works on Python 3.9+, and that we raise
a NotImplementedError on other Python versions.
"""
if (sys.version_info.major, sys.version_info.minor) < (3, 9):
with self.assertRaises(NotImplementedError):
super().assertCodemod(before, after, **kwargs)
else:
super().assertCodemod(before, after, **kwargs)
class TestConvertTypeComments_AssignForWith(TestConvertTypeCommentsBase):
def test_preserves_trailing_comment(self) -> None:
before = """
y = 5 # type: int # foo
"""
after = """
y: int = 5 # foo
"""
self.assertCodemod39Plus(before, after)
def test_convert_assignments(self) -> None:
before = """
y = 5 # type: int
z = ('this', 7) # type: typing.Tuple[str, int]
"""
after = """
y: int = 5
z: "typing.Tuple[str, int]" = ('this', 7)
"""
self.assertCodemod39Plus(before, after)
def test_convert_assignments_in_context(self) -> None:
"""
Also verify that our matching works regardless of spacing
"""
before = """
def foo():
z = ('this', 7) # type: typing.Tuple[str, int]
class C:
attr0 = 10# type: int
def __init__(self):
self.attr1 = True # type: bool
"""
after = """
def foo():
z: "typing.Tuple[str, int]" = ('this', 7)
class C:
attr0: int = 10
def __init__(self):
self.attr1: bool = True
"""
self.assertCodemod39Plus(before, after)
def test_multiple_elements_in_assign_lhs(self) -> None:
before = """
x, y = [], [] # type: List[int], List[str]
z, w = [], [] # type: (List[int], List[str])
a, b, *c = range(5) # type: float, float, List[float]
d, (e1, e2) = foo() # type: float, (int, str)
"""
after = """
x: "List[int]"
y: "List[str]"
x, y = [], []
z: "List[int]"
w: "List[str]"
z, w = [], []
a: float
b: float
c: "List[float]"
a, b, *c = range(5)
d: float
e1: int
e2: str
d, (e1, e2) = foo()
"""
self.assertCodemod39Plus(before, after)
def test_multiple_assignments(self) -> None:
before = """
x = y = z = 15 # type: int
a, b = c, d = 'this', 'that' # type: (str, str)
"""
after = """
x: int
y: int
z: int
x = y = z = 15
a: str
b: str
c: str
d: str
a, b = c, d = 'this', 'that'
"""
self.assertCodemod39Plus(before, after)
def test_semicolons_with_assignment(self) -> None:
"""
When we convert an Assign to an AnnAssign, preserve
semicolons. But if we have to add separate type declarations,
expand them.
"""
before = """
foo(); x = 12 # type: int
bar(); y, z = baz() # type: int, str
"""
after = """
foo(); x: int = 12
bar()
y: int
z: str
y, z = baz()
"""
self.assertCodemod39Plus(before, after)
def test_converting_for_statements(self) -> None:
before = """
# simple binding
for x in foo(): # type: int
pass
# nested binding
for (a, (b, c)) in bar(): # type: int, (str, float)
pass
"""
after = """
# simple binding
x: int
for x in foo():
pass
# nested binding
a: int
b: str
c: float
for (a, (b, c)) in bar():
pass
"""
self.assertCodemod39Plus(before, after)
def test_converting_with_statements(self) -> None:
before = """
# simple binding
with open('file') as f: # type: File
pass
# simple binding, with extra items
with foo(), open('file') as f, bar(): # type: File
pass
# nested binding
with bar() as (a, (b, c)): # type: int, (str, float)
pass
"""
after = """
# simple binding
f: "File"
with open('file') as f:
pass
# simple binding, with extra items
f: "File"
with foo(), open('file') as f, bar():
pass
# nested binding
a: int
b: str
c: float
with bar() as (a, (b, c)):
pass
"""
self.assertCodemod39Plus(before, after)
def test_no_change_when_type_comment_unused(self) -> None:
before = """
# type-ignores are not type comments
x = 10 # type: ignore
# a commented type comment (per PEP 484) is not a type comment
z = 15 # # type: int
# ignore unparseable type comments
var = "var" # type: this is not a python type!
# a type comment in an illegal location won't be used
print("hello") # type: None
# These examples are not PEP 484 compliant, and result in arity errors
a, b = 1, 2 # type: Tuple[int, int]
w = foo() # type: float, str
# Multiple assigns with mismatched LHS arities always result in arity
# errors, and we only codemod if each target is error-free
v = v0, v1 = (3, 5) # type: int, int
# Ignore for statements with arity mismatches
for x in []: # type: int, int
pass
# Ignore with statements with arity mismatches
with open('file') as (f0, f1): # type: File
pass
# Ignore with statements that have multiple item bindings
with open('file') as f0, open('file') as f1: # type: File
pass
# In cases where the entire statement cannot successfully be parsed
# with `type_comments=True` because of an invalid type comment, we
# skip it. Here, annotating the inner `pass` is illegal.
for x in []: # type: int
pass # type: None
"""
after = before
self.assertCodemod39Plus(before, after)
class TestConvertTypeComments_FunctionDef(TestConvertTypeCommentsBase):
"""
Some notes on our testing strategy: In order to avoid a combinatorial
explosion in test cases, we leverage some knowledge about the
implementation.
Here are the key ideas that allow us to write fewer cases:
- The logic for generating annotations is the same for all annotations,
and is well-covered by TestConvertTypeComments_AssignForWith, so we
can stick to just simple builtin types.
- The application of types is independent of where they came from.
- Type comment removal is indepenent of type application, other
than in the case where we give up entirely.
- The rules for which type gets used (existing annotation, inline comment,
or func type comment) is independent of the location of a parameter.
"""
def test_simple_function_type_comments(self) -> None:
before = """
def f0(x): # type: (...) -> None
pass
def f1(x): # type: (int) -> None
pass
def f2(x, /, y = 'y', *, z = 1.5):
# type: (int, str, float) -> None
pass
def f3(x, *args, y, **kwargs):
# type: (str, int, str, float) -> None
pass
def f4(x, *args, **kwargs):
# type: (str, *int, **float) -> None
pass
"""
after = """
def f0(x) -> None:
pass
def f1(x: int) -> None:
pass
def f2(x: int, /, y: str = 'y', *, z: float = 1.5) -> None:
pass
def f3(x: str, *args: int, y: str, **kwargs: float) -> None:
pass
def f4(x: str, *args: int, **kwargs: float) -> None:
pass
"""
self.assertCodemod39Plus(before, after)
def test_prioritization_order_for_type_application(self) -> None:
before = """
def f(
x: int, # type: str
y, # type: str
z
): # type: (float, float, float) -> None
pass
"""
after = """
def f(
x: int,
y: str,
z: float
) -> None:
pass
"""
self.assertCodemod39Plus(before, after)
def test_inlined_function_type_comments(self) -> None:
before = """
def f(
x, # not-a-type-comment
# also-not-a-type-comment
y = 42, # type: int
*args,
# type: technically-another-line-is-legal :o
z,
**kwargs, # type: str
): # not-a-type-comment
# also-not-a-type-comment
pass
"""
after = """
def f(
x, # not-a-type-comment
# also-not-a-type-comment
y: int = 42,
*args: "technically-another-line-is-legal :o",
z,
**kwargs: str,
): # not-a-type-comment
# also-not-a-type-comment
pass
"""
self.assertCodemod39Plus(before, after)
def test_method_transforms(self) -> None:
before = """
class A:
def __init__(self, thing): # type: (str) -> None
self.thing = thing
@classmethod
def make(cls): # type: () -> A
return cls("thing")
@staticmethod
def f(x, y): # type: (object, object) -> None
pass
def method0(
self,
other_thing,
): # type: (str) -> bool
return self.thing == other_thing
def method1(
self, # type: A
other_thing, # type: str
): # type: (int) -> bool
return self.thing == other_thing
def method2(
self,
other_thing,
): # type: (A, str) -> bool
return self.thing == other_thing
"""
after = """
class A:
def __init__(self, thing: str) -> None:
self.thing = thing
@classmethod
def make(cls) -> "A":
return cls("thing")
@staticmethod
def f(x: object, y: object) -> None:
pass
def method0(
self,
other_thing: str,
) -> bool:
return self.thing == other_thing
def method1(
self: "A",
other_thing: str,
) -> bool:
return self.thing == other_thing
def method2(
self: "A",
other_thing: str,
) -> bool:
return self.thing == other_thing
"""
self.assertCodemod39Plus(before, after)
def test_no_change_if_function_type_comments_unused(self) -> None:
before = """
# arity error in arguments
def f(x, y): # type: (int) -> float
pass
# unparseable function type
def f(x, y): # type: this is not a type!
pass
# In cases where the entire statement cannot successfully be parsed
# with `type_comments=True` because of an invalid type comment, we
# skip it. Here, annotating the inner `pass` is illegal.
def f(x, y): # type: (int, int) -> None
pass # type: None
"""
after = before
self.assertCodemod39Plus(before, after)
def test_do_not_traverse_lambda_Param(self) -> None:
"""
The Param node can happen not just in FunctionDef but also in
Lambda. Make sure this doesn't cause problems.
"""
before = """
@dataclass
class WrapsAFunction:
func: Callable
msg_gen: Callable = lambda self: f"calling {self.func.__name__}..."
"""
after = before
self.assertCodemod39Plus(before, after)
def test_no_quoting(self) -> None:
before = """
def f(x):
# type: (Foo) -> Foo
pass
w = x # type: Foo
y, z = x, x # type: (Foo, Foo)
return w
with get_context() as context: # type: Context
pass
for loop_var in the_iterable: # type: LoopType
pass
"""
after = """
def f(x: Foo) -> Foo:
pass
w: Foo = x
y: Foo
z: Foo
y, z = x, x
return w
context: Context
with get_context() as context:
pass
loop_var: LoopType
for loop_var in the_iterable:
pass
"""
self.assertCodemod39Plus(before, after, no_quote_annotations=True)