Skip to content

Commit 6c9386b

Browse files
hetmankpilevkivskyi
authored andcommitted
Cache directory may be set via environment (#7016) (#7443)
This adds an environment variable MYPY_CACHE_DIR which can set the current cache directory. This variable will override the cache_dir setting in the configuration but can be overridden via the command line. Fixes #7016.
1 parent 14c6877 commit 6c9386b

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

docs/source/command_line.rst

+3
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ beyond what incremental mode can offer, try running mypy in
533533
change this folder. This flag can also be useful for controlling
534534
cache use when using :ref:`remote caching <remote-cache>`.
535535

536+
This setting will override the ``MYPY_CACHE_DIR`` environment
537+
variable if it is set.
538+
536539
Mypy will also always write to the cache even when incremental
537540
mode is disabled so it can "warm up" the cache. To disable
538541
writing to the cache, use ``--cache-dir=/dev/null`` (UNIX)

docs/source/config_file.rst

+2
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ section of the command line docs.
416416
``cache_dir`` (string, default ``.mypy_cache``)
417417
Specifies the location where mypy stores incremental cache info.
418418
User home directory and environment variables will be expanded.
419+
This setting will be overridden by the ``MYPY_CACHE_DIR`` environment
420+
variable.
419421

420422
Note that the cache is only read when incremental mode is enabled
421423
but is always written to, unless the value is set to ``/dev/nul``

mypy/main.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ def infer_python_executable(options: Options,
244244
""" # type: Final
245245

246246
FOOTER = """Environment variables:
247-
Define MYPYPATH for additional module search path entries.""" # type: Final
247+
Define MYPYPATH for additional module search path entries.
248+
Define MYPY_CACHE_DIR to override configuration cache_dir path.""" # type: Final
248249

249250

250251
def process_options(args: List[str],
@@ -704,6 +705,11 @@ def add_invertible_flag(flag: str,
704705
for dest, value in strict_flag_assignments:
705706
setattr(options, dest, value)
706707

708+
# Override cache_dir if provided in the environment
709+
environ_cache_dir = os.getenv('MYPY_CACHE_DIR', '')
710+
if environ_cache_dir.strip():
711+
options.cache_dir = environ_cache_dir
712+
707713
# Parse command line for real, using a split namespace.
708714
special_opts = argparse.Namespace()
709715
parser.parse_args(args, SplitNamespace(options, special_opts, 'special-opts:'))

0 commit comments

Comments
 (0)