pip icon indicating copy to clipboard operation
pip copied to clipboard

Fix PowerShell completion for PowerShell 7.4+ using Register-ArgumentCompleter

Open tomaswoj-eth opened this issue 6 months ago • 7 comments

Description

This PR fixes issue #12440 where pip's PowerShell completion breaks in PowerShell 7.4+ due to the use of the deprecated TabExpansion function. The solution implements a modern approach using Register-ArgumentCompleter API, which works across all PowerShell versions (5.1+, Core 6.0+, and 7.4+).

Changes Made

  1. New PowerShell Completion Script

    • Created src/pip/_internal/cli/pip-completion.ps1
    • Uses Register-ArgumentCompleter API
    • Implements cursor position support
    • Adds dynamic command name detection
    • Includes robust error handling
  2. Python Backend Updates

    • Modified completion.py to load external PowerShell script
    • Enhanced autocompletion.py with cursor position support
    • Added proper error handling and logging
    • Improved type annotations and code quality
  3. Documentation

    • Added PowerShell completion section to README
    • Updated installation instructions
    • Added compatibility notes

Key Features

  • ✅ Works with PowerShell 5.1+, Core 6.0+, and 7.4+
  • ✅ Supports cursor-aware completion
  • ✅ Handles different pip command names (pip, pip3, etc.)
  • ✅ Provides robust error handling
  • ✅ Maintains backward compatibility

Related Issues

Fixes #12440

tomaswoj-eth avatar Jun 03 '25 12:06 tomaswoj-eth

hey @ichard26 Could you please review the changes in my PR and guide me on what needs to be fixed to make the CI checks pass? I see there are some failures but would appreciate your guidance on the best way to address them.

tomaswoj-eth avatar Jun 03 '25 12:06 tomaswoj-eth

what needs to be fixed to make the CI checks pass?

The current failure is that the new .ps1 file you've added doesn't get included in the Python package because nothing in the MANIFEST.in file would add it. Given this is 1 file I recommend adding a line which adds it explicitly: include {file path}.

notatallshaw avatar Jun 03 '25 13:06 notatallshaw

pre-commit.ci autofix

notatallshaw avatar Jun 03 '25 19:06 notatallshaw

You need to fix the remaining linting errors, you can run linting locally by installing nox and running linting: https://pip.pypa.io/en/stable/development/getting-started/#running-linters, or installing pre-commit and running it directly.

notatallshaw avatar Jun 03 '25 19:06 notatallshaw

Just so we're all on the same page, I won't have time to review PRs until June 25th at the earliest. More realistically, I'll be OOO until July 6th. It is possible another maintainer will be able to review your PR in the meanwhile, but I consider that unlikely as none of the core team is well-versed in writing shell completions AFAIK. Sorry about that, but this project is entirely volunteer supported so this is unavoidable.

Thank you for your interest in improving pip!

ichard26 avatar Jun 05 '25 21:06 ichard26

All your remaining pre-commit failures is that you have lines that are too long: https://results.pre-commit.ci/run/github/1446467/1749038869.IP9MHtygQgCpn4JgPdfpwg

Here are some examples of reducing line length:

logger.warning("PowerShell completion script not found or unreadable, falling back to basic completion")

logger.warning(
    "PowerShell completion script not found or unreadable, "
    "falling back to basic completion"
)
# TypeError can be raised by importlib_resources on older Pythons if package not found

# TypeError can be raised by importlib_resources
# on older Pythons if the package is not found

And if you really need to you can turn this check off (but please have justification):

logger.warning("PowerShell completion script not found or unreadable, falling back to basic completion")  # noqa: E501

You should rerun linting/formatting after making these changes.

notatallshaw avatar Jun 05 '25 21:06 notatallshaw

pre-commit.ci autofix

notatallshaw avatar Jun 07 '25 20:06 notatallshaw

pre-commit.ci autofix

tomaswoj-eth avatar Jul 17 '25 13:07 tomaswoj-eth

@tomaswoj-eth you seem to be having problems with linting and pre-commit, let me know if you need any assistance.

notatallshaw avatar Jul 17 '25 16:07 notatallshaw

@tomaswoj-eth also, I'd like to apologise for taking so long to review this. The thing is that this is a massive PR so it's very difficult to find enough uninterrupted free time to review everything (reviewing in chunks isn't very effective for me). I still would like to review/land this when I get the chance to.

ichard26 avatar Jul 18 '25 18:07 ichard26