Repository: python/mypy

Issue:
Feature request: Cache directory environment variable
The cache directory can currently be customised using the --cache-dir argument. It would be convenient to also be able to do this using an environment variable akin to the MYPYPATH variable.

For very large projects that are not pure Python and contain multiple Python packages within the source tree, both the search path and cache directory often need to be customised for the entire project. Being able to do this via the environment is very convenient. Additionally, environment variables can often provide more flexible options when dealing with wrapper tools.

I would be happy to provide the implementation if this feature is considered reasonable.


Discussion:
I think it may be a reasonable thing to add, please go ahead with a PR.
I'm happy to provide an implementation however the one thing that is still not obvious to me is whether the environment should take precedence over the config file or the other way around.

Making the environment variable take precedence would give any wrapper scripts ultimate control, however it seems like in some circumstances this could lead to unexpected results as a localised config file option may be unexpectedly overridden by a more global environment setting.

I think I would err on the side of removing potentially confusing situations at the cost of perhaps reducing the power of this feature and make the config file take precedence over the environment variable.

Any thoughts?
I would try to match behaviour here with the one of `MYPYPATH` vs `mypy_path` as much as possible.
That would have been ideal but for `MYPYPATH` and `mypy_path` both are merged. In the case of `cache_dir` this is not possible as there is only a single path that can apply (rather than a set of paths) so either it or the environment variable would have to take precedence.
> That would have been ideal but for `MYPYPATH` and `mypy_path` both are merged. In the case of cache_dir this is not possible as there is only a single path that can apply

Well, but when you merge something, one part goes first, and another goes last, why can't we have the same "order" here?
Sure I can do that. It's just that that order makes the environment variable take precedence. I mean it should be fine as long as it's clear that the environment variable should not be used as a default fallback and I can clarify that in the documentation.

Resolving patch:
diff --git a/mypy/main.py b/mypy/main.py
--- a/mypy/main.py
+++ b/mypy/main.py
@@ -244,7 +244,8 @@ def infer_python_executable(options: Options,
 """  # type: Final
 
 FOOTER = """Environment variables:
-  Define MYPYPATH for additional module search path entries."""  # type: Final
+  Define MYPYPATH for additional module search path entries.
+  Define MYPY_CACHE_DIR to override configuration cache_dir path."""  # type: Final
 
 
 def process_options(args: List[str],
@@ -704,6 +705,11 @@ def add_invertible_flag(flag: str,
         for dest, value in strict_flag_assignments:
             setattr(options, dest, value)
 
+    # Override cache_dir if provided in the environment
+    environ_cache_dir = os.getenv('MYPY_CACHE_DIR', '')
+    if environ_cache_dir.strip():
+        options.cache_dir = environ_cache_dir
+
     # Parse command line for real, using a split namespace.
     special_opts = argparse.Namespace()
     parser.parse_args(args, SplitNamespace(options, special_opts, 'special-opts:'))


Test patch: