zulip/tools/run-mypy

116 lines
4.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
from __future__ import absolute_import
from __future__ import print_function
import os
import sys
import argparse
import subprocess
import six
import lister
2016-07-24 09:38:08 +02:00
from typing import cast, Dict, List
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(os.path.dirname(TOOLS_DIR))
sys.path.append(os.path.dirname(TOOLS_DIR))
from lib.test_script import get_provisioning_status
exclude = """
2016-05-17 17:31:39 +02:00
zproject/settings.py
zproject/test_settings.py
""".split()
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
parser.add_argument('targets', nargs='*',
help="""files and directories to include in the result.
If this is not specified, the current directory is used""")
parser.add_argument('-m', '--modified', action='store_true',
help='list only modified files')
parser.add_argument('-a', '--all', action='store_true',
help="""run mypy on all python files, ignoring the exclude list.
This is useful if you have to find out which files fail mypy check.""")
parser.add_argument('--linecoverage-report', action='store_true',
help="""run the linecoverage report to see annotation coverage""")
parser.add_argument('--no-disallow-untyped-defs',
dest='disallow_untyped_defs', action='store_false',
help="""Don't throw errors when functions are not annotated""")
parser.add_argument('--scripts-only', action='store_true',
help="""Only type check extensionless python scripts""")
parser.add_argument('--strict-optional', action='store_true',
2017-05-23 20:41:07 +02:00
help="""Use the --strict-optional flag with mypy""")
parser.add_argument('--warn-unused-ignores', action='store_true',
help="""Use the --warn-unused-ignores flag with mypy""")
parser.add_argument('--no-ignore-missing-imports',
dest='ignore_missing_imports', action='store_false',
2017-05-23 20:41:07 +02:00
help="""Don't use the --ignore-missing-imports flag with mypy""")
parser.add_argument('--quick', action='store_true',
2017-05-23 20:41:07 +02:00
help="""Use the --quick flag with mypy""")
parser.add_argument('--force', action="store_true",
help='Run tests despite possible provisioning problems.')
args = parser.parse_args()
2016-07-01 18:38:57 +02:00
if not args.force:
ok, msg = get_provisioning_status()
if not ok:
print(msg)
print('If you really know what you are doing, use --force to run anyway.')
sys.exit(1)
if args.all:
exclude = []
# find all non-excluded files in current directory
2016-07-24 09:38:08 +02:00
files_dict = cast(Dict[str, List[str]],
lister.list_files(targets=args.targets, ftypes=['py', 'pyi'],
use_shebang=True, modified_only=args.modified,
2016-07-24 09:38:08 +02:00
exclude = exclude + ['stubs'], group_by_ftype=True,
extless_only=args.scripts_only))
pyi_files = set(files_dict['pyi'])
python_files = [fpath for fpath in files_dict['py']
if not fpath.endswith('.py') or fpath + 'i' not in pyi_files]
# Use zulip-py3-venv's mypy if it's available.
VENV_DIR = "/srv/zulip-py3-venv"
MYPY_VENV_PATH = os.path.join(VENV_DIR, "bin", "mypy")
if os.path.exists(MYPY_VENV_PATH):
mypy_command = MYPY_VENV_PATH
print("Using mypy from", mypy_command)
else:
mypy_command = "mypy"
2017-05-23 20:41:07 +02:00
extra_args = ["--check-untyped-defs",
"--follow-imports=silent",
"--scripts-are-modules",
"-i", "--cache-dir=var/mypy-cache"]
if args.linecoverage_report:
extra_args.append("--linecoverage-report")
extra_args.append("var/linecoverage-report")
if args.disallow_untyped_defs:
extra_args.append("--disallow-untyped-defs")
if args.warn_unused_ignores:
extra_args.append("--warn-unused-ignores")
2017-05-23 20:41:07 +02:00
if args.strict_optional:
extra_args.append("--strict-optional")
if args.ignore_missing_imports:
extra_args.append("--ignore-missing-imports")
if args.quick:
extra_args.append("--quick")
# run mypy
if python_files:
rc = subprocess.call([mypy_command] + extra_args + python_files)
if args.linecoverage_report:
# Move the coverage report to where codecov will look for it.
try:
os.rename('var/linecoverage-report/coverage.txt', 'var/.coverage')
except OSError:
# maybe mypy crashed; exit with its error code
pass
sys.exit(rc)
else:
print("There are no files to run mypy on.")