From ab171e298215fc9633f8516d723b4c8cdd1cfbc0 Mon Sep 17 00:00:00 2001 From: aghast Date: Wed, 2 Sep 2020 23:53:10 -0400 Subject: [PATCH 1/4] Add MYPY_CONFIG_FILE_DIR to environment when config file is read --- test-data/unit/envvars.test | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test-data/unit/envvars.test diff --git a/test-data/unit/envvars.test b/test-data/unit/envvars.test new file mode 100644 index 0000000000000..b52759d5b033f --- /dev/null +++ b/test-data/unit/envvars.test @@ -0,0 +1,14 @@ +# Test cases related to environment variables +[case testEnvvar_MYPY_CONFIG_FILE_DIR] +# cmd: mypy --config-file=subdir/mypy.ini +# flags: --save-failures-to=/tmp/mypy +# To run just this test: +# pytest -n0 -k testEnvvar_MYPY_CONFIG_FILE_DIR +[file bogus.py] +FOO: int = 'x' +[file subdir/good.py] +BAR: int = 0 +[file subdir/mypy.ini] +\[mypy] +files=$MYPY_CONFIG_FILE_DIR/good.py + From b9e1ccd42d8fb1758c62ac409d471301e8e7998b Mon Sep 17 00:00:00 2001 From: aghast Date: Thu, 3 Sep 2020 00:05:22 -0400 Subject: [PATCH 2/4] Code changes to add MYPY_CONFIG_FILE_DIR environment variable, and test case. See #7968 --- mypy/config_parser.py | 3 +++ mypy/test/testcmdline.py | 1 + 2 files changed, 4 insertions(+) diff --git a/mypy/config_parser.py b/mypy/config_parser.py index 7e1f16f56b25b..e5f769f0986b2 100644 --- 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) diff --git a/mypy/test/testcmdline.py b/mypy/test/testcmdline.py index 8d6a0d1fdc968..9ae6a0eb70761 100644 --- a/mypy/test/testcmdline.py +++ b/mypy/test/testcmdline.py @@ -25,6 +25,7 @@ cmdline_files = [ 'cmdline.test', 'reports.test', + 'envvars.test', ] From ed82e554f9796f0c745aaa315b634bc3f5c46ae2 Mon Sep 17 00:00:00 2001 From: aghast Date: Thu, 3 Sep 2020 20:43:55 -0400 Subject: [PATCH 3/4] Use type comments, not annotations, to support 3.5.1 --- test-data/unit/envvars.test | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test-data/unit/envvars.test b/test-data/unit/envvars.test index b52759d5b033f..fc11df71a12f6 100644 --- a/test-data/unit/envvars.test +++ b/test-data/unit/envvars.test @@ -1,13 +1,10 @@ # Test cases related to environment variables [case testEnvvar_MYPY_CONFIG_FILE_DIR] # cmd: mypy --config-file=subdir/mypy.ini -# flags: --save-failures-to=/tmp/mypy -# To run just this test: -# pytest -n0 -k testEnvvar_MYPY_CONFIG_FILE_DIR [file bogus.py] -FOO: int = 'x' +FOO = 'x' # type: int [file subdir/good.py] -BAR: int = 0 +BAR = 0 # type: int [file subdir/mypy.ini] \[mypy] files=$MYPY_CONFIG_FILE_DIR/good.py From 9a31096e0af0410be5b13505fcd58591b0d3f46b Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 3 Sep 2020 18:35:49 -0700 Subject: [PATCH 4/4] PEP 8 for the test code (no tabs or alignment) --- test-data/unit/envvars.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-data/unit/envvars.test b/test-data/unit/envvars.test index fc11df71a12f6..12835e6f1f7cf 100644 --- a/test-data/unit/envvars.test +++ b/test-data/unit/envvars.test @@ -2,9 +2,9 @@ [case testEnvvar_MYPY_CONFIG_FILE_DIR] # cmd: mypy --config-file=subdir/mypy.ini [file bogus.py] -FOO = 'x' # type: int +FOO = 'x'. # type: int [file subdir/good.py] -BAR = 0 # type: int +BAR = 0. # type: int [file subdir/mypy.ini] \[mypy] files=$MYPY_CONFIG_FILE_DIR/good.py