2020-04-03 05:11:02 -04:00
|
|
|
#!/usr/bin/env python3
|
2017-09-15 07:24:20 -04:00
|
|
|
|
|
|
|
import argparse
|
2021-05-28 05:00:04 -04:00
|
|
|
import os
|
2017-09-15 07:24:20 -04:00
|
|
|
import subprocess
|
2021-05-28 05:00:04 -04:00
|
|
|
import sys
|
2017-09-26 13:34:11 -04:00
|
|
|
from collections import OrderedDict
|
|
|
|
from pathlib import PurePath
|
2021-05-28 05:00:04 -04:00
|
|
|
from typing import Dict, List, cast
|
|
|
|
|
2019-08-02 09:44:11 -04:00
|
|
|
from zulint import lister
|
2017-09-15 07:24:20 -04:00
|
|
|
|
|
|
|
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
os.chdir(os.path.dirname(TOOLS_DIR))
|
|
|
|
|
|
|
|
sys.path.append(os.path.dirname(TOOLS_DIR))
|
|
|
|
|
2017-10-27 02:02:52 -04:00
|
|
|
exclude = [
|
|
|
|
# Excluded because it's third-party code.
|
|
|
|
"zulip/integrations/perforce/git_p4.py",
|
|
|
|
# Excluded because we don't want to require bot authors to
|
|
|
|
# fully annotate their bots.
|
|
|
|
"zulip_bots/zulip_bots/bots",
|
2017-11-02 09:47:28 -04:00
|
|
|
"zulip_bots/zulip_bots/bots_unmaintained",
|
2017-11-28 11:44:39 -05:00
|
|
|
# Excluded out of laziness:
|
|
|
|
"zulip_bots/zulip_bots/terminal.py",
|
|
|
|
"zulip_bots/zulip_bots/simple_lib.py",
|
2018-05-30 17:53:10 -04:00
|
|
|
"zulip_bots/zulip_bots/tests/test_lib.py",
|
2018-09-14 16:31:13 -04:00
|
|
|
# Excluded because this is a self-contained script
|
|
|
|
# we ask our users to download and run directly and
|
|
|
|
# py2 and py3 compatibility is required.
|
|
|
|
"zulip/integrations/trello/zulip_trello.py",
|
2018-03-22 21:37:49 -04:00
|
|
|
"tools",
|
2017-10-27 02:02:52 -04:00
|
|
|
]
|
2017-09-15 07:24:20 -04:00
|
|
|
|
2017-12-04 04:54:56 -05:00
|
|
|
# These files will be included even if excluded by a rule above.
|
|
|
|
force_include = [
|
|
|
|
# Include bots that we migrate to mypy.
|
|
|
|
"zulip_bots/zulip_bots/bots/helloworld/helloworld.py",
|
2017-12-08 04:09:38 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/helloworld/test_helloworld.py",
|
2017-12-06 19:58:46 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/followup/followup.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/followup/test_followup.py",
|
2017-12-06 20:03:53 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/giphy/giphy.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/giphy/test_giphy.py",
|
2017-12-07 14:25:28 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/github_detail/github_detail.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py",
|
2017-12-11 13:26:31 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/google_search/google_search.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/google_search/test_google_search.py",
|
2017-12-07 14:44:42 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/help/help.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/help/test_help.py",
|
2017-12-09 16:55:55 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/incrementor/incrementor.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/incrementor/test_incrementor.py",
|
2017-12-09 17:00:33 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/link_shortener/link_shortener.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py",
|
2017-12-08 06:14:55 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/virtual_fs/test_virtual_fs.py",
|
2017-12-08 07:31:42 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/weather/test_weather.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/weather/weather.py",
|
2017-12-09 05:03:45 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/youtube/youtube.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/youtube/test_youtube.py",
|
2017-12-07 18:53:29 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/converter/converter.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/converter/test_converter.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/define/define.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/define/test_define.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/encrypt/encrypt.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/encrypt/test_encrypt.py",
|
2018-05-28 06:45:28 -04:00
|
|
|
"zulip_bots/zulip_bots/bots/chessbot/chessbot.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/chessbot/test_chessbot.py",
|
2017-12-09 15:35:16 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/xkcd/xkcd.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/xkcd/test_xkcd.py",
|
2017-12-17 21:23:22 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/witai/witai.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/witai/test_witai.py",
|
2017-12-09 15:44:11 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/wikipedia/test_wikipedia.py",
|
2017-12-09 15:55:41 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/yoda/yoda.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/yoda/test_yoda.py",
|
2017-12-09 15:11:18 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/dialogflow/dialogflow.py",
|
2017-12-24 07:08:45 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/dialogflow/test_dialogflow.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/mention/mention.py",
|
2017-12-29 08:22:27 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/mention/test_mention.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/baremetrics/baremetrics.py",
|
2017-12-31 12:26:13 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/salesforce/salesforce.py",
|
2018-01-05 06:09:21 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/salesforce/test_salesforce.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/idonethis/idonethis.py",
|
2018-01-19 10:55:42 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/idonethis/test_idonethis.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/connect_four/connect_four.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/tictactoe/test_tictactoe.py",
|
2018-06-11 10:01:02 -04:00
|
|
|
"zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/trivia_quiz/test_trivia_quiz.py",
|
2018-01-19 10:55:42 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/game_handler_bot/test_game_handler_bot.py",
|
2018-01-23 12:29:26 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/trello/trello.py",
|
2018-02-22 00:49:52 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/trello/test_trello.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/susi/susi.py",
|
|
|
|
"zulip_bots/zulip_bots/bots/susi/test_susi.py",
|
2018-03-02 18:27:35 -05:00
|
|
|
"zulip_bots/zulip_bots/bots/front/front.py",
|
2018-03-22 21:37:49 -04:00
|
|
|
"zulip_bots/zulip_bots/bots/front/test_front.py",
|
2020-04-18 18:47:39 -04:00
|
|
|
"tools/custom_check.py",
|
2021-05-28 05:03:46 -04:00
|
|
|
"tools/deploy",
|
2017-12-04 04:54:56 -05:00
|
|
|
]
|
|
|
|
|
2017-09-15 07:24:20 -04:00
|
|
|
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")
|
2021-05-28 05:03:46 -04:00
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"targets",
|
|
|
|
nargs="*",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=[],
|
|
|
|
help="""files and directories to include in the result.
|
|
|
|
If this is not specified, the current directory is used""",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"-m", "--modified", action="store_true", default=False, help="list only modified files"
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"-a",
|
|
|
|
"--all",
|
|
|
|
dest="all",
|
|
|
|
action="store_true",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=False,
|
|
|
|
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(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--no-disallow-untyped-defs",
|
|
|
|
dest="disallow_untyped_defs",
|
|
|
|
action="store_false",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=True,
|
|
|
|
help="""Don't throw errors when functions are not annotated""",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--scripts-only",
|
|
|
|
dest="scripts_only",
|
|
|
|
action="store_true",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=False,
|
|
|
|
help="""Only type check extensionless python scripts""",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--warn-unused-ignores",
|
|
|
|
dest="warn_unused_ignores",
|
|
|
|
action="store_true",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=False,
|
|
|
|
help="""Use the --warn-unused-ignores flag with mypy""",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--no-ignore-missing-imports",
|
|
|
|
dest="ignore_missing_imports",
|
|
|
|
action="store_false",
|
2021-05-28 05:03:46 -04:00
|
|
|
default=True,
|
|
|
|
help="""Don't use the --ignore-missing-imports flag with mypy""",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-28 05:05:11 -04:00
|
|
|
"--quick", action="store_true", default=False, help="""Use the --quick flag with mypy"""
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2017-09-15 07:24:20 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.all:
|
|
|
|
exclude = []
|
|
|
|
|
|
|
|
# find all non-excluded files in current directory
|
2021-05-28 05:03:46 -04:00
|
|
|
files_dict = cast(
|
|
|
|
Dict[str, List[str]],
|
|
|
|
lister.list_files(
|
|
|
|
targets=args.targets,
|
2021-05-28 05:05:11 -04:00
|
|
|
ftypes=["py", "pyi"],
|
2021-05-28 05:03:46 -04:00
|
|
|
use_shebang=True,
|
|
|
|
modified_only=args.modified,
|
2021-05-28 05:05:11 -04:00
|
|
|
exclude=exclude + ["stubs"],
|
2021-05-28 05:03:46 -04:00
|
|
|
group_by_ftype=True,
|
|
|
|
extless_only=args.scripts_only,
|
|
|
|
),
|
|
|
|
)
|
2017-12-04 04:54:56 -05:00
|
|
|
|
|
|
|
for inpath in force_include:
|
2018-03-22 21:37:49 -04:00
|
|
|
try:
|
2021-05-28 05:05:11 -04:00
|
|
|
ext = os.path.splitext(inpath)[1].split(".")[1]
|
2018-03-22 21:37:49 -04:00
|
|
|
except IndexError:
|
2021-05-28 05:05:11 -04:00
|
|
|
ext = "py" # type: str
|
2017-12-04 04:54:56 -05:00
|
|
|
files_dict[ext].append(inpath)
|
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
pyi_files = set(files_dict["pyi"])
|
2021-05-28 05:03:46 -04:00
|
|
|
python_files = [
|
2021-05-28 05:05:11 -04:00
|
|
|
fpath for fpath in files_dict["py"] if not fpath.endswith(".py") or fpath + "i" not in pyi_files
|
2021-05-28 05:03:46 -04:00
|
|
|
]
|
2017-09-15 07:24:20 -04:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
repo_python_files = OrderedDict(
|
2021-05-28 05:05:11 -04:00
|
|
|
[("zulip", []), ("zulip_bots", []), ("zulip_botserver", []), ("tools", [])]
|
2021-05-28 05:03:46 -04:00
|
|
|
)
|
2017-09-26 13:34:11 -04:00
|
|
|
for file_path in python_files:
|
|
|
|
repo = PurePath(file_path).parts[0]
|
|
|
|
if repo in repo_python_files:
|
|
|
|
repo_python_files[repo].append(file_path)
|
|
|
|
|
2017-09-15 07:24:20 -04:00
|
|
|
mypy_command = "mypy"
|
|
|
|
|
2018-03-31 02:00:35 -04:00
|
|
|
extra_args = ["--follow-imports=silent"]
|
2017-09-15 07:24:20 -04:00
|
|
|
if args.disallow_untyped_defs:
|
|
|
|
extra_args.append("--disallow-untyped-defs")
|
|
|
|
if args.warn_unused_ignores:
|
|
|
|
extra_args.append("--warn-unused-ignores")
|
|
|
|
if args.ignore_missing_imports:
|
|
|
|
extra_args.append("--ignore-missing-imports")
|
|
|
|
if args.quick:
|
|
|
|
extra_args.append("--quick")
|
|
|
|
|
|
|
|
# run mypy
|
2017-09-26 13:34:11 -04:00
|
|
|
status = 0
|
|
|
|
for repo, python_files in repo_python_files.items():
|
2021-05-28 07:19:40 -04:00
|
|
|
print(f"Running mypy for `{repo}`.", flush=True)
|
2017-09-26 13:34:11 -04:00
|
|
|
if python_files:
|
|
|
|
result = subprocess.call([mypy_command] + extra_args + python_files)
|
|
|
|
if result != 0:
|
|
|
|
status = result
|
|
|
|
else:
|
|
|
|
print("There are no files to run mypy on.")
|
|
|
|
sys.exit(status)
|