Skip to content

Add flag to set custom mypy cache directory #1853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__')
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions mypy/defaults.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PYTHON2_VERSION = (2, 7)
PYTHON3_VERSION = (3, 5)
MYPY_CACHE = '.mypy_cache'
3 changes: 3 additions & 0 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
4 changes: 2 additions & 2 deletions mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down