Repository navigation
fix(remove): normalize group names when cleaning up include-group references - #11225
officialpm wants to merge 2 commits into
Conversation
…erences When the last dependency of a PEP 735 dependency group is removed, the group is deleted and any `include-group` references to it are cleaned up. The cleanup compared references against the literal group key, so a reference spelled differently (e.g. `lint-tools` for the key `lint_tools`) was left dangling and broke subsequent operations on the project. PEP 735 requires tools to normalize group names before comparing them, so normalize both sides of the comparison. The legacy `[tool.poetry.group]` path is intentionally left as is. Resolves python-poetry#11224.
Regression test for python-poetry#11224: removing the last package of a group must also remove `include-group` references that spell the group name differently (PEP 735 normalization).
Linked issuesChecked at commit ba0af77 #11224 poetry remove keeps an include-group whose name is spelled differently from the deleted group · 2 of 2 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="235" />
<code_context>
- isinstance(item, dict)
- and item.get("include-group") == group_name
+ isinstance(include_group, str)
+ and canonicalize_name(include_group) == normalized_group_name
):
to_remove.append(item)
</code_context>
<issue_to_address>
**Valid group references disappear**
When a group is removed while a differently spelled, canonically equivalent group remains in the other group section, `_remove_references_to_group` matches PEP 735 references by normalized name and removes references to the surviving group. Its dependents stop inheriting that group’s dependencies, and cleanup can also delete a dependent group and its contents.
Before removing a reference, check whether a canonically equivalent group still exists in the other group section and preserve references to it.
</issue_to_address>
### Comment 2
<location path="src/poetry/console/commands/remove.py" line_range="236" />
<code_context>
- and item.get("include-group") == group_name
+ isinstance(include_group, str)
+ and canonicalize_name(include_group) == normalized_group_name
):
to_remove.append(item)
</code_context>
<issue_to_address>
**Concurrent removal restores dangling references**
When two remove commands read the project before either writes, and the later command removes a different package, the second command writes its stale full-file snapshot after the first command has removed the group and normalized references, restoring the removed group and its dangling `include-group`; the successful commands leave the project broken again.
Serialize project edits or re-read and merge the project before writing so a later command cannot overwrite an earlier cleanup.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and if the normalized comparison matches a reference that should remain, the remove command will delete that include-group entry from the project’s pyproject.toml. Reverting the code prevents future removals but does not restore the edited file; the bounded change can be repaired manually or from version control.
Blocking findings: src/poetry/console/commands/remove.py:235, src/poetry/console/commands/remove.py:236
| isinstance(item, dict) | ||
| and item.get("include-group") == group_name | ||
| isinstance(include_group, str) | ||
| and canonicalize_name(include_group) == normalized_group_name |
There was a problem hiding this comment.
🟡 Medium · Valid group references disappear
When a group is removed while a differently spelled, canonically equivalent group remains in the other group section, _remove_references_to_group matches PEP 735 references by normalized name and removes references to the surviving group. Its dependents stop inheriting that group’s dependencies, and cleanup can also delete a dependent group and its contents.
Before removing a reference, check whether a canonically equivalent group still exists in the other group section and preserve references to it.
Prompt for AI agents
In `src/poetry/console/commands/remove.py` at line 235:
**Valid group references disappear**
When a group is removed while a differently spelled, canonically equivalent group remains in the other group section, `_remove_references_to_group` matches PEP 735 references by normalized name and removes references to the surviving group. Its dependents stop inheriting that group’s dependencies, and cleanup can also delete a dependent group and its contents.
Before removing a reference, check whether a canonically equivalent group still exists in the other group section and preserve references to it.| and item.get("include-group") == group_name | ||
| isinstance(include_group, str) | ||
| and canonicalize_name(include_group) == normalized_group_name | ||
| ): |
There was a problem hiding this comment.
🟡 Medium · Concurrent removal restores dangling references
When two remove commands read the project before either writes, and the later command removes a different package, the second command writes its stale full-file snapshot after the first command has removed the group and normalized references, restoring the removed group and its dangling include-group; the successful commands leave the project broken again.
Serialize project edits or re-read and merge the project before writing so a later command cannot overwrite an earlier cleanup.
Prompt for AI agents
In `src/poetry/console/commands/remove.py` at line 236:
**Concurrent removal restores dangling references**
When two remove commands read the project before either writes, and the later command removes a different package, the second command writes its stale full-file snapshot after the first command has removed the group and normalized references, restoring the removed group and its dangling `include-group`; the successful commands leave the project broken again.
Serialize project edits or re-read and merge the project before writing so a later command cannot overwrite an earlier cleanup.|
On the first point: that scenario can't occur in a project Poetry can load. On the second: two |
chrikrah
left a comment
There was a problem hiding this comment.
@officialpm #11224 is fixed for [dependency-groups], and nothing here blocks a merge as it stands. It continues what #10653 set out to do, cleaning dangling references.
# git archive ba0af77 (head) and 610d11a (merge base), CPython 3.12.14, ruff 0.17.4
$ pytest -q tests/console/commands/test_remove.py
# ba0af77: 41 passed
# 610d11a remove.py, ba0af77 tests: 1 failed, 40 passed (the new test)
$ ruff check . ; ruff format --check . ; mypy src/poetry/console/commands/remove.py # on the two changed files
# clean
# + a copy of your new test with this pyproject and `tester.execute("foo --group Lint_Tools --lock")`:
# [tool.poetry]
# package-mode = false
# [tool.poetry.group.Lint_Tools.dependencies]
# foo = "*"
# [tool.poetry.group.ci]
# include-groups = ["lint-tools"]
$ pytest -q -p no:randomly tests/console/commands/test_remove.py
# ba0af77: 1 failed, 41 passed (ci keeps include-groups = ["lint-tools"])
# 610d11a remove.py: 2 failed, 40 passed (yours and this one)
# not run: a live `poetry lock` after the remove
non-blocking: you left the legacy [tool.poetry.group] path unchanged, and my issue never ran it, so that gap is mine. poetry-core normalizes include-groups on load, and line 257 still compares literally, so the layout above fails before and after this PR. I'll send that fix with the test as a follow-up once this lands.
Restores the boundary stated in the description: python-poetry#11225 already normalizes the `[dependency-groups]` include-group comparison with the same `canonicalize_name` match, so keeping that hunk here only made the two PRs conflict in remove.py. The legacy `include-groups` fix and its regression test are unchanged, so the two PRs can now land in either order.
Pull Request Check List
Resolves: #11224
When the last dependency of a PEP 735 dependency group is removed, the group itself is deleted and any
include-groupreferences to it are cleaned up. That cleanup compared against the literal group key, so aninclude-groupspelled differently (e.g.lint-toolsfor the keylint_tools) was left dangling, breaking subsequent Poetry operations on the project.PEP 735 requires tools to normalize group names before comparing them, so this normalizes both sides of the comparison. The legacy
[tool.poetry.group]path is unchanged.