Skip to content

Commit c426585

Browse files
committed
Update -2 argument dest to match --python-version.
And check config for python_version when looking for a python executable.
1 parent f9c0614 commit c426585

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

mypy/main.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,31 @@ def infer_python_version_and_executable(options: Options,
277277
This function mutates options based on special_opts to infer the correct Python version and
278278
executable to use.
279279
"""
280+
# Check Options in case python_version is set in a config file, but prefer command
281+
# line arguments.
282+
python_version = special_opts.python_version or options.python_version
283+
280284
# Infer Python version and/or executable if one is not given
281285

282286
# TODO: (ethanhs) Look at folding these checks and the site packages subprocess calls into
283287
# one subprocess call for speed.
284-
if special_opts.python_executable is not None and special_opts.python_version is not None:
288+
if special_opts.python_executable is not None and python_version is not None:
285289
py_exe_ver = _python_version_from_executable(special_opts.python_executable)
286-
if py_exe_ver != special_opts.python_version:
290+
if py_exe_ver != python_version:
287291
raise PythonExecutableInferenceError(
288292
'Python version {} did not match executable {}, got version {}.'.format(
289-
special_opts.python_version, special_opts.python_executable, py_exe_ver
293+
python_version, special_opts.python_executable, py_exe_ver
290294
))
291295
else:
292-
options.python_version = special_opts.python_version
296+
options.python_version = python_version
293297
options.python_executable = special_opts.python_executable
294-
elif special_opts.python_executable is None and special_opts.python_version is not None:
295-
options.python_version = special_opts.python_version
298+
elif special_opts.python_executable is None and python_version is not None:
299+
options.python_version = python_version
296300
py_exe = None
297301
if not special_opts.no_executable:
298-
py_exe = _python_executable_from_version(special_opts.python_version)
302+
py_exe = _python_executable_from_version(python_version)
299303
options.python_executable = py_exe
300-
elif special_opts.python_version is None and special_opts.python_executable is not None:
304+
elif python_version is None and special_opts.python_executable is not None:
301305
options.python_version = _python_version_from_executable(
302306
special_opts.python_executable)
303307
options.python_executable = special_opts.python_executable
@@ -454,7 +458,7 @@ def add_invertible_flag(flag: str,
454458
help='Type check code assuming it will be running on Python x.y',
455459
dest='special-opts:python_version')
456460
platform_group.add_argument(
457-
'-2', '--py2', dest='python_version', action='store_const',
461+
'-2', '--py2', dest='special-opts:python_version', action='store_const',
458462
const=defaults.PYTHON2_VERSION,
459463
help="Use Python 2 mode (same as --python-version 2.7)")
460464
platform_group.add_argument(

0 commit comments

Comments
 (0)