forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
370 lines (314 loc) · 11.7 KB
/
stats.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
"""Utilities for calculating and reporting statistics about types."""
import cgi
import os.path
import re
from typing import Any, Dict, List, cast, Tuple
from mypy.traverser import TraverserVisitor
from mypy.types import (
Type, AnyType, Instance, FunctionLike, TupleType, Void, TypeVarType,
TypeQuery, ANY_TYPE_STRATEGY, CallableType
)
from mypy import nodes
from mypy.nodes import (
Node, FuncDef, TypeApplication, AssignmentStmt, NameExpr, CallExpr,
MemberExpr, OpExpr, ComparisonExpr, IndexExpr, UnaryExpr, YieldFromExpr
)
TYPE_EMPTY = 0
TYPE_PRECISE = 1
TYPE_IMPRECISE = 2
TYPE_ANY = 3
precision_names = [
'empty',
'precise',
'imprecise',
'any',
]
class StatisticsVisitor(TraverserVisitor):
def __init__(self, inferred: bool, typemap: Dict[Node, Type] = None,
all_nodes: bool = False) -> None:
self.inferred = inferred
self.typemap = typemap
self.all_nodes = all_nodes
self.num_precise = 0
self.num_imprecise = 0
self.num_any = 0
self.num_simple = 0
self.num_generic = 0
self.num_tuple = 0
self.num_function = 0
self.num_typevar = 0
self.num_complex = 0
self.line = -1
self.line_map = {} # type: Dict[int, int]
self.output = [] # type: List[str]
TraverserVisitor.__init__(self)
def visit_func_def(self, o: FuncDef) -> None:
self.line = o.line
if len(o.expanded) > 1:
if o in o.expanded:
print('ERROR: cycle in function expansion; skipping')
return
for defn in o.expanded:
self.visit_func_def(cast(FuncDef, defn))
else:
if o.type:
sig = cast(CallableType, o.type)
arg_types = sig.arg_types
if (sig.arg_names and sig.arg_names[0] == 'self' and
not self.inferred):
arg_types = arg_types[1:]
for arg in arg_types:
self.type(arg)
self.type(sig.ret_type)
elif self.all_nodes:
self.record_line(self.line, TYPE_ANY)
super().visit_func_def(o)
def visit_type_application(self, o: TypeApplication) -> None:
self.line = o.line
for t in o.types:
self.type(t)
super().visit_type_application(o)
def visit_assignment_stmt(self, o: AssignmentStmt) -> None:
self.line = o.line
if (isinstance(o.rvalue, nodes.CallExpr) and
isinstance(o.rvalue.analyzed, nodes.TypeVarExpr)):
# Type variable definition -- not a real assignment.
return
if o.type:
self.type(o.type)
elif self.inferred:
for lvalue in o.lvalues:
if isinstance(lvalue, nodes.TupleExpr):
items = lvalue.items
elif isinstance(lvalue, nodes.ListExpr):
items = lvalue.items
else:
items = [lvalue]
for item in items:
if hasattr(item, 'is_def') and cast(Any, item).is_def:
t = self.typemap.get(item)
if t:
self.type(t)
else:
self.log(' !! No inferred type on line %d' %
self.line)
self.record_line(self.line, TYPE_ANY)
super().visit_assignment_stmt(o)
def visit_name_expr(self, o: NameExpr) -> None:
self.process_node(o)
super().visit_name_expr(o)
def visit_yield_from_expr(self, o: YieldFromExpr) -> None:
if o.expr:
o.expr.accept(self)
def visit_call_expr(self, o: CallExpr) -> None:
self.process_node(o)
if o.analyzed:
o.analyzed.accept(self)
else:
o.callee.accept(self)
for a in o.args:
a.accept(self)
def visit_member_expr(self, o: MemberExpr) -> None:
self.process_node(o)
super().visit_member_expr(o)
def visit_op_expr(self, o: OpExpr) -> None:
self.process_node(o)
super().visit_op_expr(o)
def visit_comparison_expr(self, o: ComparisonExpr) -> None:
self.process_node(o)
super().visit_comparison_expr(o)
def visit_index_expr(self, o: IndexExpr) -> None:
self.process_node(o)
super().visit_index_expr(o)
def visit_unary_expr(self, o: UnaryExpr) -> None:
self.process_node(o)
super().visit_unary_expr(o)
def process_node(self, node: Node) -> None:
if self.all_nodes:
typ = self.typemap.get(node)
if typ:
self.line = node.line
self.type(typ)
def type(self, t: Type) -> None:
if isinstance(t, AnyType):
self.log(' !! Any type around line %d' % self.line)
self.num_any += 1
self.record_line(self.line, TYPE_ANY)
elif ((not self.all_nodes and is_imprecise(t)) or
(self.all_nodes and is_imprecise2(t))):
self.log(' !! Imprecise type around line %d' % self.line)
self.num_imprecise += 1
self.record_line(self.line, TYPE_IMPRECISE)
else:
self.num_precise += 1
self.record_line(self.line, TYPE_PRECISE)
if isinstance(t, Instance):
if t.args:
if any(is_complex(arg) for arg in t.args):
self.num_complex += 1
else:
self.num_generic += 1
else:
self.num_simple += 1
elif isinstance(t, Void):
self.num_simple += 1
elif isinstance(t, FunctionLike):
self.num_function += 1
elif isinstance(t, TupleType):
if any(is_complex(item) for item in t.items):
self.num_complex += 1
else:
self.num_tuple += 1
elif isinstance(t, TypeVarType):
self.num_typevar += 1
def log(self, string: str) -> None:
self.output.append(string)
def record_line(self, line: int, precision: int) -> None:
self.line_map[line] = max(precision,
self.line_map.get(line, TYPE_PRECISE))
def dump_type_stats(tree: Node, path: str, inferred: bool = False,
typemap: Dict[Node, Type] = None) -> None:
if is_special_module(path):
return
print(path)
visitor = StatisticsVisitor(inferred, typemap)
tree.accept(visitor)
for line in visitor.output:
print(line)
print(' ** precision **')
print(' precise ', visitor.num_precise)
print(' imprecise', visitor.num_imprecise)
print(' any ', visitor.num_any)
print(' ** kinds **')
print(' simple ', visitor.num_simple)
print(' generic ', visitor.num_generic)
print(' function ', visitor.num_function)
print(' tuple ', visitor.num_tuple)
print(' TypeVar ', visitor.num_typevar)
print(' complex ', visitor.num_complex)
print(' any ', visitor.num_any)
def is_special_module(path: str) -> bool:
return os.path.basename(path) in ('abc.pyi', 'typing.pyi', 'builtins.pyi')
def is_imprecise(t: Type) -> bool:
return t.accept(HasAnyQuery())
class HasAnyQuery(TypeQuery):
def __init__(self) -> None:
super().__init__(False, ANY_TYPE_STRATEGY)
def visit_any(self, t: AnyType) -> bool:
return True
def visit_instance(self, t: Instance) -> bool:
if t.type.fullname() == 'builtins.tuple':
return True
else:
return super().visit_instance(t)
def is_imprecise2(t: Type) -> bool:
return t.accept(HasAnyQuery2())
class HasAnyQuery2(HasAnyQuery):
def visit_callable_type(self, t: CallableType) -> bool:
# We don't want to flag references to functions with some Any
# argument types (etc.) since they generally don't mean trouble.
return False
def is_generic(t: Type) -> bool:
return isinstance(t, Instance) and bool(t.args)
def is_complex(t: Type) -> bool:
return is_generic(t) or isinstance(t, (FunctionLike, TupleType,
TypeVarType))
html_files = [] # type: List[Tuple[str, str, int, int]]
def generate_html_report(tree: Node, path: str, type_map: Dict[Node, Type],
output_dir: str) -> None:
if is_special_module(path):
return
# There may be more than one right answer for "what should we do here?"
# but this is a reasonable one.
path = os.path.relpath(path)
if path.startswith('..'):
return
visitor = StatisticsVisitor(inferred=True, typemap=type_map, all_nodes=True)
tree.accept(visitor)
assert not os.path.isabs(path) and not path.startswith('..')
# This line is *wrong* if the preceding assert fails.
target_path = os.path.join(output_dir, 'html', path)
# replace .py or .pyi with .html
target_path = os.path.splitext(target_path)[0] + '.html'
assert target_path.endswith('.html')
ensure_dir_exists(os.path.dirname(target_path))
output = [] # type: List[str]
append = output.append
append('''\
<html>
<head>
<style>
.red { background-color: #faa; }
.yellow { background-color: #ffa; }
.white { }
.lineno { color: #999; }
</style>
</head>
<body>
<pre>''')
num_imprecise_lines = 0
num_lines = 0
with open(path) as input_file:
for i, line in enumerate(input_file):
lineno = i + 1
status = visitor.line_map.get(lineno, TYPE_PRECISE)
style_map = {TYPE_PRECISE: 'white',
TYPE_IMPRECISE: 'yellow',
TYPE_ANY: 'red'}
style = style_map[status]
append('<span class="lineno">%4d</span> ' % lineno +
'<span class="%s">%s</span>' % (style,
cgi.escape(line)))
if status != TYPE_PRECISE:
num_imprecise_lines += 1
if line.strip():
num_lines += 1
append('</pre>')
append('</body></html>')
with open(target_path, 'w') as output_file:
output_file.writelines(output)
target_path = target_path[len(output_dir) + 1:]
html_files.append((path, target_path, num_lines, num_imprecise_lines))
def generate_html_index(output_dir: str) -> None:
path = os.path.join(output_dir, 'index.html')
output = [] # type: List[str]
append = output.append
append('''\
<html>
<head>
<style>
body { font-family: courier; }
table { border-collapse: collapse; }
table tr td { border: 1px solid black; }
td { padding: 0.4em; }
.red { background-color: #faa; }
.yellow { background-color: #ffa; }
</style>
</head>
<body>''')
append('<h1>Mypy Type Check Coverage Report</h1>\n')
append('<table>\n')
for source_path, target_path, num_lines, num_imprecise in sorted(html_files):
if num_lines == 0:
continue
source_path = os.path.normpath(source_path)
# TODO: Windows paths.
if (source_path.startswith('stubs/') or
'/stubs/' in source_path):
continue
percent = 100.0 * num_imprecise / num_lines
style = ''
if percent >= 20:
style = 'class="red"'
elif percent >= 5:
style = 'class="yellow"'
append('<tr %s><td><a href="%s">%s</a><td>%.1f%% imprecise<td>%d LOC\n' % (
style, target_path, source_path, percent, num_lines))
append('</table>\n')
append('</body></html>')
with open(path, 'w') as file:
file.writelines(output)
print('Generated HTML report (old): %s' % os.path.abspath(path))
def ensure_dir_exists(dir: str) -> None:
if not os.path.exists(dir):
os.makedirs(dir)