Skip to content

fix(remove): clean up dangling extras references after removing a dependency from main - #11237

Open
lau0708 wants to merge 1 commit into
python-poetry:mainfrom
lau0708:fix/remove-cleans-up-extras-references
Open

lau0708 wants to merge 1 commit into
python-poetry:mainfrom
lau0708:fix/remove-cleans-up-extras-references

Conversation

@lau0708

@lau0708 lau0708 commented Nov 18, 2026

Copy link
Copy Markdown

Resolves: #11236

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code. (no user-facing documented behavior change; remove now only cleans up references that can no longer be valid)

Why / Problem

poetry remove <pkg> removes the package from [tool.poetry.dependencies] but leaves entries in [tool.poetry.extras] that reference it. Since poetry-core validates that every extras entry resolves to a main dependency (Cannot find dependency "..." for extra "..." in main dependencies.), the project ends up in a state where poetry check exits 1 — while remove itself exited 0:

$ poetry remove six --lock        # exit 0
$ poetry check
Error: Cannot find dependency "six" for extra "tools" in main dependencies.   # exit 1

This is the same "dangling reference left behind by remove" family as the include-group cleanups (#11225, #11230, #11233), on the [tool.poetry.extras] side.

What changed

  • src/poetry/console/commands/remove.py: in the no---group branch, after removing packages from the main group, drop their references from [tool.poetry.extras] (matched by canonicalized name). Extras that become empty are removed, and the extras section is removed if it becomes empty — mirroring how this command already cleans up emptied group sections.
  • tests/console/commands/test_remove.py: two regression tests (partial cleanup keeps remaining extras; fully emptied section is dropped).

Scope note: this does not add extras management to the CLI (that was #10726, and the maintainer feedback there was to edit the pyproject manually). Here nothing is added — we only clean up a reference that is invalid by definition once the dependency it points to is gone, so that remove does not leave the project in a state that fails its own validation.

Validation

Red on base (8c3ddc3), green on this branch, control assertion passes on both sides:

# base, red (control asserts "foo" not in dependencies passed; failure is the extras state):
AssertionError: assert {'tools': ['f...ete': ['foo']} == {'tools': ['bar']}

# branch, green:
tests/console/commands/test_remove.py  42 passed

# full suite, identical to clean-base baseline:
tests/console  1008 passed, 3 failed (pre-existing local-environment 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)

ruff 0.17.4 (check + format): clean
mypy on src/poetry/console/commands/remove.py + tests/console/commands/test_remove.py: Success

# CLI end-to-end (probe project with optional dep + extras):
poetry remove six --lock        -> exit 0, [tool.poetry.extras] removed from pyproject
poetry check                    -> exit 0 (was exit 1 on base)

@sourcery-ai

sourcery-ai Bot commented Nov 18, 2026

Copy link
Copy Markdown

Linked issues

Checked at commit 0349a22

#11236 poetry remove leaves a dangling reference in [tool.poetry.extras] when removing an optional dependency, breaking poetry check · 1 of 1 met

# Requirement Status Notes
1 When removing a main dependency, remove its references from [tool.poetry.extras], and remove extras and the extras section that become empty. ✅ Met The remove command cleans extras after a package is removed from the main group, matching names canonically; regression tests cover partial cleanup and removal of an emptied extras section.

@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="105" />
<code_context>
                 )
+                removed |= removed_in_group
+                if group_name == MAIN_GROUP and removed_in_group:
+                    self._remove_extras_references(removed_in_group, poetry_content)
                 if group_name != MAIN_GROUP:
                     if (
</code_context>
<issue_to_address>
**Lock retains removed extra references**

When a removed main dependency is referenced by an extra and the command regenerates the lock, `_remove_extras_references` edits only `poetry_content`, leaving `self.poetry.package.extras` stale, so the installer writes the removed dependency into `poetry.lock` despite its removal from `pyproject.toml`.

Update `self.poetry.package.extras` as well as `poetry_content` when removing extra references.
</issue_to_address>

### Comment 2
<location path="src/poetry/console/commands/remove.py" line_range="234" />
<code_context>
+            remaining = [
+                requirement
+                for requirement in requirements
+                if canonicalize_name(requirement) not in removed_names
+            ]
+            if len(remaining) == len(requirements):
</code_context>
<issue_to_address>
**Constrained extras references remain**

When an extra contains a valid constrained requirement such as `foo (>=1)`, `canonicalize_name(requirement)` canonicalizes the entire constrained string instead of extracting its package name, so it does not match the removed package; the dangling reference remains and `poetry check` fails.

Extract the dependency name from each requirement before canonicalizing it.

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

Sourcery assessment

Needs a human reviewer. 2 findings to address first, and if the name matching is wrong, removing a dependency could also delete its entry from one or more extras in pyproject.toml; reverting the code will not restore those edits, but the affected entries can be added back manually.

Blocking findings: src/poetry/console/commands/remove.py:105, src/poetry/console/commands/remove.py:234


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

)
removed |= removed_in_group
if group_name == MAIN_GROUP and removed_in_group:
self._remove_extras_references(removed_in_group, poetry_content)

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 · Lock retains removed extra references

When a removed main dependency is referenced by an extra and the command regenerates the lock, _remove_extras_references edits only poetry_content, leaving self.poetry.package.extras stale, so the installer writes the removed dependency into poetry.lock despite its removal from pyproject.toml.

Update self.poetry.package.extras as well as poetry_content when removing extra references.

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

**Lock retains removed extra references**

When a removed main dependency is referenced by an extra and the command regenerates the lock, `_remove_extras_references` edits only `poetry_content`, leaving `self.poetry.package.extras` stale, so the installer writes the removed dependency into `poetry.lock` despite its removal from `pyproject.toml`.

Update `self.poetry.package.extras` as well as `poetry_content` when removing extra references.

remaining = [
requirement
for requirement in requirements
if canonicalize_name(requirement) not in removed_names

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Constrained extras references remain

When an extra contains a valid constrained requirement such as foo (>=1), canonicalize_name(requirement) canonicalizes the entire constrained string instead of extracting its package name, so it does not match the removed package; the dangling reference remains and poetry check fails.

Extract the dependency name from each requirement before canonicalizing it.

Also at src/poetry/console/commands/remove.py:235.

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

**Constrained extras references remain**

When an extra contains a valid constrained requirement such as `foo (>=1)`, `canonicalize_name(requirement)` canonicalizes the entire constrained string instead of extracting its package name, so it does not match the removed package; the dangling reference remains and `poetry check` fails.

Extract the dependency name from each requirement before canonicalizing it.

Also at `src/poetry/console/commands/remove.py:235`.

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 leaves a dangling reference in [tool.poetry.extras] when removing an optional dependency, breaking poetry check

1 participant