Repository navigation
Conversation
…endency from main
Linked issuesChecked at commit 0349a22 #11236 poetry remove leaves a dangling reference in [tool.poetry.extras] when removing an optional dependency, breaking poetry check · 1 of 1 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/poetry/console/commands/remove.py" line_range="105" />
<code_context>
)
+ removed |= removed_in_group
+ if group_name == MAIN_GROUP and removed_in_group:
+ self._remove_extras_references(removed_in_group, poetry_content)
if group_name != MAIN_GROUP:
if (
</code_context>
<issue_to_address>
**Lock retains removed extra references**
When a removed main dependency is referenced by an extra and the command regenerates the lock, `_remove_extras_references` edits only `poetry_content`, leaving `self.poetry.package.extras` stale, so the installer writes the removed dependency into `poetry.lock` despite its removal from `pyproject.toml`.
Update `self.poetry.package.extras` as well as `poetry_content` when removing extra references.
</issue_to_address>
### Comment 2
<location path="src/poetry/console/commands/remove.py" line_range="234" />
<code_context>
+ remaining = [
+ requirement
+ for requirement in requirements
+ if canonicalize_name(requirement) not in removed_names
+ ]
+ if len(remaining) == len(requirements):
</code_context>
<issue_to_address>
**Constrained extras references remain**
When an extra contains a valid constrained requirement such as `foo (>=1)`, `canonicalize_name(requirement)` canonicalizes the entire constrained string instead of extracting its package name, so it does not match the removed package; the dangling reference remains and `poetry check` fails.
Extract the dependency name from each requirement before canonicalizing it.
Also at `src/poetry/console/commands/remove.py:235`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and if the name matching is wrong, removing a dependency could also delete its entry from one or more extras in pyproject.toml; reverting the code will not restore those edits, but the affected entries can be added back manually.
Blocking findings: src/poetry/console/commands/remove.py:105, src/poetry/console/commands/remove.py:234
| ) | ||
| removed |= removed_in_group | ||
| if group_name == MAIN_GROUP and removed_in_group: | ||
| self._remove_extras_references(removed_in_group, poetry_content) |
There was a problem hiding this comment.
🟡 Medium · Lock retains removed extra references
When a removed main dependency is referenced by an extra and the command regenerates the lock, _remove_extras_references edits only poetry_content, leaving self.poetry.package.extras stale, so the installer writes the removed dependency into poetry.lock despite its removal from pyproject.toml.
Update self.poetry.package.extras as well as poetry_content when removing extra references.
Prompt for AI agents
In `src/poetry/console/commands/remove.py` at line 105:
**Lock retains removed extra references**
When a removed main dependency is referenced by an extra and the command regenerates the lock, `_remove_extras_references` edits only `poetry_content`, leaving `self.poetry.package.extras` stale, so the installer writes the removed dependency into `poetry.lock` despite its removal from `pyproject.toml`.
Update `self.poetry.package.extras` as well as `poetry_content` when removing extra references.| remaining = [ | ||
| requirement | ||
| for requirement in requirements | ||
| if canonicalize_name(requirement) not in removed_names |
There was a problem hiding this comment.
🟠 High · Constrained extras references remain
When an extra contains a valid constrained requirement such as foo (>=1), canonicalize_name(requirement) canonicalizes the entire constrained string instead of extracting its package name, so it does not match the removed package; the dangling reference remains and poetry check fails.
Extract the dependency name from each requirement before canonicalizing it.
Also at src/poetry/console/commands/remove.py:235.
Prompt for AI agents
In `src/poetry/console/commands/remove.py` at line 234:
**Constrained extras references remain**
When an extra contains a valid constrained requirement such as `foo (>=1)`, `canonicalize_name(requirement)` canonicalizes the entire constrained string instead of extracting its package name, so it does not match the removed package; the dangling reference remains and `poetry check` fails.
Extract the dependency name from each requirement before canonicalizing it.
Also at `src/poetry/console/commands/remove.py:235`.
Resolves: #11236
Pull Request Check List
removenow only cleans up references that can no longer be valid)Why / Problem
poetry remove <pkg>removes the package from[tool.poetry.dependencies]but leaves entries in[tool.poetry.extras]that reference it. Since poetry-core validates that every extras entry resolves to a main dependency (Cannot find dependency "..." for extra "..." in main dependencies.), the project ends up in a state wherepoetry checkexits 1 — whileremoveitself exited 0:This is the same "dangling reference left behind by remove" family as the
include-groupcleanups (#11225, #11230, #11233), on the[tool.poetry.extras]side.What changed
src/poetry/console/commands/remove.py: in the no---groupbranch, after removing packages from the main group, drop their references from[tool.poetry.extras](matched by canonicalized name). Extras that become empty are removed, and the extras section is removed if it becomes empty — mirroring how this command already cleans up emptied group sections.tests/console/commands/test_remove.py: two regression tests (partial cleanup keeps remaining extras; fully emptied section is dropped).Scope note: this does not add extras management to the CLI (that was #10726, and the maintainer feedback there was to edit the pyproject manually). Here nothing is added — we only clean up a reference that is invalid by definition once the dependency it points to is gone, so that
removedoes not leave the project in a state that fails its own validation.Validation
Red on base (
8c3ddc3), green on this branch, control assertion passes on both sides: