diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index d1b9438650e..159d703c49d 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -459,7 +459,8 @@ def _find_best_version_for_package( # TODO: find similar raise ValueError(f"Could not find a matching version of package {name}") - return package.pretty_name, f"^{package.version.to_string()}" + version = package.version.without_local() + return package.pretty_name, f"^{version}" def _parse_requirements(self, requirements: list[str]) -> list[dict[str, Any]]: from poetry.core.pyproject.exceptions import PyProjectException diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index da6467d2854..73ba0241018 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -103,6 +103,7 @@ def repo_add_default_packages(repo: TestRepository) -> None: repo.add_package(get_package("tomlkit", "0.5.5")) repo.add_package(get_package("pyyaml", "3.13")) repo.add_package(get_package("pyyaml", "4.2b2")) + repo.add_package(get_package("torch", "2.4.0+cpu")) def test_add_no_constraint(app: PoetryTestApplication, tester: CommandTester) -> None: @@ -133,6 +134,33 @@ def test_add_no_constraint(app: PoetryTestApplication, tester: CommandTester) -> assert content["dependencies"]["cachy"] == "^0.2.0" +def test_add_local_version(app: PoetryTestApplication, tester: CommandTester) -> None: + tester.execute("torch") + + expected = """\ +Using version ^2.4.0 for torch + +Updating dependencies +Resolving dependencies... + +Package operations: 1 install, 0 updates, 0 removals + + - Installing torch (2.4.0+cpu) + +Writing lock file +""" + + assert tester.io.fetch_output() == expected + assert isinstance(tester.command, InstallerCommand) + assert tester.command.installer.executor.installations_count == 1 + + pyproject: dict[str, Any] = app.poetry.file.read() + content = pyproject["tool"]["poetry"] + + assert "torch" in content["dependencies"] + assert content["dependencies"]["torch"] == "^2.4.0" + + def test_add_non_package_mode_no_name( project_factory: ProjectFactory, command_tester_factory: CommandTesterFactory,