fix: suggest poetry self lock for self commands (#10536)#10723
fix: suggest poetry self lock for self commands (#10536)#10723veeceey wants to merge 1 commit intopython-poetry:mainfrom
poetry self lock for self commands (#10536)#10723Conversation
…mands When `poetry self` subcommands encounter an outdated or missing lock file, the error message now correctly suggests `poetry self lock` instead of `poetry lock`, which would operate on the wrong pyproject.toml. Fixes python-poetry#10536 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Reviewer's GuideMakes lock-file related error messages configurable so that self-related commands suggest Sequence diagram for poetry self install with custom lock command hintsequenceDiagram
actor User
participant Cli as PoetryCli
participant SelfCmd as SelfCommand
participant Inst as Installer
participant Lock as Locker
User->>Cli: run "poetry self install"
Cli->>SelfCmd: handle()
SelfCmd->>SelfCmd: reset()
SelfCmd->>Inst: set_lock_command_hint("poetry self lock")
SelfCmd->>Inst: run()
Inst->>Lock: is_locked()
alt lock file present
Inst->>Lock: is_fresh()
alt lock not fresh
Inst-->>User: error pyproject changed. Run "poetry self lock" to fix the lock file.
else lock fresh
Inst-->>User: perform installation
end
else lock file missing
Inst-->>User: error poetry.lock not found. Run "poetry self lock" to create it.
end
Sequence diagram for poetry self show using overridden lock command hintsequenceDiagram
actor User
participant Cli as PoetryCli
participant SelfShow as SelfShowCommand
participant Locker as Locker
User->>Cli: run "poetry self show"
Cli->>SelfShow: handle()
SelfShow->>Locker: is_locked()
alt lock file missing
SelfShow-->>User: Error: poetry.lock not found. Run "poetry self lock" to create it.
else lock file present
SelfShow-->>User: show package information
end
Class diagram for configurable lock command hints in installer and show commandsclassDiagram
class Installer {
-Locker _locker
-bool _update
-Iterable~NormalizedName~ _groups
-bool _skip_directory
-bool _lock
-str _lock_command_hint
-list~NormalizedName~ _whitelist
+__init__(io, env, package, locker, pool, config, installed_repository, execute_operations)
+set_locker(locker Locker) Installer
+set_lock_command_hint(hint str) Installer
+run() int
-_do_install() int
}
class Locker {
+is_locked() bool
+is_fresh() bool
+is_locked_groups_and_markers() bool
}
class ShowCommand {
<<abstract>>
+name str
+description str
+arguments list~Argument~
+handle() int
+activated_groups() set~NormalizedName~
+_lock_command_hint str
}
class SelfCommand {
+installer Installer
+system_pyproject
+handle() int
+reset() None
-_system_project_handle() int
}
class SelfShowCommand {
+description str
+_lock_command_hint str
+activated_groups() set~NormalizedName~
}
Installer --> Locker : uses
SelfCommand --> Installer : configures
SelfShowCommand --|> ShowCommand
SelfShowCommand --|> SelfCommand
ShowCommand : _lock_command_hint = "poetry lock"
SelfShowCommand : _lock_command_hint = "poetry self lock"
Installer : _lock_command_hint = "poetry lock"
Installer : set_lock_command_hint sets _lock_command_hint
SelfCommand : handle calls installer.set_lock_command_hint("poetry self lock")
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `tests/console/commands/self/test_show.py:79-83` </location>
<code_context>
assert tester.io.fetch_output().strip() == expected
+
+
+def test_show_errors_without_lock_file_suggests_self_lock(
+ tester: CommandTester,
+) -> None:
+ command = tester.command
+ assert isinstance(command, SelfCommand)
+ # Ensure the system pyproject exists but the lock file doesn't
+ system_pyproject_file = command.system_pyproject
+ system_pyproject_file.parent.mkdir(parents=True, exist_ok=True)
+ lock_path = system_pyproject_file.parent.joinpath("poetry.lock")
+ if lock_path.exists():
+ lock_path.unlink()
+
+ tester.execute()
+
+ expected = "Error: poetry.lock not found. Run `poetry self lock` to create it.\n"
+ assert tester.io.fetch_error() == expected
+ assert tester.status_code == 1
</code_context>
<issue_to_address>
**suggestion (testing):** Clarify or align the comment about ensuring the system pyproject exists
The inline comment claims the system pyproject exists, but this test only creates the parent directory and never the `system_pyproject_file` itself. That makes the test misleading and potentially brittle if later code checks for the file’s existence. Either create a minimal `system_pyproject_file` here so behavior matches the comment, or reword the comment to reflect that only the directory (and absence of `poetry.lock`) is being ensured.
```suggestion
assert isinstance(command, SelfCommand)
# Ensure the system pyproject exists but the lock file doesn't
system_pyproject_file = command.system_pyproject
system_pyproject_file.parent.mkdir(parents=True, exist_ok=True)
# Create a minimal system pyproject file so the test setup matches the comment
system_pyproject_file.write_text("")
lock_path = system_pyproject_file.parent.joinpath("poetry.lock")
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| assert isinstance(command, SelfCommand) | ||
| # Ensure the system pyproject exists but the lock file doesn't | ||
| system_pyproject_file = command.system_pyproject | ||
| system_pyproject_file.parent.mkdir(parents=True, exist_ok=True) | ||
| lock_path = system_pyproject_file.parent.joinpath("poetry.lock") |
There was a problem hiding this comment.
suggestion (testing): Clarify or align the comment about ensuring the system pyproject exists
The inline comment claims the system pyproject exists, but this test only creates the parent directory and never the system_pyproject_file itself. That makes the test misleading and potentially brittle if later code checks for the file’s existence. Either create a minimal system_pyproject_file here so behavior matches the comment, or reword the comment to reflect that only the directory (and absence of poetry.lock) is being ensured.
| assert isinstance(command, SelfCommand) | |
| # Ensure the system pyproject exists but the lock file doesn't | |
| system_pyproject_file = command.system_pyproject | |
| system_pyproject_file.parent.mkdir(parents=True, exist_ok=True) | |
| lock_path = system_pyproject_file.parent.joinpath("poetry.lock") | |
| assert isinstance(command, SelfCommand) | |
| # Ensure the system pyproject exists but the lock file doesn't | |
| system_pyproject_file = command.system_pyproject | |
| system_pyproject_file.parent.mkdir(parents=True, exist_ok=True) | |
| # Create a minimal system pyproject file so the test setup matches the comment | |
| system_pyproject_file.write_text("") | |
| lock_path = system_pyproject_file.parent.joinpath("poetry.lock") |
|
Cf #10715 You've made at least a couple of pull requests that duplicate existing PRs. You probably should say what it is you think un-fixable about the originals. |
Summary
poetry selfsubcommands (e.g.self install,self sync,self show) encounter an outdated or missing lock file, the error message now correctly suggestspoetry self lockinstead ofpoetry lockset_lock_command_hint()method toInstallerto allow customizing the lock command suggestion in error messages_lock_command_hintproperty toShowCommand(overridden inSelfShowCommand) for the "lock not found" error messageFixes #10536
Test plan
test_not_fresh_locktest continues to verify the defaultpoetry locksuggestiontest_not_fresh_lock_custom_hinttest verifies thepoetry self locksuggestion when the hint is settest_show_errors_without_lock_file_suggests_self_locktest verifiespoetry self showsuggestspoetry self lock🤖 Generated with Claude Code
Summary by Sourcery
Update lock-file related error hints so self subcommands correctly reference
poetry self lockand make the installer’s lock hint configurable.Bug Fixes:
poetry self lockwhen the self lock file is missing or outdated.Enhancements:
Tests:
poetry self locksuggestion for self commands.