Skip to content

Commit c528638

Browse files
Michael0x2agvanrossum
authored andcommitted
Add flag to set custom mypy cache directory (#1853)
Fixes #1414 This pull request adds 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
1 parent 839f16a commit c528638

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

mypy/build.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,22 +671,21 @@ def read_with_python_encoding(path: str, pyversion: Tuple[int, int]) -> str:
671671
return source_bytearray.decode(encoding)
672672

673673

674-
MYPY_CACHE = '.mypy_cache'
675-
676-
677-
def get_cache_names(id: str, path: str, pyversion: Tuple[int, int]) -> Tuple[str, str]:
674+
def get_cache_names(id: str, path: str, cache_dir: str,
675+
pyversion: Tuple[int, int]) -> Tuple[str, str]:
678676
"""Return the file names for the cache files.
679677
680678
Args:
681679
id: module ID
682680
path: module path (used to recognize packages)
681+
cache_dir: cache directory
683682
pyversion: Python version (major, minor)
684683
685684
Returns:
686685
A tuple with the file names to be used for the meta JSON and the
687686
data JSON, respectively.
688687
"""
689-
prefix = os.path.join(MYPY_CACHE, '%d.%d' % pyversion, *id.split('.'))
688+
prefix = os.path.join(cache_dir, '%d.%d' % pyversion, *id.split('.'))
690689
is_package = os.path.basename(path).startswith('__init__.py')
691690
if is_package:
692691
prefix = os.path.join(prefix, '__init__')
@@ -706,7 +705,8 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> Optional[Cache
706705
valid; otherwise None.
707706
"""
708707
# TODO: May need to take more build options into account
709-
meta_json, data_json = get_cache_names(id, path, manager.options.python_version)
708+
meta_json, data_json = get_cache_names(
709+
id, path, manager.options.cache_dir, manager.options.python_version)
710710
manager.trace('Looking for {} {}'.format(id, data_json))
711711
if not os.path.exists(meta_json):
712712
return None
@@ -795,7 +795,8 @@ def write_cache(id: str, path: str, tree: MypyFile,
795795
st = os.stat(path) # TODO: Errors
796796
mtime = st.st_mtime
797797
size = st.st_size
798-
meta_json, data_json = get_cache_names(id, path, manager.options.python_version)
798+
meta_json, data_json = get_cache_names(
799+
id, path, manager.options.cache_dir, manager.options.python_version)
799800
manager.log('Writing {} {} {}'.format(id, meta_json, data_json))
800801
data = tree.serialize()
801802
parent = os.path.dirname(data_json)

mypy/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
PYTHON2_VERSION = (2, 7)
22
PYTHON3_VERSION = (3, 5)
3+
MYPY_CACHE = '.mypy_cache'

mypy/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ def parse_version(v: str) -> Tuple[int, int]:
166166
help="enable experimental fast parser")
167167
parser.add_argument('-i', '--incremental', action='store_true',
168168
help="enable experimental module cache")
169+
parser.add_argument('--cache-dir', action='store', metavar='DIR',
170+
help="store module cache info in the given folder in incremental mode "
171+
"(defaults to '{}')".format(defaults.MYPY_CACHE))
169172
parser.add_argument('--strict-optional', action='store_true',
170173
dest='special-opts:strict_optional',
171174
help="enable experimental strict Optional checks")

mypy/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(self) -> None:
5555
# -- experimental options --
5656
self.fast_parser = False
5757
self.incremental = False
58+
self.cache_dir = defaults.MYPY_CACHE
5859

5960
def __eq__(self, other: object) -> bool:
6061
return self.__class__ == other.__class__ and self.__dict__ == other.__dict__

mypy/test/testcheck.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from typing import Tuple, List, Dict, Set
1010

11-
from mypy import build
11+
from mypy import build, defaults
1212
import mypy.myunit # for mutable globals (ick!)
1313
from mypy.build import BuildSource, find_module_clear_caches
1414
from mypy.myunit import Suite, AssertionFailure
@@ -96,7 +96,7 @@ def run_test(self, testcase: DataDrivenTestCase) -> None:
9696
self.run_test_once(testcase)
9797

9898
def clear_cache(self) -> None:
99-
dn = build.MYPY_CACHE
99+
dn = defaults.MYPY_CACHE
100100

101101
if os.path.exists(dn):
102102
shutil.rmtree(dn)

0 commit comments

Comments
 (0)