Skip to content

Commit 74b645d

Browse files
committed
add: do not create empty [tool.poetry.group] sections when adding a dependency to [dependency-groups] (#10130)
1 parent 41a7d22 commit 74b645d

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/poetry/console/commands/add.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,11 @@ def handle(self) -> int:
198198
for dep in groups_content[group]
199199
]
200200

201-
if "group" not in poetry_content:
202-
poetry_content["group"] = table(is_super_table=True)
203-
204-
groups = poetry_content["group"]
205-
206-
if group not in groups:
207-
groups[group] = table()
208-
groups.add(nl())
209-
210-
this_group = groups[group]
211-
212-
if "dependencies" not in this_group:
213-
this_group["dependencies"] = table()
214-
215-
poetry_section = this_group["dependencies"]
201+
poetry_section = (
202+
poetry_content.get("group", {})
203+
.get(group, {})
204+
.get("dependencies", table())
205+
)
216206
project_section = []
217207

218208
existing_packages = self.get_existing_packages_from_input(
@@ -379,8 +369,21 @@ def handle(self) -> int:
379369
content["tool"] = table()
380370
if "poetry" not in content["tool"]:
381371
content["tool"]["poetry"] = poetry_content
382-
if group == MAIN_GROUP and "dependencies" not in poetry_content:
383-
poetry_content["dependencies"] = poetry_section
372+
if group == MAIN_GROUP:
373+
if "dependencies" not in poetry_content:
374+
poetry_content["dependencies"] = poetry_section
375+
else:
376+
if "group" not in poetry_content:
377+
poetry_content["group"] = table(is_super_table=True)
378+
379+
groups = poetry_content["group"]
380+
381+
if group not in groups:
382+
groups[group] = table()
383+
groups.add(nl())
384+
385+
if "dependencies" not in groups[group]:
386+
groups[group]["dependencies"] = poetry_section
384387

385388
if groups_content and group != MAIN_GROUP:
386389
if "dependency-groups" not in content:

tests/console/commands/test_add.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,9 @@ def test_add_to_new_group_keeps_existing_group(
633633
]
634634

635635

636+
@pytest.mark.parametrize("additional_poetry_group", [False, True])
636637
def test_add_to_existing_group(
637-
app: PoetryTestApplication, tester: CommandTester
638+
app: PoetryTestApplication, tester: CommandTester, additional_poetry_group: bool
638639
) -> None:
639640
pyproject: dict[str, Any] = app.poetry.file.read()
640641
groups_content: dict[str, Any] = tomlkit.parse(
@@ -646,6 +647,16 @@ def test_add_to_existing_group(
646647
"""
647648
)
648649
pyproject["dependency-groups"] = groups_content["dependency-groups"]
650+
if additional_poetry_group:
651+
poetry_groups_content: dict[str, Any] = tomlkit.parse(
652+
"""\
653+
[tool.poetry.group.example.dependencies]
654+
cachy = { allow-prereleases = true }
655+
"""
656+
)
657+
pyproject["tool"]["poetry"]["group"] = poetry_groups_content["tool"]["poetry"][
658+
"group"
659+
]
649660
pyproject = cast("TOMLDocument", pyproject)
650661
app.poetry.file.write(pyproject)
651662

@@ -666,6 +677,13 @@ def test_add_to_existing_group(
666677
"cachy (>=0.2.0,<0.3.0)",
667678
"pendulum (>=1.4.4,<2.0.0)",
668679
]
680+
if additional_poetry_group:
681+
assert "example" in pyproject["tool"]["poetry"]["group"]
682+
assert pyproject["tool"]["poetry"]["group"]["example"]["dependencies"] == {
683+
"cachy": {"allow-prereleases": True},
684+
}
685+
else:
686+
assert "group" not in pyproject["tool"]["poetry"]
669687

670688

671689
def test_add_to_group_with_latest_overwrite_existing(
@@ -757,6 +775,7 @@ def test_add_to_group_uses_existing_legacy_group(
757775

758776
pyproject = app.poetry.file.read()
759777
pyproject = cast("dict[str, Any]", pyproject)
778+
assert "dependency-groups" not in pyproject
760779
assert "example" in pyproject["tool"]["poetry"]["group"]
761780
assert "pendulum" in pyproject["tool"]["poetry"]["group"]["example"]["dependencies"]
762781
assert "cachy" in pyproject["tool"]["poetry"]["group"]["example"]["dependencies"]

0 commit comments

Comments
 (0)