summaryrefslogtreecommitdiff
path: root/check_checkers.py
blob: 42415f0fff132a52e4eee83f56c923369522ece8 (plain)
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
#!/usr/bin/env python3
import hashlib
import html
import json
import os
import re
import shutil
from subprocess import PIPE, Popen, check_output
import tempfile
import time
from typing import Dict, List, NamedTuple, TextIO, Tuple, TypedDict
from pathlib import Path

import mypy.api
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import PythonLexer
import tomli


Error = Tuple[str, int, str]
"""The filename, line number and the error message."""


class Checker:
    name: str
    url: str
    extra_args: Tuple[str, ...] = ()

    def run(self, path: str, typeshed_path: str) -> List[Error]:
        """
        Type checks the given path with the given options.
        """
        raise NotImplementedError()

    def version(self) -> str:
        """Returns the version of the checker."""
        raise NotImplementedError()


class Mypy(Checker):
    name = 'Mypy'
    url = 'https://github.com/python/mypy'

    # mypy cannot output JSON (https://github.com/python/mypy/issues/10816)
    # though there is a PR (https://github.com/python/mypy/pull/11396)
    # so we just use the regex from https://github.com/matangover/mypy-vscode/blob/48162f345c7f14b96f29976660100ae1dd49cc0a/src/mypy.ts
    _pattern = re.compile(
        r'^(?P<file>[^\n]+?):((?P<line>\d+):)?((?P<column>\d+):)? (?P<type>\w+): (?P<message>.*)$',
        re.MULTILINE,
    )

    @classmethod
    def run(cls, path: str, typeshed_path: str):
        cachedir = tempfile.mkdtemp(prefix='mypy-cache-')
        stdout, stderr, retcode = mypy.api.run(
            [
                # fmt: off
                '--cache-dir', cachedir,
                '--custom-typeshed-dir', typeshed_path,
                *cls.extra_args,
                # fmt: on
                '--',
                path,
            ]
        )
        shutil.rmtree(cachedir)
        return [
            (m.group('file'), m.group('line'), m.group('message'))
            for m in cls._pattern.finditer(stdout)
        ]

    @staticmethod
    def version():
        return mypy.api.run(['--version'])[0].split()[1].strip()


class MypyStrict(Mypy):
    extra_args = ('--strict',)


class Pytype(Checker):
    name = 'Pytype'
    url = 'https://github.com/google/pytype'

    # pytype supports CSV output only for pytype-single which however doesn't support multiple modules
    # (https://github.com/google/pytype/issues/92)
    _pattern = re.compile(
        r'^File "(?P<file>[^"]+?)", line (?P<line>\d+), in (?P<module>[^ ]+): (?P<message>.*) \[(?P<id>[^]]+)\]$',
        re.MULTILINE,
    )

    @classmethod
    def run(cls, path: str, typeshed_path: str):
        env = {'TYPESHED_HOME': typeshed_path, 'PATH': os.environ['PATH']}
        proc = Popen(
            ['pytype', *cls.extra_args, '--', path],
            stdout=PIPE,
            stderr=PIPE,
            encoding='utf-8',
            env=env,
        )
        stdout, stderr = proc.communicate()
        return [
            (m.group('file'), m.group('line'), m.group('message'))
            for m in cls._pattern.finditer(stdout)
        ]

    @staticmethod
    def version():
        return check_output(['pytype', '--version'], encoding='utf-8').strip()


class Pyright(Checker):
    name = 'Pyright'
    url = 'https://github.com/microsoft/pyright'

    @classmethod
    def run(cls, path: str, typeshed_path: str):
        proc = Popen(
            [
                'pyright',
                # fmt: off
                '--typeshed-path', typeshed_path,
                '--outputjson',
                *cls.extra_args,
                # fmt: on
                # pyright does not support --
                path,
            ],
            stdout=PIPE,
            stderr=PIPE,
            encoding='utf-8',
        )
        stdout, stderr = proc.communicate()
        return [
            (d['file'], d['range']['start']['line'] + 1, d['message'])
            for d in json.loads(stdout)['generalDiagnostics']
        ]

    @staticmethod
    def version():
        return (
            check_output(['pyright', '--version'], encoding='utf-8').split()[1].strip()
        )


class PyrightStrict(Pyright):
    extra_args = ('-p', 'pyrightstrict.json')


# We don't check pyre because it has a very slow startup time (5s) since it parses the whole typeshed.
# (see https://github.com/facebook/pyre-check/issues/592)


class Puzzle(TypedDict):
    checker_results: Dict[str, List[Error]]
    puzzle_hash: str


def run_checkers(checkers: List[Checker], puzzle: str, typeshed_path: str):
    results = {}
    for checker in checkers:
        start = time.time()
        results[checker.__class__.__name__] = checker.run(puzzle, typeshed_path)
        duration = time.time() - start
        print(checker, time.time() - start)
    return results


def run(
    checkers: List[Checker],
    puzzles: List[Path],
    default_typeshed: str,
    out: TextIO,
    cache: Dict[str, Puzzle],
    issues: Dict[str, Dict[str, str]],
):
    python_lexer = PythonLexer()
    html_formatter = HtmlFormatter(noclasses=True, linenos='table')
    out.write(
        "<meta charset=utf-8><title>Comparison of static type checkers for Python</title>"
    )
    out.write(
        '''<p>This page compares three static type checkers for Python.
    The <span class=unexpected>red</span> background indicates that the checker
    fails to detect a type error or reports a false positive.
    <a href=/>Back to start page</a>.</p>'''
    )

    out.write(
        '''<style>
    .unexpected {background: #ffd2d0}
    th {position: sticky; top: 0; background: #e8e8e8;}
    table {border-spacing: 0;}
    td:target {background: #ffffc3;}

    html {
        /* prevent sticky table header from overlapping table cell targeted via #anchor */
        scroll-padding-top: 60px;
    }
    </style>
    '''
    )

    out.write('<table border=1>')
    out.write('<tr><th>Input')
    for checker in checkers:
        out.write('<th>')
        out.write('<a href="{}">'.format(html.escape(checker.url)))
        out.write(checker.name)
        out.write('</a>')
        if checker.extra_args:
            out.write(' with ')
            out.write('<code>')
            for arg in checker.extra_args:
                if not arg.startswith('-'):
                    out.write('<a href="configs/{}">'.format(html.escape(arg)))
                out.write(html.escape(arg) + ' ')
                if not arg.startswith('-'):
                    out.write('</a>')
                    os.makedirs('dist/configs', exist_ok=True)
                    shutil.copyfile(arg, 'dist/configs/' + arg)
            out.write('</code>')
        out.write('<br>({})'.format(html.escape(checker.version())))
    out.write('</tr>')

    for puzzle in puzzles:
        print(puzzle)

        codes: Dict[str, bytes] = {}

        if puzzle.is_dir():
            for file in sorted(puzzle.iterdir()):
                with file.open('rb') as f:
                    codes[file.name] = f.read()
        else:
            with puzzle.open('rb') as f:
                codes[puzzle.name] = f.read()

        puzzle_hash = '-'.join(
            hashlib.sha256(code).hexdigest() for code in codes.values()
        )
        if str(puzzle) in cache and puzzle_hash == cache[str(puzzle)]['puzzle_hash']:
            checker_results = cache[str(puzzle)]['checker_results']
        else:
            checker_results = run_checkers(checkers, str(puzzle), default_typeshed)
            cache[str(puzzle)] = {
                'puzzle_hash': puzzle_hash,
                'checker_results': checker_results,
            }

        anchor = html.escape(puzzle.name).replace('.py', '').replace('_', '-')
        out.write('<tr><td id="{0}"><a href="#{0}">{0}</a><br>'.format(anchor))

        error_ok = False
        no_error_ok = True

        for fname, code in codes.items():
            code = code.decode('utf-8')
            if len(codes) > 1:
                out.write(html.escape(fname))
            out.write(highlight(code, python_lexer, html_formatter))

            if '# error' in code:
                error_ok = True
                no_error_ok = False
            elif '# maybe error' in code:
                error_ok = True

        results = [
            (checker, checker_results[checker.__class__.__name__])
            for checker in checkers
        ]

        while results:
            checker, errors = results.pop(0)
            expected = (errors and error_ok) or (not errors and no_error_ok)
            colspan = 1
            if results and (checker.name, errors) == (
                results[0][0].name,
                results[0][1],
            ):
                results.pop(0)
                colspan = 2
            out.write(
                '<td class="{}" colspan={}>'.format(
                    'ok' if expected else 'unexpected', colspan
                )
            )
            if errors:
                out.write('<ul>')
                for filename, line, message in errors:
                    out.write('<li>')
                    out.write(f'{line}: ' + html.escape(message).replace('\n', '<br>'))
                    out.write('</li>')
                out.write('</ul>')
            else:
                out.write('<center>no errors found')
            if not expected:
                checker_issues = issues.get(
                    checker.__class__.__name__.lower(),
                    issues.get(
                        checker.__class__.__name__.lower().replace('strict', ''), {}
                    ),
                )
                issue = checker_issues.get(
                    os.path.splitext(os.path.basename(puzzle))[0]
                )
                if issue:
                    out.write('<br>(')
                    out.write(issue)
                    out.write(')')
    out.write('</table>')


if __name__ == '__main__':
    # TODO: git clone typeshed if missing
    typeshed = os.path.abspath('typeshed')  # pytype requries an absolute path

    try:
        with open('cache.json') as f:
            cache = json.load(f)
    except FileNotFoundError:
        cache = {}

    with open('issues.toml', 'rb') as f:
        issues = tomli.load(f)

    with open('dist/checkers.html', 'w') as f:
        run(
            [Mypy(), MypyStrict(), Pytype(), Pyright(), PyrightStrict()],
            list(sorted(Path('puzzles/').iterdir())),
            typeshed,
            f,
            cache,
            issues,
        )

    with open('cache.json', 'w') as f:
        json.dump(cache, f)