Skip to content

fix(remove): only clean up groups whose dependencies were actually removed - #11233

Open
lau0708 wants to merge 1 commit into
python-poetry:mainfrom
lau0708:fix/remove-from-all-groups-preserves-empty-groups
Open

lau0708 wants to merge 1 commit into
python-poetry:mainfrom
lau0708:fix/remove-from-all-groups-preserves-empty-groups

Conversation

@lau0708

@lau0708 lau0708 commented Nov 16, 2026

Copy link
Copy Markdown

Pull Request Check List

Resolves: #11232

  • Added tests for changed code.
  • Updated documentation for changed code.

Why / Problem

Without --group, poetry remove deletes dependency groups that were empty before the command and unrelated to the removed packages — the cleanup blocks in the remove-from-all-groups path (src/poetry/console/commands/remove.py, group is None branch) run for every group whose sections are empty after the loop, which is also true for already-empty unrelated groups. Both declaration styles are affected:

  • PEP 735: if not standard_section and group_name in groups_content: del groups_content[group_name]
  • legacy: if not poetry_section and "dependencies" in poetry_groups_content.get(group_name, {})

The single-group path already preserves unrelated empty groups (see test_remove_preserves_unrelated_empty_groups on main), so the remove-from-all-groups path violating that contract is a sibling-path inconsistency, not a design question.

What changed

Capture what _remove_packages actually removed per group and gate the per-group cleanup blocks on it (removed_in_group). Nothing else changes: a group whose last dependency was genuinely removed is still deleted, and the include-reference cleanup still runs for it. Deliberately not touched: the single-group path and the _remove_references_to_group cascade, which already behave as intended.

Bug fix verification

Red on main (18668c8), both parametrized variants of the new test:

E           KeyError: 'dependency-groups'   # PEP 735 variant: the whole [dependency-groups] table was deleted
E           KeyError: 'group'               # legacy variant: [tool.poetry.group] was deleted

End-to-end on a real project (dependencies = ["six"], [dependency-groups] docs = []):

$ poetry remove six --lock   # before: [dependency-groups] gone from pyproject.toml, exit 0
$ poetry remove six --lock   # after:  [dependency-groups] / docs = [] preserved

With docs = [] included by dev = [{ include-group = "docs" }]: before, both groups were deleted; after, both are preserved.

Validation / Testing

  • pytest tests/console/commands/test_remove.py: 40 passed before → 42 passed after (2 new parametrized cases).
  • pytest tests/console: 1008 passed / 3 failed, identical to the clean-main baseline on this machine (env/test_info.py::test_env_info_displays_complete_info, debug/test_info.py::test_debug_info_displays_complete_info, test_init.py::test_empty_license — pre-existing, unrelated).
  • ruff check --no-fix + ruff format --diff (0.17.4): clean.
  • mypy src/poetry/console/commands/remove.py and mypy tests/console/commands/test_remove.py: clean.

…moved

Without a group specified, poetry remove deletes empty dependency groups
from pyproject.toml even when nothing was removed from them. The cleanup
blocks in the remove-from-all-groups path were gated on the sections being
empty after the removal loop, which is also true for groups that were
empty before the command and unrelated to the removed packages. This
silently dropped unrelated [dependency-groups] entries (and empty legacy
[tool.poetry.group.*] tables) and, when those groups were included by
others, cascaded into deleting the referencing groups as well.

Gate the per-group cleanup on the packages actually removed from that
group, matching the single-group path, which already preserves unrelated
empty groups (test_remove_preserves_unrelated_empty_groups).
@sourcery-ai

sourcery-ai Bot commented Nov 16, 2026

Copy link
Copy Markdown

Linked issues

Checked at commit a1c9982

#11232 poetry remove (without --group) silently deletes unrelated empty dependency groups · 3 of 3 met

# Requirement Status Notes
1 Preserve unrelated dependency groups that were empty before poetry remove is run without --group, in both PEP 735 and legacy formats. ✅ Met Cleanup is gated on _remove_packages actually removing a package from that group; the new parametrized test covers both formats.
2 Do not delete groups that include an unrelated empty group. ✅ Met Because the unrelated group remains declared, reference cleanup is not triggered for it; the regression test checks that its including group is preserved in both formats.
3 Continue cleaning up a group and its references when its dependency was actually removed. ✅ Met The existing cleanup and reference-removal logic still runs when removed_in_group is non-empty.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the removal result is misreported, an empty group could remain in the project's configuration after the command; it can be removed manually, but reverting the code will not restore or clean up configuration already written.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

poetry remove (without --group) silently deletes unrelated empty dependency groups

1 participant