Skip to content

Install types to correct environment #11457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ def main(script_path: Optional[str],
fail("error: --install-types not supported with incremental mode disabled",
stderr, options)

if options.install_types and options.python_executable is None:
fail("error: --install-types not supported without python executable or site packages",
stderr, options)

if options.install_types and not sources:
install_types(options.cache_dir, formatter, non_interactive=options.non_interactive)
install_types(formatter, options, non_interactive=options.non_interactive)
return

res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
Expand All @@ -90,7 +94,7 @@ def main(script_path: Optional[str],
missing_pkgs = read_types_packages_to_install(options.cache_dir, after_run=True)
if missing_pkgs:
# Install missing type packages and rerun build.
install_types(options.cache_dir, formatter, after_run=True, non_interactive=True)
install_types(formatter, options, after_run=True, non_interactive=True)
fscache.flush()
print()
res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
Expand All @@ -117,8 +121,7 @@ def main(script_path: Optional[str],
stdout.flush()

if options.install_types and not options.non_interactive:
result = install_types(options.cache_dir, formatter, after_run=True,
non_interactive=False)
result = install_types(formatter, options, after_run=True, non_interactive=False)
if result:
print()
print("note: Run mypy again for up-to-date results with installed types")
Expand Down Expand Up @@ -1146,20 +1149,21 @@ def read_types_packages_to_install(cache_dir: str, after_run: bool) -> List[str]
return [line.strip() for line in f.readlines()]


def install_types(cache_dir: str,
formatter: util.FancyFormatter,
def install_types(formatter: util.FancyFormatter,
options: Options,
*,
after_run: bool = False,
non_interactive: bool = False) -> bool:
"""Install stub packages using pip if some missing stubs were detected."""
packages = read_types_packages_to_install(cache_dir, after_run)
packages = read_types_packages_to_install(options.cache_dir, after_run)
if not packages:
# If there are no missing stubs, generate no output.
return False
if after_run and not non_interactive:
print()
print('Installing missing stub packages:')
cmd = [sys.executable, '-m', 'pip', 'install'] + packages
assert options.python_executable, 'Python executable required to install types'
cmd = [options.python_executable, '-m', 'pip', 'install'] + packages
print(formatter.style(' '.join(cmd), 'none', bold=True))
print()
if not non_interactive:
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,12 @@ pkg.py:1: error: "int" not callable
1 + 2
[out]

[case testCmdlineNonInteractiveInstallTypesNoSitePackages]
# cmd: mypy --install-types --non-interactive --no-site-packages -m pkg
[out]
error: --install-types not supported without python executable or site packages
== Return code: 2

[case testCmdlineInteractiveInstallTypesNothingToDo]
# cmd: mypy --install-types -m pkg
[file pkg.py]
Expand Down