Skip to content

Commit

Permalink
pip install now autocompletes paths
Browse files Browse the repository at this point in the history
Previously, `pip install <tab>` did not autocomplete anything. With this
patch, it autocompletes paths to e.g. wheels, sdists, and directories.

Fixes pypa#10646.
  • Loading branch information
rouge8 committed Nov 24, 2021
1 parent 5c13274 commit 4d4822f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/10646.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pip install <tab>`` autocompletes paths.
8 changes: 8 additions & 0 deletions src/pip/_internal/cli/autocompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def autocomplete() -> None:
print(dist)
sys.exit(1)

should_list_installables = (
not current.startswith("-") and subcommand_name == "install"
)
if should_list_installables:
for path in auto_complete_paths(current, "path"):
print(path)
sys.exit(1)

subcommand = create_command(subcommand_name)

for opt in subcommand.parser.option_list_all:
Expand Down
22 changes: 20 additions & 2 deletions tests/functional/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ def test_completion_not_files_after_option(
) -> None:
"""
Test not getting completion files after options which not applicable
(e.g. ``pip install``)
(e.g. ``pip wheel``)
"""
res, env = autocomplete(
words=("pip install r"),
words=("pip wheel r"),
cword="2",
cwd=data.completion_paths,
)
Expand All @@ -256,6 +256,24 @@ def test_completion_not_files_after_option(
), "autocomplete function completed <dir> when it should not complete"


def test_pip_install_complete_files(
autocomplete: DoAutocomplete, data: TestData
) -> None:
"""``pip install`` autocompletes wheel and sdist files."""
res, env = autocomplete(
words=("pip install r"),
cword="2",
cwd=data.completion_paths,
)
assert all(
out in res.stdout
for out in (
"requirements.txt",
"resources",
)
), "autocomplete function could not complete <path>"


@pytest.mark.parametrize("cl_opts", ["-U", "--user", "-h"])
def test_completion_not_files_after_nonexpecting_option(
autocomplete: DoAutocomplete, data: TestData, cl_opts: str
Expand Down

0 comments on commit 4d4822f

Please sign in to comment.