Skip to content

Commit 3ea8a5d

Browse files
authored
remove local version specifier at poetry add (#9603)
1 parent 36c7e18 commit 3ea8a5d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/poetry/console/commands/init.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ def _find_best_version_for_package(
459459
# TODO: find similar
460460
raise ValueError(f"Could not find a matching version of package {name}")
461461

462-
return package.pretty_name, f"^{package.version.to_string()}"
462+
version = package.version.without_local()
463+
return package.pretty_name, f"^{version.to_string()}"
463464

464465
def _parse_requirements(self, requirements: list[str]) -> list[dict[str, Any]]:
465466
from poetry.core.pyproject.exceptions import PyProjectException

tests/console/commands/test_add.py

+28
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def repo_add_default_packages(repo: TestRepository) -> None:
103103
repo.add_package(get_package("tomlkit", "0.5.5"))
104104
repo.add_package(get_package("pyyaml", "3.13"))
105105
repo.add_package(get_package("pyyaml", "4.2b2"))
106+
repo.add_package(get_package("torch", "2.4.0+cpu"))
106107

107108

108109
def test_add_no_constraint(app: PoetryTestApplication, tester: CommandTester) -> None:
@@ -133,6 +134,33 @@ def test_add_no_constraint(app: PoetryTestApplication, tester: CommandTester) ->
133134
assert content["dependencies"]["cachy"] == "^0.2.0"
134135

135136

137+
def test_add_local_version(app: PoetryTestApplication, tester: CommandTester) -> None:
138+
tester.execute("torch")
139+
140+
expected = """\
141+
Using version ^2.4.0 for torch
142+
143+
Updating dependencies
144+
Resolving dependencies...
145+
146+
Package operations: 1 install, 0 updates, 0 removals
147+
148+
- Installing torch (2.4.0+cpu)
149+
150+
Writing lock file
151+
"""
152+
153+
assert tester.io.fetch_output() == expected
154+
assert isinstance(tester.command, InstallerCommand)
155+
assert tester.command.installer.executor.installations_count == 1
156+
157+
pyproject: dict[str, Any] = app.poetry.file.read()
158+
content = pyproject["tool"]["poetry"]
159+
160+
assert "torch" in content["dependencies"]
161+
assert content["dependencies"]["torch"] == "^2.4.0"
162+
163+
136164
def test_add_non_package_mode_no_name(
137165
project_factory: ProjectFactory,
138166
command_tester_factory: CommandTesterFactory,

0 commit comments

Comments
 (0)