Fix PowerShell completion for PowerShell 7.4+ using Register-ArgumentCompleter
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
-
New PowerShell Completion Script
- Created
src/pip/_internal/cli/pip-completion.ps1 - Uses
Register-ArgumentCompleterAPI - Implements cursor position support
- Adds dynamic command name detection
- Includes robust error handling
- Created
-
Python Backend Updates
- Modified
completion.pyto load external PowerShell script - Enhanced
autocompletion.pywith cursor position support - Added proper error handling and logging
- Improved type annotations and code quality
- Modified
-
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
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.
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}.
pre-commit.ci autofix
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.
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!
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.
pre-commit.ci autofix
pre-commit.ci autofix
@tomaswoj-eth you seem to be having problems with linting and pre-commit, let me know if you need any assistance.
@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.