Skip to content

Commit 042be4c

Browse files
committed
fix: ensure no duplicate package names are stored in pyproject.toml
1 parent 1ec8be3 commit 042be4c

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Diff for: src/poetry/console/commands/add.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,15 @@ def handle(self) -> int:
215215

216216
constraint_name = _constraint["name"]
217217
assert isinstance(constraint_name, str)
218-
section[constraint_name] = constraint
218+
219+
canonical_constraint_name = canonicalize_name(constraint_name)
220+
221+
for key in section:
222+
if canonicalize_name(key) == canonical_constraint_name:
223+
section[key] = constraint
224+
break
225+
else:
226+
section[constraint_name] = constraint
219227

220228
with contextlib.suppress(ValueError):
221229
self.poetry.package.dependency_group(group).remove_dependency(

Diff for: tests/console/commands/test_add.py

+38
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,44 @@ def test_add_should_skip_when_adding_canonicalized_existing_package_with_no_cons
10551055
assert expected in tester.io.fetch_output()
10561056

10571057

1058+
def test_add_latest_should_not_create_duplicate_keys(
1059+
project_factory: ProjectFactory,
1060+
repo: TestRepository,
1061+
command_tester_factory: CommandTesterFactory,
1062+
):
1063+
pyproject_content = """\
1064+
[tool.poetry]
1065+
name = "simple-project"
1066+
version = "1.2.3"
1067+
description = "Some description."
1068+
authors = [
1069+
"Python Poetry <[email protected]>"
1070+
]
1071+
license = "MIT"
1072+
readme = "README.md"
1073+
1074+
[tool.poetry.dependencies]
1075+
python = "^3.6"
1076+
Foo = "^0.6"
1077+
"""
1078+
1079+
poetry = project_factory(name="simple-project", pyproject_content=pyproject_content)
1080+
content = poetry.file.read()
1081+
1082+
assert "Foo" in content["tool"]["poetry"]["dependencies"]
1083+
assert content["tool"]["poetry"]["dependencies"]["Foo"] == "^0.6"
1084+
assert "foo" not in content["tool"]["poetry"]["dependencies"]
1085+
1086+
tester = command_tester_factory("add", poetry=poetry)
1087+
repo.add_package(get_package("foo", "1.1.2"))
1088+
tester.execute("foo@latest")
1089+
1090+
updated_content = poetry.file.read()
1091+
assert "Foo" in updated_content["tool"]["poetry"]["dependencies"]
1092+
assert updated_content["tool"]["poetry"]["dependencies"]["Foo"] == "^1.1.2"
1093+
assert "foo" not in updated_content["tool"]["poetry"]["dependencies"]
1094+
1095+
10581096
def test_add_should_work_when_adding_existing_package_with_latest_constraint(
10591097
app: PoetryTestApplication, repo: TestRepository, tester: CommandTester
10601098
):

0 commit comments

Comments
 (0)