From 3518e062cc46e9b0c6996c1130643f4ab6a3e25a Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 12 Jul 2016 18:38:19 -0700 Subject: [PATCH 1/2] Add flag to set custom mypy cache directory This pull request implements https://github.com/python/mypy/issues/1414 by adding the `--cache-dir` command line flag. Mypy will continue to default to using `.mypy_cache` when the flag is omitted. Usage: mypy --cache-dir ../.custom-cache my_module --- mypy/build.py | 15 ++++++++------- mypy/defaults.py | 1 + mypy/main.py | 3 +++ mypy/options.py | 1 + mypy/test/testcheck.py | 4 ++-- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 817cb5ea9a1f..d27ac802c25d 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -671,22 +671,21 @@ def read_with_python_encoding(path: str, pyversion: Tuple[int, int]) -> str: return source_bytearray.decode(encoding) -MYPY_CACHE = '.mypy_cache' - - -def get_cache_names(id: str, path: str, pyversion: Tuple[int, int]) -> Tuple[str, str]: +def get_cache_names(id: str, path: str, cache_dir: str, + pyversion: Tuple[int, int]) -> Tuple[str, str]: """Return the file names for the cache files. Args: id: module ID path: module path (used to recognize packages) + cache_dir: cache directory pyversion: Python version (major, minor) Returns: A tuple with the file names to be used for the meta JSON and the data JSON, respectively. """ - prefix = os.path.join(MYPY_CACHE, '%d.%d' % pyversion, *id.split('.')) + prefix = os.path.join(cache_dir, '%d.%d' % pyversion, *id.split('.')) is_package = os.path.basename(path).startswith('__init__.py') if is_package: prefix = os.path.join(prefix, '__init__') @@ -706,7 +705,8 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> Optional[Cache valid; otherwise None. """ # TODO: May need to take more build options into account - meta_json, data_json = get_cache_names(id, path, manager.options.python_version) + meta_json, data_json = get_cache_names( + id, path, manager.options.cache_dir, manager.options.python_version) manager.trace('Looking for {} {}'.format(id, data_json)) if not os.path.exists(meta_json): return None @@ -795,7 +795,8 @@ def write_cache(id: str, path: str, tree: MypyFile, st = os.stat(path) # TODO: Errors mtime = st.st_mtime size = st.st_size - meta_json, data_json = get_cache_names(id, path, manager.options.python_version) + meta_json, data_json = get_cache_names( + id, path, manager.options.cache_dir, manager.options.python_version) manager.log('Writing {} {} {}'.format(id, meta_json, data_json)) data = tree.serialize() parent = os.path.dirname(data_json) diff --git a/mypy/defaults.py b/mypy/defaults.py index 5a0875f525e2..9ce210b1f097 100644 --- a/mypy/defaults.py +++ b/mypy/defaults.py @@ -1,2 +1,3 @@ PYTHON2_VERSION = (2, 7) PYTHON3_VERSION = (3, 5) +MYPY_CACHE = '.mypy_cache' diff --git a/mypy/main.py b/mypy/main.py index 1d4fca785868..f83249f35dfc 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -166,6 +166,9 @@ def parse_version(v: str) -> Tuple[int, int]: help="enable experimental fast parser") parser.add_argument('-i', '--incremental', action='store_true', help="enable experimental module cache") + parser.add_argument('--cache-dir', action='store', metavar='DIR', + help="store module cache info in the given folder in incremental mode " + "(defaults to '{}')".format(defaults.MYPY_CACHE)) parser.add_argument('--strict-optional', action='store_true', dest='special-opts:strict_optional', help="enable experimental strict Optional checks") diff --git a/mypy/options.py b/mypy/options.py index b29db48c62a3..cf1340af3978 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -55,6 +55,7 @@ def __init__(self) -> None: # -- experimental options -- self.fast_parser = False self.incremental = False + self.cache_dir = defaults.MYPY_CACHE def __eq__(self, other: object) -> bool: return self.__class__ == other.__class__ and self.__dict__ == other.__dict__ diff --git a/mypy/test/testcheck.py b/mypy/test/testcheck.py index f97aa00dd773..0136e5a9f147 100644 --- a/mypy/test/testcheck.py +++ b/mypy/test/testcheck.py @@ -8,7 +8,7 @@ from typing import Tuple, List, Dict, Set -from mypy import build +from mypy import build, defaults import mypy.myunit # for mutable globals (ick!) from mypy.build import BuildSource, find_module_clear_caches from mypy.myunit import Suite, AssertionFailure @@ -96,7 +96,7 @@ def run_test(self, testcase: DataDrivenTestCase) -> None: self.run_test_once(testcase) def clear_cache(self) -> None: - dn = build.MYPY_CACHE + dn = defaults.MYPY_CACHE if os.path.exists(dn): shutil.rmtree(dn) From dc62a9e068f7f73fade4664ce504295c69e15a8b Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 12 Jul 2016 18:52:08 -0700 Subject: [PATCH 2/2] Fix linting errors --- mypy/build.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index d27ac802c25d..c4b945921420 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -706,7 +706,7 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> Optional[Cache """ # TODO: May need to take more build options into account meta_json, data_json = get_cache_names( - id, path, manager.options.cache_dir, manager.options.python_version) + id, path, manager.options.cache_dir, manager.options.python_version) manager.trace('Looking for {} {}'.format(id, data_json)) if not os.path.exists(meta_json): return None @@ -796,7 +796,7 @@ def write_cache(id: str, path: str, tree: MypyFile, mtime = st.st_mtime size = st.st_size meta_json, data_json = get_cache_names( - id, path, manager.options.cache_dir, manager.options.python_version) + id, path, manager.options.cache_dir, manager.options.python_version) manager.log('Writing {} {} {}'.format(id, meta_json, data_json)) data = tree.serialize() parent = os.path.dirname(data_json)