zulip/tools/run-mypy

88 lines
2.7 KiB
Plaintext
Raw Permalink Normal View History

#!/usr/bin/env python3
import argparse
import os
import subprocess
import sys
from zulint import lister
2016-07-24 09:38:08 +02:00
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(TOOLS_DIR)
os.chdir(ROOT_DIR)
sys.path.append(ROOT_DIR)
from tools.lib.test_script import add_provision_check_override_param, assert_provisioning_status_ok
exclude = """
stubs/
""".split()
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
parser.add_argument("targets", nargs="*", help="files and directories to check (default: .)")
parser.add_argument("--version", action="store_true", help="show mypy version information and exit")
parser.add_argument("-m", "--modified", action="store_true", help="check only modified files")
parser.add_argument(
"--scripts-only", action="store_true", help="only check extensionless python scripts"
)
parser.add_argument(
"-a", "--all", action="store_true", help="check all files, bypassing the default exclude list"
)
parser.add_argument("-d", "--use-daemon", action="store_true", help="run mypy daemon instead")
add_provision_check_override_param(parser)
parser.add_argument("--quiet", action="store_true", help="suppress mypy summary output")
args = parser.parse_args()
assert_provisioning_status_ok(args.skip_provision_check)
if args.use_daemon:
command_name = "dmypy"
else:
command_name = "mypy"
# 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", command_name)
if os.path.exists(MYPY_VENV_PATH):
mypy_command = MYPY_VENV_PATH
else:
mypy_command = command_name
if args.version:
print("mypy command:", mypy_command)
sys.exit(subprocess.call([mypy_command, "--version"]))
if args.all:
exclude = []
# find all non-excluded files in current directory
linter_lib: Fix mypy errors. tools/linter_lib/pyflakes.py:35: error: Argument 3 to "run_pyflakes" has incompatible type "List[Tuple[bytes, bytes]]"; expected "List[Tuple[str, str]]" tools/linter_lib/custom_check.py:110: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:214: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:214: error: Argument "shebang_rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:502: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:502: error: Argument "shebang_rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:519: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:706: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:728: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:738: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:779: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:779: error: Argument "length_exclude" to "RuleList" has incompatible type "Set[str]"; expected "List[str]" tools/linter_lib/custom_check.py:803: error: Argument "length_exclude" to "RuleList" has incompatible type "Set[str]"; expected "List[str]" tools/linter_lib/custom_check.py:805: error: Unsupported operand types for + ("List[Rule]" and "List[Dict[str, Any]]") tools/linter_lib/custom_check.py:819: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" These were missed the `zulint` package was missing PEP 561 type annotation markers, and if it’d had them, mypy daemon mode would’ve required us to set `follow_imports = skip` for it. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-08-07 03:29:33 +02:00
files_dict = lister.list_files(
targets=args.targets,
ftypes=["py", "pyi"],
use_shebang=True,
modified_only=args.modified,
exclude=exclude,
group_by_ftype=True,
linter_lib: Fix mypy errors. tools/linter_lib/pyflakes.py:35: error: Argument 3 to "run_pyflakes" has incompatible type "List[Tuple[bytes, bytes]]"; expected "List[Tuple[str, str]]" tools/linter_lib/custom_check.py:110: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:214: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:214: error: Argument "shebang_rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:502: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:502: error: Argument "shebang_rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:519: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:706: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:728: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:738: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:779: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" tools/linter_lib/custom_check.py:779: error: Argument "length_exclude" to "RuleList" has incompatible type "Set[str]"; expected "List[str]" tools/linter_lib/custom_check.py:803: error: Argument "length_exclude" to "RuleList" has incompatible type "Set[str]"; expected "List[str]" tools/linter_lib/custom_check.py:805: error: Unsupported operand types for + ("List[Rule]" and "List[Dict[str, Any]]") tools/linter_lib/custom_check.py:819: error: Argument "rules" to "RuleList" has incompatible type "List[Dict[str, Any]]"; expected "List[Rule]" These were missed the `zulint` package was missing PEP 561 type annotation markers, and if it’d had them, mypy daemon mode would’ve required us to set `follow_imports = skip` for it. Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-08-07 03:29:33 +02:00
extless_only=args.scripts_only,
)
pyi_files = list(files_dict["pyi"])
python_files = [
fpath for fpath in files_dict["py"] if not fpath.endswith(".py") or fpath + "i" not in pyi_files
]
if not python_files and not pyi_files:
print("There are no files to run mypy on.")
sys.exit(0)
mypy_args: list[str] = []
# --no-error-summary is a mypy flag that comes after all dmypy options
if args.use_daemon:
mypy_args += ["run", "--"]
if args.quiet:
mypy_args += ["--no-error-summary"]
mypy_args += ["--", *python_files, *pyi_files]
rc = subprocess.call([mypy_command, *mypy_args])
if rc != 0:
print()
print("See https://zulip.readthedocs.io/en/latest/testing/mypy.html for debugging tips.")
sys.exit(rc)