File: pytype_test.py

package info (click to toggle)
mypy 0.470-complete-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,864 kB
  • ctags: 3,264
  • sloc: python: 21,838; makefile: 18
file content (109 lines) | stat: -rwxr-xr-x 2,941 bytes parent folder | download
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
#!/usr/bin/env python
"""Test runner for typeshed.

Depends on mypy and pytype being installed.

If pytype is installed:
    1. For every pyi, run "pytd <foo.pyi>" in a separate process
"""

import os
import re
import sys
import argparse
import subprocess
import collections

parser = argparse.ArgumentParser(description="Pytype tests.")
parser.add_argument('-n', '--dry-run', action='store_true', help="Don't actually run tests")
parser.add_argument('--num-parallel', type=int, default=1,
                    help="Number of test processes to spawn")


def main():
    args = parser.parse_args()
    code, runs = pytype_test(args)

    if code:
        print("--- exit status %d ---" % code)
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)


def load_blacklist():
    filename = os.path.join(os.path.dirname(__file__), "pytype_blacklist.txt")
    regex = r"^\s*([^\s#]+)\s*(?:#.*)?$"

    with open(filename) as f:
        return re.findall(regex, f.read(), flags=re.M)


class PytdRun(object):
    def __init__(self, args, dry_run=False):
        self.args = args
        self.dry_run = dry_run
        self.results = None

        if dry_run:
            self.results = (0, "", "")
        else:
            self.proc = subprocess.Popen(
                ["pytd"] + args,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE)

    def communicate(self):
        if self.results:
            return self.results

        stdout, stderr = self.proc.communicate()
        self.results = self.proc.returncode, stdout, stderr
        return self.results


def pytype_test(args):
    try:
        PytdRun(["-h"]).communicate()
    except OSError:
        print("Cannot run pytd. Did you install pytype?")
        return 0, 0

    wanted = re.compile(r"stdlib/(2|2\.7|2and3)/.*\.pyi$")
    skipped = re.compile("(%s)$" % "|".join(load_blacklist()))
    files = []

    for root, _, filenames in os.walk("stdlib"):
        for f in sorted(filenames):
            f = os.path.join(root, f)
            if wanted.search(f) and not skipped.search(f):
                files.append(f)

    running_tests = collections.deque()
    max_code, runs, errors = 0, 0, 0
    print("Running pytype tests...")
    while 1:
        while files and len(running_tests) < args.num_parallel:
            test_run = PytdRun([files.pop()], dry_run=args.dry_run)
            running_tests.append(test_run)

        if not running_tests:
            break

        test_run = running_tests.popleft()
        code, stdout, stderr = test_run.communicate()
        max_code = max(max_code, code)
        runs += 1

        if code:
            print("pytd error processing \"%s\":" % test_run.args[0])
            print(stderr)
            errors += 1

    print("Ran pytype with %d pyis, got %d errors." % (runs, errors))
    return max_code, runs


if __name__ == '__main__':
    main()