Skip to content

Fix transitive dependency-group reference cleanup on removal - #11141

Merged
radoering merged 3 commits into
python-poetry:mainfrom
Trunal2005:codex/investigate-recursive-group-removal
Nov 1, 2026
Merged

radoering merged 3 commits into
python-poetry:mainfrom
Trunal2005:codex/investigate-recursive-group-removal

Conversation

@Trunal2005

@Trunal2005 Trunal2005 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Resolves: #11140

Related to #10590 and the direct-reference cleanup in #10653.

When tools includes lint and dev includes tools, removing the last dependency from lint succeeds but leaves dev referring to the deleted tools group. The next project command then fails because tools is undefined.

This change recursively cleans references to groups deleted by reference cleanup. A group is deleted only when removing a matching inclusion made it empty. Pre-existing empty groups and their parents survive, as do references to groups still defined in the other TOML section.

The regression tests cover chains and a diamond with shared parents, both declaration orders, explicit/all-group removal, PEP 735/legacy/mixed declarations, retained cross-section definitions, and unrelated empty groups and their parents. They check remaining TOML structure, project reload, remaining requirements, locked packages, and lock freshness.

Review follow-up

  • Rebased onto upstream main at b1e370bf.
  • Adapted the added tests to tomlrt.loads/tomlrt.dumps. Reversed declarations use explicit section tables, preserving valid legacy TOML sections.
  • Added the numbered recursive-cleanup comment and moved all three complex tests to the end. Existing upstream tests remain unchanged.
  • Preserved both earlier commits, including the Sourcery correction. No additional production behavior, dependency, or lockfile changes.

Validation at b8581f12

Ubuntu 24.04, official setup-python CPython 3.11.17 artifact, committed locked dependencies:

  • Before migration: 12 obsolete-API failures and 3 passes in the focused cases. After migration: all 15 focused cases pass.

  • Complete remove-command suite: 40 passed.

  • Console-command suite: 881 passed, 6 standard skips.

  • Full mypy: 380 files passed.

  • All applicable pinned pre-commit hooks on both changed files, including Ruff lint/format, and git diff --check: passed.

  • Full default suite, non-root user with compiler prerequisites: 3,332 passed, 34 standard skips, 2 expected failures. Fresh upstream base with the same environment and seed: 3,317 passed, the same 34 skips and 2 expected failures. Initial container failures from PATH, missing GCC and root permission bypass were corrected in the environment only; no tests were weakened or newly skipped.

Fresh upstream CI completed successfully: 47 jobs passed and 3 were skipped by workflow conditions, including a passing Ubuntu/Python 3.11 pytest job. Sourcery and pre-commit.ci also passed. No review approval is implied.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. No documentation change is needed to restore the existing cleanup behavior.

AI assistance: OpenAI Codex was used for investigation, implementation, regression tests, rebase, and validation. No human code review is claimed.

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

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="246" />
<code_context>

             for group_key in groups_to_remove:
                 del content["dependency-groups"][group_key]
+                removed_groups.add(group_key)

         # 2. Legacy: [tool.poetry.group.<name>] include-groups = [...]
</code_context>
<issue_to_address>
**Unrelated groups are deleted**

When reference cleanup encounters a pre-existing empty PEP 735 group or a legacy group with an empty `include-groups` list, `groups_to_remove` includes groups even when this cleanup removed none of their entries. Adding them to `removed_groups` makes recursive reference cleanup delete groups that include them, so unrelated group declarations disappear.

Add a group to `removed_groups` only when this cleanup made it empty.

Also at `src/poetry/console/commands/remove.py:267`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the recursive cleanup removes a group that still contains meaningful configuration, that group and its dependency declarations are deleted from the project file, and the regenerated lock can drop those packages. Reverting the code does not restore the edited project file or lock; they would need to be recovered or rebuilt.

Blocking findings: src/poetry/console/commands/remove.py:246


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

Comment thread src/poetry/console/commands/remove.py Resolved
@Trunal2005

Copy link
Copy Markdown
Contributor Author

I investigated the Ubuntu/Python 3.11 CI failure and reproduced it on both unchanged base 38a86661 and PR head 7f81db5d, using Ubuntu 24.04, the official setup-python 3.11.17 artifact, and locked dependencies.

The failing test globally mocks sys.platform = "darwin". With cold sysconfig state, NullEnv reaches real host configuration code and attempts to import _sysconfigdata__darwin_x86_64-linux-gnu. The exact case fails in isolation on both revisions; preceding installer tests initialize sysconfig on Linux and mask it.

With the CI seed, the added tests shift xdist's initial batch boundary, making this case the first test on its worker. Replaying the base's preceding tests passes on both revisions. The complete installer module also passes all 460 cases on both.

This appears to be a pre-existing test-isolation defect exposed by the changed worker assignment. I've left the group-cleanup implementation unchanged. Would you prefer the test correction handled separately or included here? Reproduction commands and full tracebacks are preserved.

@radoering radoering left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you rebase your branch please? (tomlkit has been replaced with tomlrt so that you have to adapt the tests.)

Comment thread src/poetry/console/commands/remove.py Resolved
Comment thread tests/console/commands/test_remove.py Resolved
Preserve section tables when reversing group declarations, move complex cleanup tests after the basic cases, and number the recursive cleanup section.
@Trunal2005
Trunal2005 force-pushed the codex/investigate-recursive-group-removal branch from 7f81db5 to b8581f1 Compare October 31, 2026 19:07
@radoering
radoering merged commit 3bdedf9 into python-poetry:main Nov 1, 2026
52 checks passed
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

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 Dec 2, 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.

poetry remove leaves dangling references to indirectly deleted dependency groups

2 participants