-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathautotyping.py
More file actions
683 lines (630 loc) · 24.7 KB
/
autotyping.py
File metadata and controls
683 lines (630 loc) · 24.7 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
import argparse
from dataclasses import dataclass, field
import enum
import json
from typing import Dict, List, Optional, Sequence, Set, Tuple, Type
from typing_extensions import TypedDict
import libcst
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.codemod.visitors import AddImportsVisitor
from libcst.metadata import CodePosition, CodeRange, PositionProvider
from autotyping.guess_type import guess_type_from_argname
_DEFAULT_POSITION = CodePosition(0, 0)
_DEFAULT_CODE_RANGE = CodeRange(_DEFAULT_POSITION, _DEFAULT_POSITION)
@dataclass
class NamedParam:
name: str
module: Optional[str]
type_name: str
@classmethod
def make(cls, input: str) -> "NamedParam":
name, type_path = input.split(":")
if "." in type_path:
module, type_name = type_path.rsplit(".", maxsplit=1)
else:
module = None
type_name = type_path
return NamedParam(name, module, type_name)
class PyanalyzeSuggestion(TypedDict):
suggested_type: str
imports: List[str]
class DecoratorKind(enum.Enum):
asynq = 1
abstractmethod = 2
@dataclass
class State:
annotate_optionals: List[NamedParam]
annotate_named_params: List[NamedParam]
annotate_magics: bool
annotate_imprecise_magics: bool
none_return: bool
scalar_return: bool
param_types: Set[Type[object]]
seen_return_statement: List[bool] = field(default_factory=lambda: [False])
seen_return_types: List[Set[Optional[Type[object]]]] = field(
default_factory=lambda: [set()]
)
seen_raise_statement: List[bool] = field(default_factory=lambda: [False])
seen_yield: List[bool] = field(default_factory=lambda: [False])
in_lambda: bool = False
pyanalyze_suggestions: Dict[Tuple[str, int, int], PyanalyzeSuggestion] = field(
default_factory=dict
)
only_without_imports: bool = False
guess_common_names: bool = False
SIMPLE_MAGICS = {
"__str__": "str",
"__repr__": "str",
"__len__": "int",
"__length_hint__": "int",
"__init__": "None",
"__del__": "None",
"__bool__": "bool",
"__bytes__": "bytes",
"__format__": "str",
"__contains__": "bool",
"__complex__": "complex",
"__int__": "int",
"__float__": "float",
"__index__": "int",
"__setattr__": "None",
"__delattr__": "None",
"__setitem__": "None",
"__delitem__": "None",
"__set__": "None",
"__instancecheck__": "bool",
"__subclasscheck__": "bool",
}
IMPRECISE_MAGICS = {
"__iter__": ("typing", "Iterator"),
"__reversed__": ("typing", "Iterator"),
"__await__": ("typing", "Iterator"),
}
class AutotypeCommand(VisitorBasedCodemodCommand):
# Add a description so that future codemodders can see what this does.
DESCRIPTION: str = "Automatically adds simple type annotations."
METADATA_DEPENDENCIES = (PositionProvider,)
state: State
@staticmethod
def add_args(arg_parser: argparse.ArgumentParser) -> None:
arg_parser.add_argument(
"--annotate-optional",
nargs="*",
help=(
"foo:bar.Baz annotates any argument named 'foo' with a default of None"
" as 'bar.Baz'"
),
)
arg_parser.add_argument(
"--annotate-named-param",
nargs="*",
help=(
"foo:bar.Baz annotates any argument named 'foo' with no default"
" as 'bar.Baz'"
),
)
arg_parser.add_argument(
"--none-return",
action="store_true",
default=False,
help="Automatically add None return types",
)
arg_parser.add_argument(
"--scalar-return",
action="store_true",
default=False,
help="Automatically add int, str, bytes, float, and bool return types",
)
arg_parser.add_argument(
"--bool-param",
action="store_true",
default=False,
help=(
"Automatically add bool annotation to parameters with a default of True"
" or False"
),
)
arg_parser.add_argument(
"--int-param",
action="store_true",
default=False,
help="Automatically add int annotation to parameters with an int default",
)
arg_parser.add_argument(
"--float-param",
action="store_true",
default=False,
help=(
"Automatically add float annotation to parameters with a float default"
),
)
arg_parser.add_argument(
"--str-param",
action="store_true",
default=False,
help="Automatically add str annotation to parameters with a str default",
)
arg_parser.add_argument(
"--bytes-param",
action="store_true",
default=False,
help=(
"Automatically add bytes annotation to parameters with a bytes default"
),
)
arg_parser.add_argument(
"--annotate-magics",
action="store_true",
default=False,
help="Add annotations to certain magic methods (e.g., __str__)",
)
arg_parser.add_argument(
"--annotate-imprecise-magics",
action="store_true",
default=False,
help=(
"Add annotations to magic methods that are less precise (e.g., Iterable"
" for __iter__)"
),
)
arg_parser.add_argument(
"--guess-common-names",
action="store_true",
default=False,
help="Guess types from argument name",
)
arg_parser.add_argument(
"--pyanalyze-report",
type=str,
default=None,
help="Path to a pyanalyze --json-report file to use for suggested types.",
)
arg_parser.add_argument(
"--only-without-imports",
action="store_true",
default=False,
help="Only apply pyanalyze suggestions that do not require imports",
)
arg_parser.add_argument(
"--safe",
action="store_true",
default=False,
help="Apply all safe transformations",
)
arg_parser.add_argument(
"--aggressive",
action="store_true",
default=False,
help="Apply all transformations that do not require arguments",
)
def __init__(
self,
context: CodemodContext,
*,
annotate_optional: Optional[Sequence[str]] = None,
annotate_named_param: Optional[Sequence[str]] = None,
annotate_magics: bool = False,
annotate_imprecise_magics: bool = False,
none_return: bool = False,
scalar_return: bool = False,
bool_param: bool = False,
str_param: bool = False,
bytes_param: bool = False,
float_param: bool = False,
int_param: bool = False,
guess_common_names: bool = False,
pyanalyze_report: Optional[str] = None,
only_without_imports: bool = False,
safe: bool = False,
aggressive: bool = False,
) -> None:
if safe or aggressive:
none_return = True
scalar_return = True
annotate_magics = True
if aggressive:
bool_param = True
int_param = True
float_param = True
str_param = True
bytes_param = True
annotate_imprecise_magics = True
guess_common_names = True
super().__init__(context)
param_type_pairs = [
(bool_param, bool),
(str_param, str),
(bytes_param, bytes),
(int_param, int),
(float_param, float),
]
pyanalyze_suggestions = {}
if pyanalyze_report is not None:
with open(pyanalyze_report) as f:
data = json.load(f)
for failure in data:
if "lineno" not in failure or "col_offset" not in failure:
continue
metadata = failure.get("extra_metadata")
if not metadata:
continue
if "suggested_type" not in metadata or "imports" not in metadata:
continue
if failure.get("code") not in (
"suggested_parameter_type",
"suggested_return_type",
):
continue
pyanalyze_suggestions[
(
failure["absolute_filename"],
failure["lineno"],
failure["col_offset"],
)
] = metadata
self.state = State(
annotate_optionals=(
[NamedParam.make(s) for s in annotate_optional]
if annotate_optional
else []
),
annotate_named_params=(
[NamedParam.make(s) for s in annotate_named_param]
if annotate_named_param
else []
),
none_return=none_return,
scalar_return=scalar_return,
param_types={typ for param, typ in param_type_pairs if param},
annotate_magics=annotate_magics,
annotate_imprecise_magics=annotate_imprecise_magics,
pyanalyze_suggestions=pyanalyze_suggestions,
only_without_imports=only_without_imports,
guess_common_names=guess_common_names,
)
def is_stub(self) -> bool:
filename = self.context.filename
return filename is not None and filename.endswith(".pyi")
def visit_FunctionDef(self, node: libcst.FunctionDef) -> None:
self.state.seen_return_statement.append(False)
self.state.seen_raise_statement.append(False)
self.state.seen_yield.append(False)
self.state.seen_return_types.append(set())
def visit_Return(self, node: libcst.Return) -> None:
if node.value is not None:
self.state.seen_return_statement[-1] = True
self.state.seen_return_types[-1].add(type_of_expression(node.value))
else:
self.state.seen_return_types[-1].add(None)
def visit_Raise(self, node: libcst.Raise) -> None:
self.state.seen_raise_statement[-1] = True
def visit_Yield(self, node: libcst.Yield) -> None:
self.state.seen_yield[-1] = True
def visit_Lambda(self, node: libcst.Lambda) -> None:
self.state.in_lambda = True
def leave_Lambda(
self, original_node: libcst.Lambda, updated_node: libcst.Lambda
) -> libcst.CSTNode:
self.state.in_lambda = False
return updated_node
def leave_FunctionDef(
self, original_node: libcst.FunctionDef, updated_node: libcst.FunctionDef
) -> libcst.CSTNode:
kinds = {get_decorator_kind(decorator) for decorator in updated_node.decorators}
is_asynq = DecoratorKind.asynq in kinds
is_abstractmethod = DecoratorKind.abstractmethod in kinds
seen_return = self.state.seen_return_statement.pop()
seen_raise = self.state.seen_raise_statement.pop()
seen_yield = self.state.seen_yield.pop()
return_types = self.state.seen_return_types.pop()
name = original_node.name.value
if self.state.annotate_magics and name in ("__exit__", "__aexit__"):
updated_node = self.annotate_exit(updated_node)
if original_node.returns is not None:
return updated_node
if self.state.pyanalyze_suggestions and self.context.filename:
# Currently pyanalyze gives the lineno of the first decorator
# and libcst that of the def.
# TODO I think the AST behavior changed in later Python versions.
if original_node.decorators:
lineno_node = original_node.decorators[0]
else:
lineno_node = original_node
pos = self.get_metadata(
PositionProvider, lineno_node, _DEFAULT_CODE_RANGE
).start
key = (self.context.filename, pos.line, pos.column)
suggestion = self.state.pyanalyze_suggestions.get(key)
if suggestion is not None and not (
suggestion["imports"] and self.state.only_without_imports
):
for import_line in suggestion["imports"]:
if "." not in import_line:
AddImportsVisitor.add_needed_import(self.context, import_line)
else:
mod, name = import_line.rsplit(".", maxsplit=1)
AddImportsVisitor.add_needed_import(self.context, mod, name)
annotation = libcst.Annotation(
annotation=libcst.parse_expression(suggestion["suggested_type"])
)
return updated_node.with_changes(returns=annotation)
if self.state.annotate_magics:
if name in SIMPLE_MAGICS:
return updated_node.with_changes(
returns=libcst.Annotation(
annotation=libcst.Name(value=SIMPLE_MAGICS[name])
)
)
if self.state.annotate_imprecise_magics:
if name in IMPRECISE_MAGICS:
module, imported_name = IMPRECISE_MAGICS[name]
AddImportsVisitor.add_needed_import(self.context, module, imported_name)
return updated_node.with_changes(
returns=libcst.Annotation(
annotation=libcst.Name(value=imported_name)
)
)
if (
self.state.none_return
and not seen_raise
and not seen_return
and (is_asynq or not seen_yield)
and not is_abstractmethod
and not self.is_stub()
):
return updated_node.with_changes(
returns=libcst.Annotation(annotation=libcst.Name(value="None"))
)
if (
self.state.scalar_return
and (is_asynq or not seen_yield)
and len(return_types) == 1
):
return_type = next(iter(return_types))
if return_type in {bool, int, float, str, bytes}:
return updated_node.with_changes(
returns=libcst.Annotation(
annotation=libcst.Name(value=return_type.__name__)
)
)
return updated_node
def annotate_exit(self, node: libcst.FunctionDef) -> libcst.FunctionDef:
if (
node.params.star_arg is not libcst.MaybeSentinel.DEFAULT
or node.params.kwonly_params
or node.params.star_kwarg
):
return node
# 4 for def __exit__(self, type, value, tb)
if len(node.params.params) == 4:
params = node.params.params
is_pos_only = False
elif len(node.params.posonly_params) == 4:
params = node.params.posonly_params
is_pos_only = True
else:
return node
new_params = [params[0]]
# type
if params[1].annotation:
new_params.append(params[1])
else:
AddImportsVisitor.add_needed_import(self.context, "typing", "Optional")
AddImportsVisitor.add_needed_import(self.context, "typing", "Type")
type_anno = libcst.Subscript(
value=libcst.Name(value="Type"),
slice=[
libcst.SubscriptElement(
slice=libcst.Index(value=libcst.Name(value="BaseException"))
)
],
)
anno = libcst.Subscript(
value=libcst.Name(value="Optional"),
slice=[libcst.SubscriptElement(slice=libcst.Index(value=type_anno))],
)
new_params.append(
params[1].with_changes(annotation=libcst.Annotation(annotation=anno))
)
# value
if params[2].annotation:
new_params.append(params[2])
else:
AddImportsVisitor.add_needed_import(self.context, "typing", "Optional")
anno = libcst.Subscript(
value=libcst.Name(value="Optional"),
slice=[
libcst.SubscriptElement(
slice=libcst.Index(value=libcst.Name(value="BaseException"))
)
],
)
new_params.append(
params[2].with_changes(annotation=libcst.Annotation(annotation=anno))
)
# tb
if params[3].annotation:
new_params.append(params[3])
else:
AddImportsVisitor.add_needed_import(self.context, "types", "TracebackType")
anno = libcst.Subscript(
value=libcst.Name(value="Optional"),
slice=[
libcst.SubscriptElement(
slice=libcst.Index(value=libcst.Name(value="TracebackType"))
)
],
)
new_params.append(
params[3].with_changes(annotation=libcst.Annotation(annotation=anno))
)
if is_pos_only:
new_parameters = node.params.with_changes(posonly_params=new_params)
else:
new_parameters = node.params.with_changes(params=new_params)
return node.with_changes(params=new_parameters)
def leave_Param(
self, original_node: libcst.Param, updated_node: libcst.Param
) -> libcst.CSTNode:
if self.state.in_lambda:
# Lambdas can't have annotations
return updated_node
# don't modify if there's already any annotations set
if original_node.annotation is not None:
return updated_node
# pyanalyze suggestions
if self.state.pyanalyze_suggestions and self.context.filename:
pos = self.get_metadata(
PositionProvider, original_node, _DEFAULT_CODE_RANGE
).start
key = (self.context.filename, pos.line, pos.column)
suggestion = self.state.pyanalyze_suggestions.get(key)
if suggestion is not None and not (
suggestion["imports"] and self.state.only_without_imports
):
for import_line in suggestion["imports"]:
if "." not in import_line:
AddImportsVisitor.add_needed_import(self.context, import_line)
else:
mod, name = import_line.rsplit(".", maxsplit=1)
AddImportsVisitor.add_needed_import(self.context, mod, name)
annotation = libcst.Annotation(
annotation=libcst.parse_expression(suggestion["suggested_type"])
)
return updated_node.with_changes(annotation=annotation)
# infer from default non-None value
if original_node.default is not None:
default_type = type_of_expression(original_node.default)
if default_type is not None and default_type in self.state.param_types:
return updated_node.with_changes(
annotation=libcst.Annotation(
annotation=libcst.Name(value=default_type.__name__)
)
)
parameter_name = original_node.name.value
default_is_none = (
original_node.default is not None
and isinstance(original_node.default, libcst.Name)
and original_node.default.value == "None"
)
# default value is None, i.e. `def foo(bar=None)`
if default_is_none:
# check if user has explicitly specified a type for this name
for anno_optional in self.state.annotate_optionals:
if parameter_name == anno_optional.name:
return self._annotate_param(
anno_optional, updated_node, containers=["Optional"]
)
# no default value, i.e. `def foo(bar)`
elif original_node.default is None:
# check if user has explicitly specified a type for this name
for anno_named_param in self.state.annotate_named_params:
if original_node.name.value == anno_named_param.name:
return self._annotate_param(anno_named_param, updated_node, [])
# guess type from name
if self.state.guess_common_names:
guessed_type, containers = guess_type_from_argname(parameter_name)
if guessed_type is not None:
if default_is_none:
containers += ["Optional"]
return self._annotate_param(
NamedParam("", None, guessed_type), updated_node, containers
)
return updated_node
def _annotate_param(
self, param: NamedParam, updated_node: libcst.Param, containers: List[str]
) -> libcst.Param:
if param.module is not None:
AddImportsVisitor.add_needed_import(
self.context, param.module, param.type_name
)
anno = libcst.Name(value=param.type_name)
for container in containers:
# Should be updated when python <3.9 support is dropped
AddImportsVisitor.add_needed_import(self.context, "typing", container)
anno = libcst.Subscript(
value=libcst.Name(value=container),
slice=[libcst.SubscriptElement(slice=libcst.Index(value=anno))],
)
return updated_node.with_changes(annotation=libcst.Annotation(annotation=anno))
def type_of_expression(expr: libcst.BaseExpression) -> Optional[Type[object]]:
"""Very simple type inference for expressions.
Return None if the type cannot be inferred.
"""
if isinstance(expr, libcst.Float):
return float
elif isinstance(expr, libcst.Integer):
return int
elif isinstance(expr, libcst.Imaginary):
return complex
elif isinstance(expr, libcst.FormattedString):
return str # f-strings can only be str, not bytes
elif isinstance(expr, libcst.SimpleString):
if "b" in expr.prefix:
return bytes
else:
return str
elif isinstance(expr, libcst.ConcatenatedString):
left = type_of_expression(expr.left)
right = type_of_expression(expr.right)
if left == right:
return left
else:
return None
elif isinstance(expr, libcst.Name) and expr.value in ("True", "False"):
return bool
elif isinstance(expr, libcst.UnaryOperation) and isinstance(
expr.operator, libcst.Not
):
return bool
elif isinstance(expr, libcst.BinaryOperation):
left = type_of_expression(expr.left)
if left in (str, bytes) and isinstance(expr.operator, libcst.Modulo):
return left
return None
elif isinstance(expr, libcst.BooleanOperation):
left = type_of_expression(expr.left)
right = type_of_expression(expr.right)
# For AND and OR, if both types are the same, we can infer that type.
if left == right:
return left
else:
return None
elif isinstance(expr, libcst.Comparison):
types = {type(comp.operator) for comp in expr.comparisons}
# Only these are actually guaranteed to return bool
if types <= {libcst.In, libcst.Is, libcst.IsNot, libcst.NotIn}:
return bool
return None
elif (
isinstance(expr, libcst.Call)
and isinstance(expr.func, libcst.Attribute)
and isinstance(expr.func.value, libcst.BaseString)
and expr.func.attr.value in ("format", "lower", "upper", "title")
):
return str
else:
return None
def get_decorator_kind(dec: libcst.Decorator) -> Optional[DecoratorKind]:
"""Is this @asynq() or @abstractmethod?"""
if isinstance(dec.decorator, libcst.Call):
call = dec.decorator
if not isinstance(call.func, libcst.Name):
return None
if call.func.value != "asynq":
return None
if call.args:
# @asynq() with custom arguments may do something unexpected
return None
return DecoratorKind.asynq
elif isinstance(dec.decorator, libcst.Name):
if dec.decorator.value == "abstractmethod":
return DecoratorKind.abstractmethod
elif isinstance(dec.decorator, libcst.Attribute):
if (
dec.decorator.attr.value == "abstractmethod"
and isinstance(dec.decorator.value, libcst.Name)
and dec.decorator.value.value == "abc"
):
return DecoratorKind.abstractmethod
return None