示例#1
0
def main():
    args = parser.parse_args()

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 6), (3, 5), (3, 4), (3, 3), (2, 7)]
    code = 0
    for major, minor in versions:
        flags = ['--python-version', '%d.%d' % (major, minor)]
        flags.append('--strict-optional')
        if args.no_implicit_optional:
            flags.append('--no-implicit-optional')
        if args.warn_unused_ignores:
            flags.append('--warn-unused-ignores')
        sys.argv = ['mypy'] + flags + ['sqlalchemy']
        if args.verbose:
            print("running", ' '.join(sys.argv))
        else:
            print("running mypy", ' '.join(flags))
        try:
            if not args.dry_run:
                mypy_main('')
        except SystemExit as err:
            code = max(code, err.code)
        if code:
            print("--- exit status", code, "---")
            sys.exit(code)
示例#2
0
def run_mypy(args,
             configurations,
             major,
             minor,
             files,
             *,
             custom_typeshed=False):
    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    with tempfile.NamedTemporaryFile("w+") as temp:
        temp.write("[mypy]\n")
        for dist_conf in configurations:
            temp.write("[mypy-%s]\n" % dist_conf.module_name)
            for k, v in dist_conf.values.items():
                temp.write("{} = {}\n".format(k, v))
        temp.flush()

        flags = get_mypy_flags(args,
                               major,
                               minor,
                               temp.name,
                               custom_typeshed=custom_typeshed)
        sys.argv = ["mypy"] + flags + files
        if args.verbose:
            print("running", " ".join(sys.argv))
        if not args.dry_run:
            try:
                mypy_main("", sys.stdout, sys.stderr)
            except SystemExit as err:
                return err.code
        return 0
示例#3
0
def run_mypy(args,
             configurations,
             major,
             minor,
             files,
             *,
             custom_typeshed=False):
    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    with tempfile.NamedTemporaryFile("w+") as temp:
        temp.write("[mypy]\n")
        for dist_conf in configurations:
            temp.write("[mypy-%s]\n" % dist_conf.module_name)
            for k, v in dist_conf.values.items():
                temp.write("{} = {}\n".format(k, v))
        temp.flush()

        flags = [
            "--python-version",
            "%d.%d" % (major, minor),
            "--config-file",
            temp.name,
            "--strict-optional",
            "--no-site-packages",
            "--show-traceback",
            "--no-implicit-optional",
            "--disallow-any-generics",
            "--disallow-subclassing-any",
            "--warn-incomplete-stub",
        ]
        if custom_typeshed:
            # Setting custom typeshed dir prevents mypy from falling back to its bundled
            # typeshed in case of stub deletions
            flags.extend([
                "--custom-typeshed-dir",
                os.path.dirname(os.path.dirname(__file__))
            ])
        if args.warn_unused_ignores:
            flags.append("--warn-unused-ignores")
        if args.platform:
            flags.extend(["--platform", args.platform])
        sys.argv = ["mypy"] + flags + files
        if args.verbose:
            print("running", " ".join(sys.argv))
        else:
            print("running mypy", " ".join(flags), "# with", len(files),
                  "files")
        if not args.dry_run:
            try:
                mypy_main("", sys.stdout, sys.stderr)
            except SystemExit as err:
                return err.code
        return 0
示例#4
0
    def test_type_hints(self):
        text_io = StringIO('')
        try:
            # mypy.main uses sys.stdout for printing
            # We override it to catch error messages
            mypy_main(None, text_io, text_io, [__file__])
        except SystemExit:
            # mypy.main could return errors found inside other files.
            # filter_errors() will filter out all errors outside this file.
            errors = text_io.getvalue().splitlines()
            errors = self.filter_errors(errors)
        else:
            errors = None

        # To prevent large errors raise error out of try/except
        if (errors):
            logging.error('\n'.join(errors))
            raise AssertionError("Type annotations check failed")
示例#5
0
    def run(self: MypyRunner) -> ToolResult:
        """Run mypy."""
        logger: TextIOLogger = cast(
            TextIOLogger, self.make_logger(TextIOLogger, logging.INFO))

        logger_as_textio: TextIO = cast(TextIO, logger)

        try:
            mypy_main(
                None,
                logger_as_textio,
                logger_as_textio,
                [str(p) for p in self.src_paths],
            )

            return ToolResult.SUCCESS

        except SystemExit as sysexit:
            return ToolResult.SUCCESS if sysexit.code == 0 else ToolResult.FAILURE
示例#6
0
文件: runtests.py 项目: tony/typeshed
def main():
    args = parser.parse_args()

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    files2 = []
    files3 = []
    for dir, subdirs, files in os.walk('.'):
        for file in files:
            if file == '__builtin__.pyi':
                continue  # Special case (alias for builtins.py).
            if file.endswith('.pyi') or file.endswith('.py'):
                full = os.path.join(dir, file)
                if match(args, full):
                    if '/2' in dir:
                        files2.append(full)
                    if '/3' in dir or '/2and3' in dir:
                        files3.append(full)
    if not (files2 or files3):
        print('--- nothing to do ---')
    code = 0
    for flags, files in [([], files3), (['--py2'], files2)]:
        if files:
            sys.argv = ['mypy'] + flags + files
            if args.verbose:
                print('running', ' '.join(sys.argv))
            else:
                print('running mypy', ' '.join(flags), '# with', len(files), 'files')
            try:
                if not args.dry_run:
                    mypy_main('')
            except SystemExit as err:
                code = max(code, err.code)
    if code:
        print('--- exit status', code, '---')
        sys.exit(code)
示例#7
0
def main():
    args = parser.parse_args()

    with open(os.path.join(os.path.dirname(__file__), "mypy_exclude_list.txt")) as f:
        exclude_list = re.compile("(%s)$" % "|".join(re.findall(r"^\s*([^\s#]+)\s*(?:#.*)?$", f.read(), flags=re.M)))

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 10), (3, 9), (3, 8), (3, 7), (3, 6), (2, 7)]
    if args.python_version:
        versions = [v for v in versions if any(("%d.%d" % v).startswith(av) for av in args.python_version)]
        if not versions:
            print("--- no versions selected ---")
            sys.exit(1)

    code = 0
    runs = 0
    for major, minor in versions:
        files = []
        seen = {"__builtin__", "builtins", "typing"}  # Always ignore these.
        configurations = []
        seen_dist_configs = set()

        # First add standard library files.
        if major == 2:
            root = os.path.join("stdlib", PY2_NAMESPACE)
            for name in os.listdir(root):
                mod, _ = os.path.splitext(name)
                if mod in seen or mod.startswith("."):
                    continue
                add_files(files, seen, root, name, args, exclude_list)
        else:
            supported_versions = parse_versions(os.path.join("stdlib", "VERSIONS"))
            root = "stdlib"
            for name in os.listdir(root):
                if name == PY2_NAMESPACE or name == "VERSIONS":
                    continue
                mod, _ = os.path.splitext(name)
                if supported_versions[mod][0] <= (major, minor) <= supported_versions[mod][1]:
                    add_files(files, seen, root, name, args, exclude_list)

        # Next add files for all third party distributions.
        for distribution in os.listdir(THIRD_PARTY_NAMESPACE):
            if not is_supported(distribution, major):
                continue
            if major == 2 and os.path.isdir(os.path.join(THIRD_PARTY_NAMESPACE, distribution, PY2_NAMESPACE)):
                root = os.path.join(THIRD_PARTY_NAMESPACE, distribution, PY2_NAMESPACE)
            else:
                root = os.path.join(THIRD_PARTY_NAMESPACE, distribution)
            for name in os.listdir(root):
                if name == PY2_NAMESPACE:
                    continue
                mod, _ = os.path.splitext(name)
                if mod in seen or mod.startswith("."):
                    continue
                add_files(files, seen, root, name, args, exclude_list)
                add_configuration(configurations, seen_dist_configs, distribution)

        if files:
            with tempfile.NamedTemporaryFile("w+", delete=False) as temp:
                temp.write("[mypy]\n")

                for dist_conf in configurations:
                    temp.write("[mypy-%s]\n" % dist_conf.module_name)
                    for k, v in dist_conf.values.items():
                        temp.write("{} = {}\n".format(k, v))

                config_file_name = temp.name
            runs += 1
            flags = [
                "--python-version", "%d.%d" % (major, minor),
                "--config-file", config_file_name,
                "--strict-optional",
                "--no-site-packages",
                "--show-traceback",
                "--no-implicit-optional",
                "--disallow-any-generics",
                "--disallow-subclassing-any",
                "--warn-incomplete-stub",
                # Setting custom typeshed dir prevents mypy from falling back to its bundled
                # typeshed in case of stub deletions
                "--custom-typeshed-dir", os.path.dirname(os.path.dirname(__file__)),
            ]
            if args.warn_unused_ignores:
                flags.append("--warn-unused-ignores")
            if args.platform:
                flags.extend(["--platform", args.platform])
            sys.argv = ["mypy"] + flags + files
            if args.verbose:
                print("running", " ".join(sys.argv))
            else:
                print("running mypy", " ".join(flags), "# with", len(files), "files")
            try:
                if not args.dry_run:
                    mypy_main("", sys.stdout, sys.stderr)
            except SystemExit as err:
                code = max(code, err.code)
            finally:
                os.remove(config_file_name)
    if code:
        print("--- exit status", code, "---")
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)
示例#8
0
def main():
    args = parser.parse_args()

    with open(os.path.join(os.path.dirname(__file__), "mypy_blacklist.txt")) as f:
        blacklist = re.compile("(%s)$" % "|".join(
            re.findall(r"^\s*([^\s#]+)\s*(?:#.*)?$", f.read(), flags=re.M)))

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 5), (3, 4), (3, 3), (3, 2), (2, 7)]
    if args.python_version:
        versions = [v for v in versions
                    if any(('%d.%d' % v).startswith(av) for av in args.python_version)]
        if not versions:
            print("--- no versions selected ---")
            sys.exit(1)

    code = 0
    runs = 0
    for major, minor in versions:
        roots = libpath(major, minor)
        files = []
        seen = {'__builtin__', 'builtins', 'typing'}  # Always ignore these.
        for root in roots:
            names = os.listdir(root)
            for name in names:
                full = os.path.join(root, name)
                mod, ext = os.path.splitext(name)
                if mod in seen or mod.startswith('.'):
                    continue
                if ext in ['.pyi', '.py']:
                    if match(full, args, blacklist):
                        seen.add(mod)
                        files.append(full)
                elif (os.path.isfile(os.path.join(full, '__init__.pyi')) or
                      os.path.isfile(os.path.join(full, '__init__.py'))):
                    for r, ds, fs in os.walk(full):
                        ds.sort()
                        fs.sort()
                        for f in fs:
                            m, x = os.path.splitext(f)
                            if x in ['.pyi', '.py']:
                                fn = os.path.join(r, f)
                                if match(fn, args, blacklist):
                                    seen.add(mod)
                                    files.append(fn)
        if files:
            runs += 1
            flags = ['--python-version', '%d.%d' % (major, minor)]
            flags.append('--strict-optional')
            ##flags.append('--fast-parser')  # Travis CI doesn't have typed_ast yet.
            ##flags.append('--warn-unused-ignores')  # Fast parser and regular parser disagree.
            sys.argv = ['mypy'] + flags + files
            if args.verbose:
                print("running", ' '.join(sys.argv))
            else:
                print("running mypy", ' '.join(flags), "# with", len(files), "files")
            try:
                if not args.dry_run:
                    mypy_main('')
            except SystemExit as err:
                code = max(code, err.code)
    if code:
        print("--- exit status", code, "---")
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)
示例#9
0
def main():
    args = parser.parse_args()

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 5), (3, 4), (3, 3), (3, 2), (2, 7)]
    if args.python_version:
        versions = [v for v in versions
                    if any(('%d.%d' % v).startswith(av) for av in args.python_version)]
        if not versions:
            print("--- no versions selected ---")
            sys.exit(1)

    code = 0
    runs = 0
    for major, minor in versions:
        roots = libpath(major, minor)
        files = []
        seen = {'__builtin__', 'builtins', 'typing'}  # Always ignore these.
        for root in roots:
            names = os.listdir(root)
            for name in names:
                full = os.path.join(root, name)
                mod, ext = os.path.splitext(name)
                if mod in seen:
                    continue
                if ext in ['.pyi', '.py']:
                    if match(args, full):
                        seen.add(mod)
                        files.append(full)
                elif (os.path.isfile(os.path.join(full, '__init__.pyi')) or
                      os.path.isfile(os.path.join(full, '__init__.py'))):
                    for r, ds, fs in os.walk(full):
                        ds.sort()
                        fs.sort()
                        for f in fs:
                            m, x = os.path.splitext(f)
                            if x in ['.pyi', '.py']:
                                fn = os.path.join(r, f)
                                if match(args, fn):
                                    seen.add(mod)
                                    files.append(fn)
        if files:
            runs += 1
            flags = ['--python-version', '%d.%d' % (major, minor)]
            flags.append('--strict-optional')
            sys.argv = ['mypy'] + flags + files
            if args.verbose:
                print("running", ' '.join(sys.argv))
            else:
                print("running mypy", ' '.join(flags), "# with", len(files), "files")
            try:
                if not args.dry_run:
                    mypy_main('')
            except SystemExit as err:
                code = max(code, err.code)
    if code:
        print("--- exit status", code, "---")
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)
示例#10
0
def main():
    args = parser.parse_args()

    with open(os.path.join(os.path.dirname(__file__),
                           "mypy_blacklist.txt")) as f:
        blacklist = re.compile("(%s)$" % "|".join(
            re.findall(r"^\s*([^\s#]+)\s*(?:#.*)?$", f.read(), flags=re.M)))

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 7), (3, 6), (3, 5), (3, 4), (2, 7)]
    if args.python_version:
        versions = [
            v for v in versions
            if any(('%d.%d' % v).startswith(av) for av in args.python_version)
        ]
        if not versions:
            print("--- no versions selected ---")
            sys.exit(1)

    code = 0
    runs = 0
    for major, minor in versions:
        roots = libpath(major, minor)
        files = []
        seen = {'__builtin__', 'builtins', 'typing'}  # Always ignore these.
        for root in roots:
            names = os.listdir(root)
            for name in names:
                full = os.path.join(root, name)
                mod, ext = os.path.splitext(name)
                if mod in seen or mod.startswith('.'):
                    continue
                if ext in ['.pyi', '.py']:
                    if match(full, args, blacklist):
                        seen.add(mod)
                        files.append(full)
                elif (os.path.isfile(os.path.join(full, '__init__.pyi'))
                      or os.path.isfile(os.path.join(full, '__init__.py'))):
                    for r, ds, fs in os.walk(full):
                        ds.sort()
                        fs.sort()
                        for f in fs:
                            m, x = os.path.splitext(f)
                            if x in ['.pyi', '.py']:
                                fn = os.path.join(r, f)
                                if match(fn, args, blacklist):
                                    seen.add(mod)
                                    files.append(fn)
        if files:
            runs += 1
            flags = ['--python-version', '%d.%d' % (major, minor)]
            flags.append('--strict-optional')
            flags.append('--no-site-packages')
            flags.append('--show-traceback')
            flags.append('--no-implicit-optional')
            if args.new_analyzer:
                flags.append('--new-semantic-analyzer')
            if args.warn_unused_ignores:
                flags.append('--warn-unused-ignores')
            sys.argv = ['mypy'] + flags + files
            if args.verbose:
                print("running", ' '.join(sys.argv))
            else:
                print("running mypy", ' '.join(flags), "# with", len(files),
                      "files")
            try:
                if not args.dry_run:
                    mypy_main('', sys.stdout, sys.stderr)
            except SystemExit as err:
                code = max(code, err.code)
    if code:
        print("--- exit status", code, "---")
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)
示例#11
0
def main():
    args = parser.parse_args()

    with open(os.path.join(os.path.dirname(__file__),
                           "mypy_exclude_list.txt")) as f:
        exclude_list = re.compile("(%s)$" % "|".join(
            re.findall(r"^\s*([^\s#]+)\s*(?:#.*)?$", f.read(), flags=re.M)))

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 9), (3, 8), (3, 7), (3, 6), (3, 5), (2, 7)]
    if args.python_version:
        versions = [
            v for v in versions
            if any(("%d.%d" % v).startswith(av) for av in args.python_version)
        ]
        if not versions:
            print("--- no versions selected ---")
            sys.exit(1)

    code = 0
    runs = 0
    for major, minor in versions:
        roots = libpath(major, minor)
        files = []
        seen = {"__builtin__", "builtins", "typing"}  # Always ignore these.
        for root in roots:
            names = os.listdir(root)
            for name in names:
                full = os.path.join(root, name)
                mod, ext = os.path.splitext(name)
                if mod in seen or mod.startswith("."):
                    continue
                if ext in [".pyi", ".py"]:
                    if match(full, args, exclude_list):
                        seen.add(mod)
                        files.append(full)
                elif os.path.isfile(os.path.join(
                        full, "__init__.pyi")) or os.path.isfile(
                            os.path.join(full, "__init__.py")):
                    for r, ds, fs in os.walk(full):
                        ds.sort()
                        fs.sort()
                        for f in fs:
                            m, x = os.path.splitext(f)
                            if x in [".pyi", ".py"]:
                                fn = os.path.join(r, f)
                                if match(fn, args, exclude_list):
                                    seen.add(mod)
                                    files.append(fn)
        if files:
            runs += 1
            flags = ["--python-version", "%d.%d" % (major, minor)]
            flags.append("--strict-optional")
            flags.append("--no-site-packages")
            flags.append("--show-traceback")
            flags.append("--no-implicit-optional")
            flags.append("--disallow-any-generics")
            flags.append("--disallow-subclassing-any")
            # Setting custom typeshed dir prevents mypy from falling back to its bundled typeshed in
            # case of stub deletions
            flags.append("--custom-typeshed-dir")
            flags.append(os.path.dirname(os.path.dirname(__file__)))
            if args.warn_unused_ignores:
                flags.append("--warn-unused-ignores")
            if args.platform:
                flags.extend(["--platform", args.platform])
            sys.argv = ["mypy"] + flags + files
            if args.verbose:
                print("running", " ".join(sys.argv))
            else:
                print("running mypy", " ".join(flags), "# with", len(files),
                      "files")
            try:
                if not args.dry_run:
                    mypy_main("", sys.stdout, sys.stderr)
            except SystemExit as err:
                code = max(code, err.code)
    if code:
        print("--- exit status", code, "---")
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)
示例#12
0
    print("No files specified. Arborting")
    sys.exit(0)

# Generate mypy arguments

ARGS = [
    # strictness: same as --strict minus --disallow-subclassing-any
    "--warn-unused-configs",
    "--disallow-any-generics",
    "--disallow-untyped-calls",
    "--disallow-untyped-defs",
    "--disallow-incomplete-defs",
    "--check-untyped-defs",
    "--disallow-untyped-decorators",
    "--no-implicit-optional",
    "--warn-redundant-casts",
    "--warn-unused-ignores",
    "--warn-return-any",
    "--no-implicit-reexport",
    "--strict-equality",
    "--ignore-missing-imports",
    # config
    "--follow-imports=skip",  # Remove eventually
    "--config-file=" + os.path.abspath(os.path.join(localdir, "mypy.ini")),
    "--show-traceback",
] + [os.path.abspath(f) for f in FILES]

# Run mypy over the files

mypy_main(None, sys.stdout, sys.stderr, ARGS)
示例#13
0
def main():
    args = parser.parse_args()

    try:
        from mypy.main import main as mypy_main
    except ImportError:
        print("Cannot import mypy. Did you install it?")
        sys.exit(1)

    versions = [(3, 5), (3, 4), (3, 3), (3, 2), (2, 7)]
    if args.python_version:
        versions = [v for v in versions
                    if any(('%d.%d' % v).startswith(av) for av in args.python_version)]
        if not versions:
            print("--- no versions selected ---")
            sys.exit(1)

    code = 0
    runs = 0
    for major, minor in versions:
        roots = libpath(major, minor)
        files = []
        seen = {'__builtin__', 'builtins', 'typing'}  # Always ignore these.
        for root in roots:
            names = os.listdir(root)
            for name in names:
                full = os.path.join(root, name)
                mod, ext = os.path.splitext(name)
                if mod in seen:
                    continue
                if ext in ['.pyi', '.py']:
                    if match(args, full):
                        seen.add(mod)
                        files.append(full)
                elif (os.path.isfile(os.path.join(full, '__init__.pyi')) or
                      os.path.isfile(os.path.join(full, '__init__.py'))):
                    for r, ds, fs in os.walk(full):
                        ds.sort()
                        fs.sort()
                        for f in fs:
                            m, x = os.path.splitext(f)
                            if x in ['.pyi', '.py']:
                                fn = os.path.join(r, f)
                                if match(args, fn):
                                    seen.add(mod)
                                    files.append(fn)
        if files:
            runs += 1
            flags = ['--python-version', '%d.%d' % (major, minor)]
            flags.append('--strict-optional')
            ##flags.append('--warn-unused-ignores')
            sys.argv = ['mypy'] + flags + files
            if args.verbose:
                print("running", ' '.join(sys.argv))
            else:
                print("running mypy", ' '.join(flags), "# with", len(files), "files")
            try:
                if not args.dry_run:
                    mypy_main('')
            except SystemExit as err:
                code = max(code, err.code)
    if code:
        print("--- exit status", code, "---")
        sys.exit(code)
    if not runs:
        print("--- nothing to do; exit 1 ---")
        sys.exit(1)
    print("### Checking PEP-8 code adherence ###")
    try:
        flake8_main()
    except SystemExit as err:
        success_flake8 = (not err.code)

    print("### Verifying type annotations and type coherence ###")
    try:
        # This requires packages and modules to be well-defined (i.e., have __init__.py)
        # Which is a useful way to keep type-checking out of messy experimental folders
        # with an opt-in mechanism
        mypy_main(
            None,
            stdout=sys.stdout,
            stderr=sys.stderr,
            args=[
                "--ignore-missing-imports", "--strict-optional",
                "--incremental", "-p", "eai_graph_tools"
            ],
        )
        success_mypy = True
    except SystemExit:
        success_mypy = False

    print("### Running unit tests ###")
    success_pytest = (pytest.main(sys.argv[1:] + ["tests/"]) == 0)

    # print("### Running integration tests ###")
    # TODO: review integration testing

    if all([success_flake8, success_mypy, success_pytest]):