Skip to content

Fix PowerShell completion for PowerShell 7.4+ using Register-ArgumentCompleter #13413

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

tomaswoj-eth
Copy link

@tomaswoj-eth tomaswoj-eth commented Jun 3, 2025

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

- Added functions to retrieve completion environment variables and the current word under the cursor.
- Implemented logic to fetch installed distributions and subcommand options for improved completion.
- Introduced a PowerShell completion script to enable modern tab completion for pip commands.
- Updated the completion command to load the PowerShell script dynamically.

This update improves the user experience by providing more accurate and context-aware command completions.
@tomaswoj-eth
Copy link
Author

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.

@notatallshaw
Copy link
Member

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
Copy link
Member

pre-commit.ci autofix

@notatallshaw
Copy link
Member

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.

- Updated the `auto_complete_paths` function to improve path completion logic, ensuring it handles empty directories and filters entries based on the completion type.
- Refactored the `get_powershell_script` function to specify return type for better type hinting.
- Improved error messaging for missing shell options in the completion command.

These changes enhance the user experience by providing more accurate and context-aware command completions.
- Added a placeholder mechanism in the PowerShell completion script to dynamically determine the command name.
- Updated the `get_powershell_script` function to read the script in a zipapp-safe manner, improving compatibility.
- Enhanced the completion command to replace the placeholder with the actual program name during execution.

These changes improve the robustness and usability of the autocompletion feature in PowerShell.
@ichard26
Copy link
Member

ichard26 commented Jun 5, 2025

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!

@notatallshaw
Copy link
Member

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.

tomaswoj-eth and others added 4 commits June 7, 2025 12:05
- Added type ignore comment for `locale.LC_MESSAGES` to address potential attribute errors.
- Improved formatting and readability in the `get_powershell_script` function by restructuring the return statement.
- Updated comments in the PowerShell completion script for clarity and consistency.

These changes enhance code maintainability and improve the robustness of the PowerShell autocompletion feature.
- Updated the PowerShell completion script to clarify comments and improve formatting.
- Refactored the completion command to streamline script retrieval and enhance readability.
- Adjusted the `auto_complete_paths` function to refine path completion logic.
- Improved test cases for completion functionality, ensuring accurate results for various command scenarios.

These changes enhance the maintainability and user experience of the autocompletion feature in pip.
@notatallshaw
Copy link
Member

pre-commit.ci autofix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix/improve argument completion for PowerShell 7.4+
3 participants