diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index 9aef36f590a..b08fbd9dc67 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -1,5 +1,7 @@ from __future__ import annotations +import re + from collections.abc import Mapping from contextlib import suppress from pathlib import Path @@ -470,7 +472,10 @@ def _parse_requirements(self, requirements: list[str]) -> list[dict[str, Any]]: env=self.env if isinstance(self, EnvCommand) else None, cwd=cwd, ) - return [parser.parse(requirement) for requirement in requirements] + return [ + parser.parse(re.sub(r"@\s*latest$", "", requirement, flags=re.I)) + for requirement in requirements + ] def _format_requirements(self, requirements: list[dict[str, str]]) -> Requirements: requires: Requirements = {} diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index 92065c10764..57ad7ecf4e9 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -15,6 +15,7 @@ from poetry.console.commands.installer_command import InstallerCommand from poetry.puzzle.exceptions import SolverProblemError from poetry.repositories.legacy_repository import LegacyRepository +from poetry.utils.dependency_specification import RequirementsParser from tests.helpers import TestLocker from tests.helpers import get_dependency from tests.helpers import get_package @@ -1245,6 +1246,21 @@ def test_add_should_fail_circular_dependency( assert expected in tester.io.fetch_error() +def test_add_latest_should_strip_out_invalid_pep508_path( + tester: CommandTester, repo: TestRepository, mocker: MockerFixture +) -> None: + spy = mocker.spy(RequirementsParser, "parse") + repo.add_package(get_package("foo", "1.1.1")) + repo.add_package(get_package("foo", "1.1.2")) + tester.execute("foo@latest") + + assert tester.status_code == 0 + assert "Using version ^1.1.2 for foo" in tester.io.fetch_output() + + assert spy.call_count == 1 + assert spy.call_args_list[0].args[1] == "foo" + + @pytest.mark.parametrize("project_dependencies", [True, False]) def test_add_latest_should_not_create_duplicate_keys( project_factory: ProjectFactory,