Repository: python/mypy

Issue:
Set MYPY_CONFIG_FILE_DIR environment variable
In addition to / as an alternative to https://github.com/python/mypy/issues/7967 mypy could set an environment variable of the config file being used.

This could be used with e.g. `mypy_path` then (`mypy_path = $MYPY_CONFIGFILE/src`), and also with defaults like `cache_dir`.


Discussion:
I like this better than #7967, since this preserves backward compatibility. My proposal for the name of the variable is `MYPY_CONFIG_FILE_DIR`.
hi @blueyed  i want work on it will  you please elaborate  it


@mridullpandey 
What is unclear here?
It should set `MYPY_CONFIG_FILE_DIR` in `os.environ`, so that it can be expanded / is available while parsing the config.
btw: I think `$MYPY_CONFIG_FILE(_PATH)` might be more flexible: it would provide the actual file (which might be `mypy.ini`, `setup.cfg` etc), and could also be used as `$MYPY_CONFIG_FILE/../…` to have its directory then.
While `$MYPY_CONFIG_FILE_DIR` might be more explicit, we might end up with adding `$MYPY_CONFIG_FILE_PATH` additionally afterwards.
@blueyed Do you have a use case for using the path to the config file, instead of the directory where it's located? Using `..` after a path to a file looks odd to me (though it's not hard to support), and I'd rather avoid it.

@mridullpandey Config file parsing is implemented in `mypy.config_parser`. In particular, `expand_path` seems relevant.
@JukkaL 
Not really, but thought about it when adding `PYTEST_CONFIG_{FILE,DIR}` to pytest, and figured it might be more flexible, since you cannot determine the ini file being used from the dir (due to different names).
As for pytest I've thought that processes/tests being run by pytest then might be able to get the used config file location, but have not thought much about a use case.

`$file/..` is a bit weird, but gets resolved fine via `os.path.{normpath,realpath}`.

So I am +0 myself on using the file - mainly to avoid having to add another env var later, and given that it is more explicit/precise.  But it has the downside of having to append "/.." for the more typical use case (this issue).
i'm taking this issue
PR was reverted (it broke CI).

Resolving patch:
diff --git a/mypy/config_parser.py b/mypy/config_parser.py
--- a/mypy/config_parser.py
+++ b/mypy/config_parser.py
@@ -135,6 +135,9 @@ def parse_config_file(options: Options, set_strict_flags: Callable[[], None],
     else:
         return
 
+    os.environ['MYPY_CONFIG_FILE_DIR'] = os.path.dirname(
+            os.path.abspath(config_file))
+
     if 'mypy' not in parser:
         if filename or file_read not in defaults.SHARED_CONFIG_FILES:
             print("%s: No [mypy] section in config file" % file_read, file=stderr)


Test patch:
diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py
--- a/mypy/test/testcmdline.py
+++ b/mypy/test/testcmdline.py
@@ -25,6 +25,7 @@
 cmdline_files = [
     'cmdline.test',
     'reports.test',
+    'envvars.test',
 ]
 
 
diff --git a/test-data/unit/envvars.test b/test-data/unit/envvars.test
new file mode 100644
--- /dev/null
+++ b/test-data/unit/envvars.test
@@ -0,0 +1,11 @@
+# Test cases related to environment variables
+[case testEnvvar_MYPY_CONFIG_FILE_DIR]
+# cmd: mypy --config-file=subdir/mypy.ini
+[file bogus.py]
+FOO = 'x'  # type: int
+[file subdir/good.py]
+BAR = 0  # type: int
+[file subdir/mypy.ini]
+\[mypy]
+files=$MYPY_CONFIG_FILE_DIR/good.py
+