Skip to content

Commit 902df03

Browse files
authored
Install types to correct environment (#11457)
Fixes #11405 by using `options.python_executable` instead of `sys.executable`.
1 parent 578d389 commit 902df03

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

mypy/main.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ def main(script_path: Optional[str],
8585
fail("error: --install-types not supported with incremental mode disabled",
8686
stderr, options)
8787

88+
if options.install_types and options.python_executable is None:
89+
fail("error: --install-types not supported without python executable or site packages",
90+
stderr, options)
91+
8892
if options.install_types and not sources:
89-
install_types(options.cache_dir, formatter, non_interactive=options.non_interactive)
93+
install_types(formatter, options, non_interactive=options.non_interactive)
9094
return
9195

9296
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
@@ -95,7 +99,7 @@ def main(script_path: Optional[str],
9599
missing_pkgs = read_types_packages_to_install(options.cache_dir, after_run=True)
96100
if missing_pkgs:
97101
# Install missing type packages and rerun build.
98-
install_types(options.cache_dir, formatter, after_run=True, non_interactive=True)
102+
install_types(formatter, options, after_run=True, non_interactive=True)
99103
fscache.flush()
100104
print()
101105
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
@@ -122,8 +126,7 @@ def main(script_path: Optional[str],
122126
stdout.flush()
123127

124128
if options.install_types and not options.non_interactive:
125-
result = install_types(options.cache_dir, formatter, after_run=True,
126-
non_interactive=False)
129+
result = install_types(formatter, options, after_run=True, non_interactive=False)
127130
if result:
128131
print()
129132
print("note: Run mypy again for up-to-date results with installed types")
@@ -1151,20 +1154,21 @@ def read_types_packages_to_install(cache_dir: str, after_run: bool) -> List[str]
11511154
return [line.strip() for line in f.readlines()]
11521155

11531156

1154-
def install_types(cache_dir: str,
1155-
formatter: util.FancyFormatter,
1157+
def install_types(formatter: util.FancyFormatter,
1158+
options: Options,
11561159
*,
11571160
after_run: bool = False,
11581161
non_interactive: bool = False) -> bool:
11591162
"""Install stub packages using pip if some missing stubs were detected."""
1160-
packages = read_types_packages_to_install(cache_dir, after_run)
1163+
packages = read_types_packages_to_install(options.cache_dir, after_run)
11611164
if not packages:
11621165
# If there are no missing stubs, generate no output.
11631166
return False
11641167
if after_run and not non_interactive:
11651168
print()
11661169
print('Installing missing stub packages:')
1167-
cmd = [sys.executable, '-m', 'pip', 'install'] + packages
1170+
assert options.python_executable, 'Python executable required to install types'
1171+
cmd = [options.python_executable, '-m', 'pip', 'install'] + packages
11681172
print(formatter.style(' '.join(cmd), 'none', bold=True))
11691173
print()
11701174
if not non_interactive:

test-data/unit/cmdline.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,12 @@ pkg.py:1: error: "int" not callable
12991299
1 + 2
13001300
[out]
13011301

1302+
[case testCmdlineNonInteractiveInstallTypesNoSitePackages]
1303+
# cmd: mypy --install-types --non-interactive --no-site-packages -m pkg
1304+
[out]
1305+
error: --install-types not supported without python executable or site packages
1306+
== Return code: 2
1307+
13021308
[case testCmdlineInteractiveInstallTypesNothingToDo]
13031309
# cmd: mypy --install-types -m pkg
13041310
[file pkg.py]

0 commit comments

Comments
 (0)