From c4265850a62ea39c0486a63f5c871a4af1e05957 Mon Sep 17 00:00:00 2001 From: sqwishy Date: Wed, 5 Sep 2018 21:10:33 -0700 Subject: [PATCH 1/3] Update -2 argument dest to match --python-version. And check config for python_version when looking for a python executable. --- mypy/main.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 6824bdd29df7e..27d7bf4610219 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -277,27 +277,31 @@ def infer_python_version_and_executable(options: Options, This function mutates options based on special_opts to infer the correct Python version and executable to use. """ + # Check Options in case python_version is set in a config file, but prefer command + # line arguments. + python_version = special_opts.python_version or options.python_version + # Infer Python version and/or executable if one is not given # TODO: (ethanhs) Look at folding these checks and the site packages subprocess calls into # one subprocess call for speed. - if special_opts.python_executable is not None and special_opts.python_version is not None: + if special_opts.python_executable is not None and python_version is not None: py_exe_ver = _python_version_from_executable(special_opts.python_executable) - if py_exe_ver != special_opts.python_version: + if py_exe_ver != python_version: raise PythonExecutableInferenceError( 'Python version {} did not match executable {}, got version {}.'.format( - special_opts.python_version, special_opts.python_executable, py_exe_ver + python_version, special_opts.python_executable, py_exe_ver )) else: - options.python_version = special_opts.python_version + options.python_version = python_version options.python_executable = special_opts.python_executable - elif special_opts.python_executable is None and special_opts.python_version is not None: - options.python_version = special_opts.python_version + elif special_opts.python_executable is None and python_version is not None: + options.python_version = python_version py_exe = None if not special_opts.no_executable: - py_exe = _python_executable_from_version(special_opts.python_version) + py_exe = _python_executable_from_version(python_version) options.python_executable = py_exe - elif special_opts.python_version is None and special_opts.python_executable is not None: + elif python_version is None and special_opts.python_executable is not None: options.python_version = _python_version_from_executable( special_opts.python_executable) options.python_executable = special_opts.python_executable @@ -454,7 +458,7 @@ def add_invertible_flag(flag: str, help='Type check code assuming it will be running on Python x.y', dest='special-opts:python_version') platform_group.add_argument( - '-2', '--py2', dest='python_version', action='store_const', + '-2', '--py2', dest='special-opts:python_version', action='store_const', const=defaults.PYTHON2_VERSION, help="Use Python 2 mode (same as --python-version 2.7)") platform_group.add_argument( From 03bbb3bad8d10d583b075c0ba17195a3e2ab4b0f Mon Sep 17 00:00:00 2001 From: sqwishy Date: Thu, 6 Sep 2018 19:39:30 -0700 Subject: [PATCH 2/3] Add test for #5578 Testing that infer_python_version_and_executable uses options.python_python. --- mypy/test/testargs.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mypy/test/testargs.py b/mypy/test/testargs.py index bccbafaf4c8bf..5a22d0e3a4e40 100644 --- a/mypy/test/testargs.py +++ b/mypy/test/testargs.py @@ -60,6 +60,17 @@ def test_executable_inference(self) -> None: assert str(e.value) == 'Python version (2, 10) did not match executable {}, got' \ ' version {}.'.format(sys.executable, sys.version_info[:2]) + # If a configuration file specifies python_version, it will be set on options. + # Use that when python_version isn't in special_opts. + special_opts = argparse.Namespace() + special_opts.python_executable = None + special_opts.python_version = None + special_opts.no_executable = None + options = Options() + options.python_version = (2, 10) + with pytest.raises(PythonExecutableInferenceError) as e: + infer_python_version_and_executable(options, special_opts) + # test that --no-site-packages will disable executable inference matching_version = base + ['--python-version={}'.format(sys_ver_str), '--no-site-packages'] From 1e56332574f474617801d206295c4fadfd200785 Mon Sep 17 00:00:00 2001 From: sqwishy Date: Thu, 6 Sep 2018 20:02:28 -0700 Subject: [PATCH 3/3] Avoid assert raises to test python inference. Instead of giving infer_python_version_and_executable() a silly value and checking for an exception, set options.python_executable to None and make sure it's not None after the call. --- mypy/test/testargs.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mypy/test/testargs.py b/mypy/test/testargs.py index 5a22d0e3a4e40..175e3e46c3006 100644 --- a/mypy/test/testargs.py +++ b/mypy/test/testargs.py @@ -61,15 +61,18 @@ def test_executable_inference(self) -> None: ' version {}.'.format(sys.executable, sys.version_info[:2]) # If a configuration file specifies python_version, it will be set on options. - # Use that when python_version isn't in special_opts. + # Use that to figure out how to determine a value for python_executable, when + # special_opts.python_version is None. special_opts = argparse.Namespace() special_opts.python_executable = None special_opts.python_version = None special_opts.no_executable = None options = Options() - options.python_version = (2, 10) - with pytest.raises(PythonExecutableInferenceError) as e: - infer_python_version_and_executable(options, special_opts) + options.python_executable = None + options.python_version = sys.version_info[:2] + infer_python_version_and_executable(options, special_opts) + assert options.python_version == sys.version_info[:2] + assert options.python_executable == sys.executable # test that --no-site-packages will disable executable inference matching_version = base + ['--python-version={}'.format(sys_ver_str),