@@ -284,27 +284,31 @@ def infer_python_version_and_executable(options: Options,
284
284
This function mutates options based on special_opts to infer the correct Python version and
285
285
executable to use.
286
286
"""
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
+
287
291
# Infer Python version and/or executable if one is not given
288
292
289
293
# TODO: (ethanhs) Look at folding these checks and the site packages subprocess calls into
290
294
# 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 :
292
296
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 :
294
298
raise PythonExecutableInferenceError (
295
299
'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
297
301
))
298
302
else :
299
- options .python_version = special_opts . python_version
303
+ options .python_version = python_version
300
304
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
303
307
py_exe = None
304
308
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 )
306
310
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 :
308
312
options .python_version = _python_version_from_executable (
309
313
special_opts .python_executable )
310
314
options .python_executable = special_opts .python_executable
@@ -461,7 +465,7 @@ def add_invertible_flag(flag: str,
461
465
help = 'Type check code assuming it will be running on Python x.y' ,
462
466
dest = 'special-opts:python_version' )
463
467
platform_group .add_argument (
464
- '-2' , '--py2' , dest = 'python_version' , action = 'store_const' ,
468
+ '-2' , '--py2' , dest = 'special-opts: python_version' , action = 'store_const' ,
465
469
const = defaults .PYTHON2_VERSION ,
466
470
help = "Use Python 2 mode (same as --python-version 2.7)" )
467
471
platform_group .add_argument (
0 commit comments