Skip to content

fix(remove): match legacy include-groups by normalized name - #11230

Open
lau0708 wants to merge 3 commits into
python-poetry:mainfrom
lau0708:fix/remove-legacy-include-group-normalization
Open

lau0708 wants to merge 3 commits into
python-poetry:mainfrom
lau0708:fix/remove-legacy-include-group-normalization

Conversation

@lau0708

@lau0708 lau0708 commented Nov 15, 2026 •

Copy link
Copy Markdown

Related: #11224 — this is the legacy-section half; #11225 covers the PEP 735 half.

Problem

poetry remove deletes a dependency group that became empty and then cleans up references to it in other groups (RemoveCommand._remove_references_to_group, src/poetry/console/commands/remove.py:219). Both branches compared group names literally, but PEP 735 says group names must be normalized before comparison, and Poetry normalizes them everywhere else (poetry-core's _configure_package_dependency_groups does canonicalize_name() on both the group key and its include-group / include-groups entries). So a group key and a reference to it can legitimately differ in spelling.

With this pyproject.toml:

[tool.poetry.group.lint_tools.dependencies]
six = ">=1.16"

[tool.poetry.group.ci]
include-groups = ["lint-tools"]
[tool.poetry.group.ci.dependencies]
idna = ">=3"

poetry remove --group lint_tools six --lock exits 0 and drops the lint_tools group, but leaves include-groups = ["lint-tools"] behind. The project is now unusable, and the failure surfaces on the next command:

$ poetry lock
Group 'ci' includes group 'lint-tools' which is not defined.
$ echo $?
1

What changed

src/poetry/console/commands/remove.py, legacy [tool.poetry.group.<name>] include-groups branch only:

  • compare canonicalize_name() of both sides instead of ==;
  • remove the entry as it is actually spelled in pyproject.toml (the stored name may differ from the --group value), instead of list.remove(group_name).

The PEP 735 [dependency-groups] branch is deliberately untouched — that is #11225. The two hunks are disjoint, so they can land in either order; I'm happy to fold this into #11225 instead if you prefer a single PR.

Out of scope: --group option lookup itself (poetry_groups_content.get(group)), which is #11192 / #11193.

Verification

Red on base (commit 037fcbe's parent), new test test_remove_cleans_up_differently_spelled_legacy_include_group:

E  AssertionError: assert {'ci': {'incl...bar': '>=1'}}} == {'ci': {'depe...bar': '>=1'}}}
E    Differing items:
E    {'ci': {'include-groups': ['lint-tools'], 'dependencies': {'bar': '>=1'}}} != {'ci': {'dependencies': {'bar': '>=1'}}}

Green on the branch, same test:

tests/console/commands/test_remove.py::test_remove_cleans_up_differently_spelled_legacy_include_group PASSED
41 passed in 3.07s
  • Control assertion that passes before and after the fix: the removed group itself is gone ("lint_tools" not in content["tool"]["poetry"]["group"]). The red is therefore attributable to the dangling reference, not to the removal.
  • The test also reloads the written pyproject.toml with Factory().create_poetry() and asserts the lock is fresh — that is the user-visible part: an unloadable project.
  • Full tests/console: 1004 passed, 6 skipped, 3 failed. The same 3 failures (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) reproduce with identical counts on the clean base in this environment, so they are unrelated.
  • End-to-end CLI repro re-checked after the fix: the same pyproject.toml keeps ci = { dependencies = { idna } }, and both poetry lock and poetry show --only ci succeed.
  • mypy clean on all 381 source files (same command the CI job runs), and ruff check / ruff format --check (0.17.4, the pinned version) clean on both changed files.

Checklist

  • Added tests for changed code.
  • Updated documentation for changed code. — not needed: no user-facing option or documented behavior changes.

AI assistance

Prepared with an AI coding assistant. The failing case, the red/green runs, the full-suite baseline comparison and the CLI reproduction above were all executed and verified locally.

@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="251" />
<code_context>
         # 2. Legacy: [tool.poetry.group.<name>] include-groups = [...]
         poetry_content = content.get("tool", {}).get("poetry", {})
         if "group" in poetry_content:
+            normalized_group_name = canonicalize_name(group_name)
             groups_to_remove = []
             for group_key, group_content in poetry_content["group"].items():
</code_context>
<issue_to_address>
**Mixed-format references remain dangling**

When a legacy group is removed while a PEP 735 group references it using a canonically equivalent spelling, the PEP 735 cleanup branch still compares `item.get("include-group")` to `group_name` literally. Removing a legacy group keyed as `lint_tools` therefore leaves a PEP 735 reference spelled `lint-tools` in `[dependency-groups]`, and the project fails to load on a later command.

Normalize the PEP 735 `include-group` value and the removed group name before comparing them.
</issue_to_address>

### Comment 2
<location path="src/poetry/console/commands/remove.py" line_range="259" />
<code_context>
+                    name
+                    for name in include_groups
+                    if isinstance(name, str)
+                    and canonicalize_name(name) == normalized_group_name
+                ]
+                if not to_remove:
</code_context>
<issue_to_address>
**Surviving group references disappear**

When a legacy group is removed while the same canonical group remains under a differently spelled key in `[dependency-groups]`, `_remove_references_to_group` removes the legacy include even though the PEP 735 table still defines the same canonical group, so the including group loses dependencies from that surviving group.

Before removing a reference, preserve it when a canonically equivalent group remains defined in either section.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if the normalized-name match is too broad, running remove could delete an include-groups entry from the project’s pyproject.toml. Reverting the code would not restore that file, though the affected configuration is bounded and can be restored manually.

Blocking findings: src/poetry/console/commands/remove.py:251, src/poetry/console/commands/remove.py:259


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

Comment thread src/poetry/console/commands/remove.py Resolved
name
for name in include_groups
if isinstance(name, str)
and canonicalize_name(name) == 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 · Surviving group references disappear

When a legacy group is removed while the same canonical group remains under a differently spelled key in [dependency-groups], _remove_references_to_group removes the legacy include even though the PEP 735 table still defines the same canonical group, so the including group loses dependencies from that surviving group.

Before removing a reference, preserve it when a canonically equivalent group remains defined in either section.

Prompt for AI agents
In `src/poetry/console/commands/remove.py` at line 259:

**Surviving group references disappear**

When a legacy group is removed while the same canonical group remains under a differently spelled key in `[dependency-groups]`, `_remove_references_to_group` removes the legacy include even though the PEP 735 table still defines the same canonical group, so the including group loses dependencies from that surviving group.

Before removing a reference, preserve it when a canonically equivalent group remains defined in either section.

`poetry remove` deletes a dependency group that became empty and then
cleans up references to it in other groups. For the legacy
`[tool.poetry.group.<name>] include-groups` section that comparison was a
literal string match, so a group spelled `lint_tools` that is referenced as
`"lint-tools"` left a dangling entry behind and the project could no longer
be loaded ("Group 'ci' includes group 'lint-tools' which is not defined").

Canonicalize both sides, and remove the entry as it is actually spelled.
The PEP 735 `[dependency-groups]` branch is untouched.
@lau0708
lau0708 force-pushed the fix/remove-legacy-include-group-normalization branch from 037fcbe to d2a178e Compare November 15, 2026 02:26
@lau0708

lau0708 commented Nov 17, 2026

Copy link
Copy Markdown
Author

Thanks, pushed 3843d7c.

Comment 1 (dangling PEP 735 include-group) — valid, fixed. _remove_references_to_group normalized names only in the legacy include-groups branch; the [dependency-groups] branch still compared include-group literally. Since both sections share one namespace that poetry-core compares by normalized name (poetry/core/factory.py::Factory._validate_dependency_groups), ci = [{ include-group = "lint-tools" }] pointing at [tool.poetry.group.lint_tools] survived the removal of that group, and every later command then failed with:

Group 'ci' includes group 'lint-tools' which is not defined.

normalized_group_name is now computed once and used by both branches. Regression test test_remove_cleans_up_differently_spelled_pep735_include_group fails on d2a178e (assert 'dependency-groups' not in {...'ci': [{'include-group': 'lint-tools'}]}) and passes on this commit.

Comment 2 (a canonically equivalent group surviving under a differently spelled key) — not reachable. Such a project does not load at all, because both sections feed the same duplicate-name check:

$ poetry check
The Poetry configuration is invalid:
  - Duplicate dependency group name after normalization: lint-tools (lint-tools, lint_tools)

So when _remove_references_to_group runs, no canonically equivalent group can remain defined in the other section, and the reference is always stale. Left unchanged.

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

@lau0708 the legacy-section fix holds up in my run below. 3843d7c also changes the [dependency-groups] comparison, which #11225 changes with the same canonicalize_name match and isinstance guard, so the two now conflict in remove.py.

# git archive 3843d7c (base 4acc75e), Python 3.12 venv, remove.py loaded from the export
$ python -m pytest -q -p no:randomly tests/console/commands/test_remove.py
# 3843d7c: 42 passed
# remove.py back at 4acc75e, tests kept: 2 failed, 40 passed (both new differently-spelled tests)
$ ruff check / ruff format --check on remove.py and test_remove.py  (ruff 0.17.6): clean
$ mypy src/poetry/console/commands/remove.py tests/console/commands/test_remove.py  (mypy 2.4.1): no issues
$ git merge-tree --write-tree 3843d7c ba0af77   # #11230 head vs #11225 head
CONFLICT (content): Merge conflict in src/poetry/console/commands/remove.py
# not run: Windows, the full tests/ suite

The description still says the PEP 735 branch is untouched and the hunks are disjoint. Would you drop that hunk and its test so both land in either order, or update the description if the overlap is intended?

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

lau0708 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Taking the first option: dropped the [dependency-groups] hunk and its test. New head is 6b9925b7 (a revert of 3843d7c on top of d2a178e, so the tree is byte-identical to the legacy-only commit).

Your read is right, and it's my mistake — I took sourcery's comment 1 as in scope without noticing #11225 already does exactly that comparison. Duplicating it here bought nothing and cost the disjointness. The mixed-format case is covered once #11225 lands, in whichever order.

At 6b9925b7 the description is accurate again, so I left it alone. Same commands you ran:

$ git merge-tree --write-tree 6b9925b7 ba0af77   # this head vs #11225 head
0239a39d3e62d88ff2da8a33f6957fba7f0e2189
# no CONFLICT
$ python -m pytest -q -p no:randomly tests/console/commands/test_remove.py
# 41 passed  (was 42; the 42nd was the dropped pep735 test)
$ remove.py back at 4acc75e, tests kept, -k differently_spelled
# 1 failed  tests/console/commands/test_remove.py:1088
$ ruff check / ruff format --check on remove.py and test_remove.py  (ruff 0.17.4): clean
$ mypy src/poetry/console/commands/remove.py tests/console/commands/test_remove.py  (mypy 2.4.1): no issues
# not run: Windows, the full tests/ suite

Red is attributable to the dangling reference and not to the removal itself — the control assertion on line 1087 ("lint_tools" not in ...group) passes on both sides.

Nothing else in the diff changed: remove.py is +15/−3, legacy include-groups branch only. Say the word if you'd rather have this folded into #11225 instead of landing separately.

The branch is BEHIND; I'm leaving it alone for now to avoid churning the diff — happy to update it whenever you're ready to merge.

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.

2 participants