From 184dda892b21090e504ce2ac9bcf304c2f178cfa Mon Sep 17 00:00:00 2001 From: Agnia Sergeyuk Date: Tue, 26 May 2026 12:14:43 +0200 Subject: [PATCH] AI-generated possible solution for #10703 --- src/poetry/console/commands/remove.py | 32 ++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/poetry/console/commands/remove.py b/src/poetry/console/commands/remove.py index 5180080a00e..cecec40bf5f 100644 --- a/src/poetry/console/commands/remove.py +++ b/src/poetry/console/commands/remove.py @@ -80,6 +80,12 @@ def handle(self) -> int: group_sections.append( (MAIN_GROUP, project_dependencies, poetry_dependencies) ) + # Add optional dependencies (extras) to the group sections + optional_dependencies = project_content.get("optional-dependencies", {}) + for extra_name, extra_deps in optional_dependencies.items(): + group_sections.append( + (extra_name, extra_deps, {}) + ) group_sections.extend( ( group_name, @@ -127,6 +133,19 @@ def handle(self) -> int: del poetry_content["dev-dependencies"] else: removed = set() + # Check if the group is an optional dependency (extra) + optional_dependencies = project_content.get("optional-dependencies", {}) + if group in optional_dependencies: + removed.update( + self._remove_packages( + packages=packages, + standard_section=optional_dependencies[group], + poetry_section={}, + group_name=group, + ) + ) + if not optional_dependencies[group]: + del optional_dependencies[group] if group_content := poetry_groups_content.get(group): poetry_section = group_content.get("dependencies", {}) removed.update( @@ -152,13 +171,24 @@ def handle(self) -> int: ) if not groups_content[group]: del groups_content[group] - if group not in groups_content and group not in poetry_groups_content: + if ( + group not in groups_content + and group not in poetry_groups_content + and group not in optional_dependencies + ): self._remove_references_to_group(group, content) if "group" in poetry_content and not poetry_content["group"]: del poetry_content["group"] if "dependency-groups" in content and not content["dependency-groups"]: del content["dependency-groups"] + # Clean up empty optional dependencies + if "optional-dependencies" in project_content: + for extra_name in list(project_content["optional-dependencies"].keys()): + if not project_content["optional-dependencies"][extra_name]: + del project_content["optional-dependencies"][extra_name] + if not project_content["optional-dependencies"]: + del project_content["optional-dependencies"] not_found = set(packages).difference(removed) if not_found: