fix: clean up dangling references when removing empty dependency groups
Fixes #10590.
When a dependency group becomes empty after removing packages, it is removed from the configuration. However, references to this group in other groups' include-groups (Legacy) or include-group (PEP 735) were left dangling, causing errors.
This PR adds logic to clean up these references when a group is removed.
Summary by Sourcery
Clean up dependency group references when groups become empty after package removal to prevent dangling references in the configuration.
Bug Fixes:
- Remove include-group entries in PEP 735 dependency-groups that point to groups removed due to becoming empty.
- Remove include-groups entries in legacy [tool.poetry.group] sections that reference groups removed due to becoming empty.
- Ensure empty dependencies sections are removed before deleting groups, avoiding stray empty tables in the configuration.
Tests:
- Adjust remove command tests to cover cleanup of dangling group references and existence checks for remaining groups.
Reviewer's Guide
This PR ensures that when dependency groups become empty and are removed, any references to those groups in other groups’ include directives (both PEP 735 and legacy formats) are also cleaned up, and adjusts tests to match the new behavior and avoid key errors on partially-removed groups.
Class diagram for updated remove command group cleanup logic
classDiagram
class RemoveCommand {
+handle() int
-_remove_packages(packages, standard_section, poetry_section, group_name) set
-_remove_references_to_group(group_name str, content dict_str_Any) void
}
class TomlContent {
+tool_poetry_group map
+dependency_groups map
}
RemoveCommand --> TomlContent : modifies
Flow diagram for _remove_references_to_group cleanup behavior
flowchart TD
A[Start _remove_references_to_group] --> B[Input group_name, content]
B --> C{content has dependency-groups?}
C -->|No| F
C -->|Yes| D[Iterate each group_content in content.dependency-groups.values]
D --> E{group_content is list?}
E -->|No| D
E -->|Yes| G[Collect items where item is dict and item.include-group == group_name]
G --> H[Remove collected items from group_content]
H --> D
D --> F[Get poetry_content = content.tool.poetry]
F --> I{poetry_content has group?}
I -->|No| Z[End]
I -->|Yes| J[Iterate each group_content in poetry_content.group.values]
J --> K{group_content has include-groups?}
K -->|No| J
K -->|Yes| L{group_name in group_content.include-groups?}
L -->|No| P{include-groups empty?}
L -->|Yes| M[Remove group_name from include-groups]
M --> P{include-groups empty?}
P -->|Yes| Q[Delete include-groups key]
P -->|No| J
Q --> J
J --> Z[End]
File-Level Changes
| Change | Details | Files |
|---|---|---|
| Clean up dangling references when removing an empty group in the new [dependency-groups] table. |
|
src/poetry/console/commands/remove.py |
| Ensure legacy [tool.poetry.group. |
|
src/poetry/console/commands/remove.py |
| Relax and harden tests to reflect the new behavior and partial group presence during removal. |
|
tests/console/commands/test_remove.py |
Assessment against linked issues
| Issue | Objective | Addressed | Explanation |
|---|---|---|---|
| https://github.com/python-poetry/poetry/issues/10590 | When removing an empty dependency group, remove any PEP 735 [dependency-groups] entries that reference that group via include-group to avoid dangling references. |
✅ | |
| https://github.com/python-poetry/poetry/issues/10590 | When removing an empty dependency group, remove any legacy [tool.poetry.group.<name>] references to that group via include-groups to avoid dangling references. |
✅ |
Possibly linked issues
- #10590: PR implements logic to remove include-group/include-groups references when an empty dependency group is removed, fixing failures.
Tips and commands
Interacting with Sourcery
- Trigger a new review: Comment
@sourcery-ai reviewon 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 issueto create an issue from it. - Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. - Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. - Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. - Resolve all Sourcery comments: Comment
@sourcery-ai resolveon 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 dismisson 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 reviewto 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
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Feedback Addressed
I've pushed a new commit that addresses the review feedback:
1. PEP 735 empty list cleanup (Sourcery + @radoering)
The _remove_references_to_group method now cleans up groups in [dependency-groups] that become empty after their include-group references are removed. This normalizes the behavior with the legacy [tool.poetry.group.<name>] path.
Key change: Added logic to track and remove groups that become empty due to include-group cleanup, while ensuring we don't prematurely delete the target group itself (which is handled by the caller).
2. Added test for reference cleanup (@radoering)
Added test_remove_group_cleans_up_include_group_references which tests:
- When a group is removed,
include-groupreferences to it in other groups are cleaned up - Groups that become empty after their include-group references are removed are also deleted
- Tests both PEP 735 and legacy formats
3. Strengthened test assertions (Sourcery)
Updated test_remove_does_not_keep_empty_groups to explicitly assert assert "group" not in content instead of the conditional check, making the expected behavior clear.
All 23 tests in test_remove.py pass.