Skip to content

fix(remove): normalize group names when cleaning up include-group references - #11225

Open
officialpm wants to merge 2 commits into
python-poetry:mainfrom
officialpm:fix-remove-include-group-cleanup
Open

officialpm wants to merge 2 commits into
python-poetry:mainfrom
officialpm:fix-remove-include-group-cleanup

Conversation

@officialpm

Copy link
Copy Markdown

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-group references to it are cleaned up. That cleanup compared against the literal group key, so an include-group spelled differently (e.g. lint-tools for the key lint_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.

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

…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).
@sourcery-ai

sourcery-ai Bot commented Nov 10, 2026

Copy link
Copy Markdown

Linked issues

Checked at commit ba0af77

#11224 poetry remove keeps an include-group whose name is spelled differently from the deleted group · 2 of 2 met

# Requirement Status Notes
1 Remove include-group references to a deleted PEP 735 group even when the reference uses a different spelling. ✅ Met The cleanup compares canonicalized group names, and the regression test verifies that lint-tools is removed as a reference to lint_tools.
2 Leave the project usable after removing the last dependency from the group. ✅ Met The regression test confirms the remaining dependency-group contents and reloads the project successfully.

@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 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


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

isinstance(item, dict)
and item.get("include-group") == group_name
isinstance(include_group, str)
and canonicalize_name(include_group) == normalized_group_name

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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
):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

@officialpm

Copy link
Copy Markdown
Author

On the first point: that scenario can't occur in a project Poetry can load. include-group resolution does look up groups across both sections, but poetry-core validates the project when it is loaded and rejects canonically duplicate group names ("Duplicate dependency group name after normalization"), so a differently spelled group in [tool.poetry.group] cannot coexist with the removed [dependency-groups] entry. The literal-name guard on the caller side already preserves references when the group survives in the other section under the same spelling (covered by test_remove_preserves_references_to_groups_still_defined_in_other_section).

On the second: two poetry remove processes racing on a full-file write is pre-existing behavior of every pyproject mutation, not something this cleanup introduces; if it matters in practice it deserves its own issue.

@chrikrah chrikrah 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.

@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.

lau0708 added a commit to lau0708/poetry that referenced this pull request Nov 24, 2026
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.

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 keeps an include-group whose name is spelled differently from the deleted group

2 participants