Skip to content
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

feat(env): prefer embedded pip for poetry use #10091

Closed
wants to merge 1 commit into from

Conversation

abn
Copy link
Member

@abn abn commented Jan 21, 2025

Presently, when Poetry dispatches pip commands for uninstallation, this uses the pip version installed in the active virtual environment. This can potentially cause issues like #10089.

This change, switches to preferring the embedded pip wheel and makes preferring the in-env version the exception, while preferring it when a user uses poetry run pip.

See-also: #4011

Summary by Sourcery

Bug Fixes:

  • Fixes potential issues caused by using the virtual environment's pip version when uninstalling packages via Poetry.

Presently, when Poetry dispatches pip commands for uninstallation, this
uses the pip version installed in the active virtual environment. This
can potentially cause issues like python-poetry#10089.

This change, switches to preferring the embedded pip wheel and makes
preferring the in-env version the exception, while preferring it when a
user uses `poetry run pip`.
@abn abn added the area/venv Related to virtualenv management label Jan 21, 2025
@abn abn requested a review from a team January 21, 2025 19:44
Copy link

sourcery-ai bot commented Jan 21, 2025

Reviewer's Guide by Sourcery

This pull request modifies how Poetry invokes pip commands, preferring the embedded pip wheel over the in-environment version, except when the user explicitly uses poetry run pip. This change aims to avoid potential issues caused by using the in-environment pip version.

Sequence diagram for Poetry's pip command execution flow

sequenceDiagram
    participant Poetry
    participant BaseEnv
    participant PipCommand

alt prefer_in_env=True and pip exists in environment
    Poetry->>BaseEnv: get_pip_command(prefer_in_env=true)
    BaseEnv->>PipCommand: Execute [python, -m, pip]
else Default case or pip not in environment
    Poetry->>BaseEnv: get_pip_command(prefer_in_env=false)
    BaseEnv->>PipCommand: Execute [python, embedded_pip]
end
Loading

Flow diagram for pip command selection logic

flowchart TD
    A[Start] --> B{prefer_in_env?}
    B -->|Yes| C{Pip exists in env?}
    C -->|Yes| D[Use in-env pip]
    C -->|No| E[Use embedded pip]
    B -->|No| E
    D --> F[Execute pip command]
    E --> F
    F --> G[End]
Loading

File-Level Changes

Change Details Files
Modified the logic for determining which pip executable to use.
  • The get_pip_command method now takes a prefer_in_env boolean argument instead of embedded.
  • The method now defaults to using the embedded pip, unless prefer_in_env is set to True and an in-environment pip is available.
  • The get_command_from_bin method now passes prefer_in_env=True to get_pip_command when the requested binary is pip.
src/poetry/utils/env/base_env.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @abn - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dimbleby
Copy link
Contributor

cf #6060, #6062

really what we want is to stop using pip altogether, iirc the remaining cases are:

  • uninstall
  • certain cases where the local project has a build step

uninstall is clearly the more important, there is discussion in #9263

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

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

I agree with dimbleby that using the embedded pip is known to cause issues and should only be done as fallback. Actually, we made sure that pip was updated before all other packages in the past. This got removed accidentally in #9392. I think we have to revert the part of #9392.

@abn
Copy link
Member Author

abn commented Jan 21, 2025

Ah #6060 and #6062 were what I was missing.

I agree that we should really just drop the uninstall case. The build step is already using an isolated builder now in main.

I'll see if I can implement some uninstaller based on pip's logic as a starter. If it's easy enough we can avoid reverting part of #9392.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/venv Related to virtualenv management
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants