Skip to content

Support pip's --user option in mypy's --install-types #16606

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

Closed
wants to merge 6 commits into from
Closed
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
7 changes: 7 additions & 0 deletions docs/source/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,13 @@ Miscellaneous
stub packages were found, they are installed and then another run
is performed.

.. option:: --user

When used together with :option:`--install-types <mypy
--install-types>`, this causes mypy to install all suggested stub
packages using pip and installing them to the user's local python
package install location by passing `--user` to `pip` command.

.. option:: --junit-xml JUNIT_XML

Causes mypy to generate a JUnit XML test result document with
Expand Down
14 changes: 13 additions & 1 deletion mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def main(
if options.non_interactive and not options.install_types:
fail("error: --non-interactive is only supported with --install-types", stderr, options)

if options.user and not options.install_types:
fail("error: --user is only supported with --install-types", stderr, options)

if options.install_types and not options.incremental:
fail(
"error: --install-types not supported with incremental mode disabled", stderr, options
Expand Down Expand Up @@ -1094,6 +1097,14 @@ def add_invertible_flag(
group=other_group,
inverse="--interactive",
)
other_group.add_argument(
"--user",
action="store_true",
help=(
"Install stubs to the user's local python package install "
+ "location when used together with --install-types"
),
)

if server_options:
# TODO: This flag is superfluous; remove after a short transition (2018-03-16)
Expand Down Expand Up @@ -1561,7 +1572,8 @@ def install_types(
print()
print("Installing missing stub packages:")
assert options.python_executable, "Python executable required to install types"
cmd = [options.python_executable, "-m", "pip", "install"] + packages
pip_options = ["--user"] if options.user else []
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If more pip options are supported in the future, this could go into a proper derive_pip_options function or similar

cmd = [options.python_executable, "-m", "pip", "install"] + pip_options + packages
print(formatter.style(" ".join(cmd), "none", bold=True))
print()
if not non_interactive:
Expand Down
2 changes: 2 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ def __init__(self) -> None:
# Install missing stub packages in non-interactive mode (don't prompt for
# confirmation, and don't show any errors)
self.non_interactive = False
# Install missing stub packages into user's local package installation directory
self.user = False
# When we encounter errors that may cause many additional errors,
# skip most errors after this many messages have been reported.
# -1 means unlimited.
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,25 @@ pkg.py:1: error: "int" not callable
error: --install-types not supported without python executable or site packages
== Return code: 2

[case testCmdlineUserOptionWithoutInstallTypes]
# cmd: mypy --user -m pkg
[out]
error: --user is only supported with --install-types
== Return code: 2

[case testCmdlineUserOptionInstallTypesNothingToDo]
# cmd: mypy --install-types --user -m pkg
[file pkg.py]
1()
[out]
pkg.py:1: error: "int" not callable

[case testCmdlineUserOptionInstallTypesNothingToDoNoError]
# cmd: mypy --install-types --user -m pkg
[file pkg.py]
1 + 2
[out]

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