Skip to content

Commit 9b9e7e1

Browse files
sqwishyemmatyping
authored andcommitted
Update "-2" argument to match "--python-version" (#5578)
This also allows for a Python executable to be inferred if `python_version` is set in the config file.
1 parent 70767bd commit 9b9e7e1

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

mypy/main.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,27 +284,31 @@ def infer_python_version_and_executable(options: Options,
284284
This function mutates options based on special_opts to infer the correct Python version and
285285
executable to use.
286286
"""
287+
# Check Options in case python_version is set in a config file, but prefer command
288+
# line arguments.
289+
python_version = special_opts.python_version or options.python_version
290+
287291
# Infer Python version and/or executable if one is not given
288292

289293
# TODO: (ethanhs) Look at folding these checks and the site packages subprocess calls into
290294
# one subprocess call for speed.
291-
if special_opts.python_executable is not None and special_opts.python_version is not None:
295+
if special_opts.python_executable is not None and python_version is not None:
292296
py_exe_ver = _python_version_from_executable(special_opts.python_executable)
293-
if py_exe_ver != special_opts.python_version:
297+
if py_exe_ver != python_version:
294298
raise PythonExecutableInferenceError(
295299
'Python version {} did not match executable {}, got version {}.'.format(
296-
special_opts.python_version, special_opts.python_executable, py_exe_ver
300+
python_version, special_opts.python_executable, py_exe_ver
297301
))
298302
else:
299-
options.python_version = special_opts.python_version
303+
options.python_version = python_version
300304
options.python_executable = special_opts.python_executable
301-
elif special_opts.python_executable is None and special_opts.python_version is not None:
302-
options.python_version = special_opts.python_version
305+
elif special_opts.python_executable is None and python_version is not None:
306+
options.python_version = python_version
303307
py_exe = None
304308
if not special_opts.no_executable:
305-
py_exe = _python_executable_from_version(special_opts.python_version)
309+
py_exe = _python_executable_from_version(python_version)
306310
options.python_executable = py_exe
307-
elif special_opts.python_version is None and special_opts.python_executable is not None:
311+
elif python_version is None and special_opts.python_executable is not None:
308312
options.python_version = _python_version_from_executable(
309313
special_opts.python_executable)
310314
options.python_executable = special_opts.python_executable
@@ -461,7 +465,7 @@ def add_invertible_flag(flag: str,
461465
help='Type check code assuming it will be running on Python x.y',
462466
dest='special-opts:python_version')
463467
platform_group.add_argument(
464-
'-2', '--py2', dest='python_version', action='store_const',
468+
'-2', '--py2', dest='special-opts:python_version', action='store_const',
465469
const=defaults.PYTHON2_VERSION,
466470
help="Use Python 2 mode (same as --python-version 2.7)")
467471
platform_group.add_argument(

mypy/test/testargs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ def test_executable_inference(self) -> None:
6060
assert str(e.value) == 'Python version (2, 10) did not match executable {}, got' \
6161
' version {}.'.format(sys.executable, sys.version_info[:2])
6262

63+
# If a configuration file specifies python_version, it will be set on options.
64+
# Use that to figure out how to determine a value for python_executable, when
65+
# special_opts.python_version is None.
66+
special_opts = argparse.Namespace()
67+
special_opts.python_executable = None
68+
special_opts.python_version = None
69+
special_opts.no_executable = None
70+
options = Options()
71+
options.python_executable = None
72+
options.python_version = sys.version_info[:2]
73+
infer_python_version_and_executable(options, special_opts)
74+
assert options.python_version == sys.version_info[:2]
75+
assert options.python_executable == sys.executable
76+
6377
# test that --no-site-packages will disable executable inference
6478
matching_version = base + ['--python-version={}'.format(sys_ver_str),
6579
'--no-site-packages']

0 commit comments

Comments
 (0)