Skip to content

fix: clean up dangling references when removing empty dependency groups#10653

Merged
radoering merged 5 commits intopython-poetry:mainfrom
JaviMaligno:fix/dangling-group-references-10590
Dec 20, 2025
Merged

fix: clean up dangling references when removing empty dependency groups#10653
radoering merged 5 commits intopython-poetry:mainfrom
JaviMaligno:fix/dangling-group-references-10590

Conversation

@JaviMaligno
Copy link
Contributor

@JaviMaligno JaviMaligno commented Dec 10, 2025

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.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 10, 2025

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
Loading

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]
Loading

File-Level Changes

Change Details Files
Clean up dangling references when removing an empty group in the new [dependency-groups] table.
  • Introduce a helper method to remove references to a group from PEP 735 dependency-groups and legacy tool.poetry.group include-groups declarations.
  • Iterate over dependency-groups entries and remove items whose include-group matches the removed group name, skipping non-list entries.
  • After removal, leave dependency-groups entries in place; only the group being removed is deleted from groups_content.
src/poetry/console/commands/remove.py
Ensure legacy [tool.poetry.group.] groups clean up both dependencies and cross-group references when they become empty.
  • Capture the group’s dependencies mapping before removal to determine whether it is empty after package removal.
  • If the dependencies section becomes empty, delete only the dependencies key first, then, if the entire group table is empty, remove the group and call the helper to strip references to it from other groups.
  • Apply the same reference cleanup when groups in the top-level [dependency-groups] table become empty and are removed.
src/poetry/console/commands/remove.py
Relax and harden tests to reflect the new behavior and partial group presence during removal.
  • Guard the assertion about foo being absent from bar’s dependencies with checks that group and bar still exist in the content structure.
  • Use nested get calls for the baz assertion to avoid KeyError if group or bar was removed while still confirming that baz is not listed as a dependency.
  • Keep existing structural assertions that the bar group section is fully removed from the serialized TOML.
tests/console/commands/test_remove.py

Assessment against linked issues

Issue Objective Addressed Explanation
#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.
#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


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

@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 there - I've reviewed your changes - here's some feedback:

  • In _remove_references_to_group, the PEP 735 branch leaves empty lists in dependency-groups after removing all include-group entries, whereas the legacy branch deletes empty include-groups; consider normalizing behavior by also cleaning up now-empty lists or empty group entries for the PEP 735 path.
  • When removing legacy group dependencies in handle, you now special-case deletion of the dependencies key and then the group; it might be clearer and less error-prone to pull this into a small helper or to centralize the “group became empty” logic so the removal path for PEP 735 and legacy groups is handled consistently.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_remove_references_to_group`, the PEP 735 branch leaves empty lists in `dependency-groups` after removing all `include-group` entries, whereas the legacy branch deletes empty `include-groups`; consider normalizing behavior by also cleaning up now-empty lists or empty group entries for the PEP 735 path.
- When removing legacy group dependencies in `handle`, you now special-case deletion of the `dependencies` key and then the group; it might be clearer and less error-prone to pull this into a small helper or to centralize the “group became empty” logic so the removal path for PEP 735 and legacy groups is handled consistently.

## Individual Comments

### Comment 1
<location> `tests/console/commands/test_remove.py:445-448` </location>
<code_context>
         assert "bar" not in pyproject.get("dependency-groups", {})
         assert "dependency-groups" not in pyproject
     else:
-        assert "foo" not in content["group"]["bar"]["dependencies"]
-        assert "baz" not in content["group"]["bar"]["dependencies"]
+        if "group" in content and "bar" in content["group"]:
+            assert "foo" not in content["group"]["bar"]["dependencies"]
+        
+        assert "baz" not in content.get("group", {}).get("bar", {}).get("dependencies", {})
         content = cast("TOMLDocument", content)
         assert "[tool.poetry.group.bar]" not in content.as_string()
</code_context>

<issue_to_address>
**issue (testing):** The new conditional weakens the assertion and may hide regressions in how groups are cleaned up.

With the new guards, we no longer assert anything if `group.bar` is removed: the `foo` assertion is skipped, and the `baz` assertion runs against an empty dict. The test no longer states whether we expect `bar` to be removed entirely or to remain without those dependencies.

To keep the expectation explicit, consider asserting directly on the final structure, for example:
- Assert `"bar" not in content.get("group", {})` when the group should be removed, or
- Assert `"bar" in content["group"]` and that its `"dependencies"` key is missing/empty when it should remain.

This way the test will fail if group cleanup behavior changes, instead of silently passing.
</issue_to_address>

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.

Copy link
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.

Can you add tests for the new code please?

Further, if sourcery is right (I did not check), this should probably be addressed:

  • In _remove_references_to_group, the PEP 735 branch leaves empty lists in dependency-groups after removing all include-group entries, whereas the legacy branch deletes empty include-groups; consider normalizing behavior by also cleaning up now-empty lists or empty group entries for the PEP 735 path.

JaviMaligno pushed a commit to JaviMaligno/poetry that referenced this pull request Dec 16, 2025
Address PR python-poetry#10653 feedback:
- Clean up PEP 735 dependency-groups that become empty after their
  include-group references are removed (normalizes with legacy behavior)
- Add test for include-group reference cleanup when groups are removed
- Strengthen test assertions for empty group removal

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@JaviMaligno
Copy link
Contributor Author

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-group references 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.

JavierSKYC and others added 5 commits December 20, 2025 08:05
Address PR python-poetry#10653 feedback:
- Clean up PEP 735 dependency-groups that become empty after their
  include-group references are removed (normalizes with legacy behavior)
- Add test for include-group reference cleanup when groups are removed
- Strengthen test assertions for empty group removal

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@radoering radoering force-pushed the fix/dangling-group-references-10590 branch from 6226b86 to b849db6 Compare December 20, 2025 09:59
@radoering radoering merged commit 03dcaef into python-poetry:main Dec 20, 2025
54 checks passed
@github-actions
Copy link

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 Jan 20, 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.

Dangling include-group references after removing empty dependency groups causes poetry command failures

3 participants