Skip to content

fix: suggest poetry self lock for self commands#10715

Merged
radoering merged 6 commits intopython-poetry:mainfrom
LouisLau-art:fix/self-lock-suggestion
Feb 28, 2026
Merged

fix: suggest poetry self lock for self commands#10715
radoering merged 6 commits intopython-poetry:mainfrom
LouisLau-art:fix/self-lock-suggestion

Conversation

@LouisLau-art
Copy link
Copy Markdown
Contributor

@LouisLau-art LouisLau-art commented Feb 6, 2026

Fixes #10536.

When an install/sync operation is run via poetry self ..., the outdated-lock message currently suggests poetry lock, which points at the current project rather than Poetry’s system project.

This changes the suggestion to poetry self lock when the installer is operating on Poetry’s system project.

Tests:

  • python -m pytest -o addopts= tests/installation/test_installer.py::test_not_fresh_lock_self_project

Summary by Sourcery

Adjust installer lock-file guidance to suggest the appropriate command when working on Poetry’s own system project.

Bug Fixes:

  • Ensure outdated-lock error messages for Poetry’s system project instruct users to run poetry self lock instead of poetry lock.

Tests:

  • Add coverage verifying that installer errors for the self project recommend poetry self lock when the lock file is not fresh.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Feb 6, 2026

Reviewer's Guide

Adjusts the installer’s outdated-lock error message to suggest poetry self lock when operating on Poetry’s own system project, and adds a test to cover this behavior.

Sequence diagram for Installer lock fix command selection

sequenceDiagram
    actor User
    participant CLI as Poetry_CLI
    participant Installer

    User->>CLI: run "poetry install" (or "poetry self install")
    CLI->>Installer: create Installer with Package
    CLI->>Installer: run()
    Installer->>Installer: _do_install()
    Installer->>Locker: is_locked()
    Locker-->>Installer: bool
    Installer->>Locker: is_fresh()
    Locker-->>Installer: bool (False when lock outdated)
    Installer->>Installer: _lock_fix_command()
    alt system project (package.name == poetry-instance)
        Installer-->>Installer: returns "poetry self lock"
    else regular project
        Installer-->>Installer: returns "poetry lock"
    end
    Installer-->>CLI: raise ValueError with suggested command
    CLI-->>User: displays error with appropriate lock command suggestion
Loading

Class diagram for updated Installer lock suggestion logic

classDiagram
    class Installer {
        - Package _package
        - Locker _locker
        - bool _update
        - bool _lock
        + set_locker(locker: Locker) Installer
        + run() int
        - _do_install() int
        - _lock_fix_command() str
    }

    class Package {
        + name: str
    }

    class Locker {
        + is_locked() bool
        + is_fresh() bool
        + is_locked_groups_and_markers() bool
    }

    Installer --> Package : uses
    Installer --> Locker : uses
Loading

File-Level Changes

Change Details Files
Route the outdated-lock error message through a helper that chooses between poetry lock and poetry self lock based on the target project.
  • Introduce a private _lock_fix_command helper on Installer that returns poetry self lock when the package name is poetry-instance, otherwise poetry lock.
  • Update the lock-staleness ValueError message in _do_install to use the new helper so the suggested command is context-aware.
src/poetry/installation/installer.py
Add a regression test ensuring poetry self operations suggest poetry self lock when the lock file is outdated.
  • Create test_not_fresh_lock_self_project that sets the installer package to poetry-instance, marks the lock as not fresh, and asserts that running the installer raises a ValueError suggesting poetry self lock.
  • Reuse existing installer/locker fixtures to minimize test duplication and focus on the new behavior.
tests/installation/test_installer.py

Assessment against linked issues

Issue Objective Addressed Explanation
#10536 When a lock file is outdated during a poetry self subcommand (operating on Poetry’s system project), the suggestion should instruct the user to run poetry self lock instead of poetry lock.
#10536 For regular (non-self) Poetry commands operating on the current project, the outdated lock file suggestion should continue to instruct the user to run poetry lock.

Possibly linked issues


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. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the 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 exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

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
Copy Markdown

@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 - I've left some high level feedback:

  • Using the magic string "poetry-instance" in _lock_fix_command tightly couples behavior to a specific package name; consider centralizing this in a constant or a helper that more explicitly identifies the system/self project to avoid brittle coupling if the name ever changes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Using the magic string "poetry-instance" in `_lock_fix_command` tightly couples behavior to a specific package name; consider centralizing this in a constant or a helper that more explicitly identifies the system/self project to avoid brittle coupling if the name ever changes.

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.

@LouisLau-art
Copy link
Copy Markdown
Contributor Author

Good point — updated to avoid the magic string by centralizing the system project name as a constant on Installer.

@radoering
Copy link
Copy Markdown
Member

Please make sure that the CI does not fail. (PRs that pass the CI are more likely to be reviewed. This also applies to your other PRs.)

@LouisLau-art
Copy link
Copy Markdown
Contributor Author

Good call. I’ll rebase and fix the failing CI jobs on this PR (and clean up the other open Poetry PRs as well) before requesting more review.

Copy link
Copy Markdown
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 think I like the approach of using the project name compared to the file location approach from the previous (stalled) PR #10568. However, this PR does not consider poetry self show, which the stalled PR did. Can you please also handle poetry self show?

Comment thread src/poetry/installation/installer.py Outdated
@LouisLau-art
Copy link
Copy Markdown
Contributor Author

Quick follow-up: acknowledged on CI quality. I’ll post an update on this PR after I rebase and get all required checks green, then request another review.

@LouisLau-art LouisLau-art force-pushed the fix/self-lock-suggestion branch from bd19ef1 to c6671a0 Compare February 24, 2026 01:08
@LouisLau-art
Copy link
Copy Markdown
Contributor Author

Thanks for the review — I pushed an update to also handle poetry self show.

What changed:

  • ShowCommand now chooses the lock-create hint based on system project context.
  • For self context, missing lock now suggests poetry self lock (instead of poetry lock).
  • Added regression test: tests/console/commands/self/test_show.py::test_self_show_errors_without_lock_file.

Validation:

  • python -m pytest -o addopts='' tests/installation/test_installer.py::test_not_fresh_lock_self_project tests/console/commands/self/test_show.py::test_self_show_errors_without_lock_file -q

Could you please take another look?

@LouisLau-art
Copy link
Copy Markdown
Contributor Author

Follow-up pushed to fix the failing pre-commit.ci check.

  • Applied ruff format on src/poetry/installation/installer.py (no logic change).
  • Re-ran targeted tests:
    • python -m pytest -o addopts= tests/installation/test_installer.py::test_not_fresh_lock_self_project tests/console/commands/self/test_show.py::test_self_show_errors_without_lock_file -q

Could you please re-check when CI completes?

@LouisLau-art
Copy link
Copy Markdown
Contributor Author

Follow-up update: branch is rebased, the constant is centralized, and all required checks are green now. Could you take another look when you have time?

@radoering radoering force-pushed the fix/self-lock-suggestion branch from 38d02e4 to 3aba07b Compare February 28, 2026 10:43
@radoering radoering changed the title fix: suggest 'poetry self lock' for self commands fix: suggest poetry self lock for self commands Feb 28, 2026
@radoering radoering changed the title fix: suggest poetry self lock for self commands fix: suggest poetry self lock for self commands Feb 28, 2026
@radoering radoering merged commit 029685c into python-poetry:main Feb 28, 2026
54 checks passed
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 31, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clarify "poetry lock" suggestion provided for poetry self subcommands

2 participants