diff --git a/mypy-strict.ini b/mypy-strict.ini index 70952c149670..bcb4e4ace01d 100644 --- a/mypy-strict.ini +++ b/mypy-strict.ini @@ -9,6 +9,7 @@ [mypy] python_version = 3.6 +plugins = mypy_plugins/check_mypy_version.py cache_dir = .mypy_cache/strict strict_optional = True diff --git a/mypy.ini b/mypy.ini index ae1547b6c6e7..bbfecafe662a 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,6 +2,8 @@ # test_run_mypy in test/test_type_hints.py uses this string) [mypy] +plugins = mypy_plugins/check_mypy_version.py + cache_dir = .mypy_cache/normal warn_unused_configs = True warn_redundant_casts = True diff --git a/mypy_plugins/check_mypy_version.py b/mypy_plugins/check_mypy_version.py new file mode 100644 index 000000000000..02a02a60b950 --- /dev/null +++ b/mypy_plugins/check_mypy_version.py @@ -0,0 +1,33 @@ +import re +import sys +from pathlib import Path + +from mypy.plugin import Plugin + + +def get_correct_mypy_version(): + # there's probably a more elegant way to do this + match, = re.finditer( + r'mypy==(\d+(?:\.\d+)*)', + Path('.circleci/docker/common/install_conda.sh').read_text(), + ) + version, = match.groups() + return version + + +def plugin(version: str): + correct_version = get_correct_mypy_version() + if version != correct_version: + print(f'''\ +You are using mypy version {version}, which is not supported +in the PyTorch repo. Please switch to mypy version {correct_version}. + +For example, if you installed mypy via pip, run this: + + pip install mypy=={correct_version} + +Or if you installed mypy via conda, run this: + + conda install -c conda-forge mypy={correct_version} +''', file=sys.stderr) + return Plugin