Skip to content

Commit 4518bab

Browse files
committed
Allow setting python_version/executable from config file
This fixes python#5620
1 parent 84229e7 commit 4518bab

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

mypy/main.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,19 +264,25 @@ def infer_python_version_and_executable(options: Options,
264264

265265
# TODO: (ethanhs) Look at folding these checks and the site packages subprocess calls into
266266
# one subprocess call for speed.
267-
if special_opts.python_executable is not None and special_opts.python_version is not None:
268-
options.python_version = special_opts.python_version
269-
options.python_executable = special_opts.python_executable
270-
elif special_opts.python_executable is None and special_opts.python_version is not None:
271-
options.python_version = special_opts.python_version
267+
268+
# Use the command line specified python_version/executable, or fall back to one set in the
269+
# config file
270+
python_version = special_opts.python_version or options.python_version
271+
python_executable = special_opts.python_executable or options.python_executable
272+
273+
if python_executable is not None and python_version is not None:
274+
options.python_version = python_version
275+
options.python_executable = python_executable
276+
elif python_executable is None and python_version is not None:
277+
options.python_version = python_version
272278
py_exe = None
273279
if not special_opts.no_executable:
274-
py_exe = _python_executable_from_version(special_opts.python_version)
280+
py_exe = _python_executable_from_version(python_version)
275281
options.python_executable = py_exe
276-
elif special_opts.python_version is None and special_opts.python_executable is not None:
282+
elif python_version is None and python_executable is not None:
277283
options.python_version = _python_version_from_executable(
278-
special_opts.python_executable)
279-
options.python_executable = special_opts.python_executable
284+
python_executable)
285+
options.python_executable = python_executable
280286

281287

282288
HEADER = """%(prog)s [-h] [-v] [-V] [more options; see below]

mypy/test/testargs.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,22 @@ def test_executable_inference(self) -> None:
5353
_, options = process_options(matching_version)
5454
assert options.python_version == sys.version_info[:2]
5555
assert options.python_executable is None
56+
57+
# Test setting python_version/executable from config file
58+
special_opts = argparse.Namespace()
59+
special_opts.python_executable = None
60+
special_opts.python_version = None
61+
special_opts.no_executable = None
62+
options = Options()
63+
# first test inferring executable from version
64+
options.python_executable = None
65+
options.python_version = sys.version_info[:2]
66+
infer_python_version_and_executable(options, special_opts)
67+
assert options.python_version == sys.version_info[:2]
68+
assert options.python_executable == sys.executable
69+
# then test inferring version from executable
70+
options.python_version = None
71+
options.python_executable = sys.executable
72+
infer_python_version_and_executable(options, special_opts)
73+
assert options.python_version == sys.version_info[:2]
74+
assert options.python_executable == sys.executable

0 commit comments

Comments
 (0)