Skip to content

Commit 42dcea9

Browse files
committed
fix(add): do not pass @latest to parser
We should not pass in front-end specific `@latest` descriptor to the core requirement parser. Relates-to: #10068
1 parent 1b04d3c commit 42dcea9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/poetry/console/commands/init.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import re
4+
35
from collections.abc import Mapping
46
from contextlib import suppress
57
from pathlib import Path
@@ -470,7 +472,10 @@ def _parse_requirements(self, requirements: list[str]) -> list[dict[str, Any]]:
470472
env=self.env if isinstance(self, EnvCommand) else None,
471473
cwd=cwd,
472474
)
473-
return [parser.parse(requirement) for requirement in requirements]
475+
return [
476+
parser.parse(re.sub(r"@\s*latest$", "", requirement, flags=re.I))
477+
for requirement in requirements
478+
]
474479

475480
def _format_requirements(self, requirements: list[dict[str, str]]) -> Requirements:
476481
requires: Requirements = {}

tests/console/commands/test_add.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from poetry.console.commands.installer_command import InstallerCommand
1616
from poetry.puzzle.exceptions import SolverProblemError
1717
from poetry.repositories.legacy_repository import LegacyRepository
18+
from poetry.utils.dependency_specification import RequirementsParser
1819
from tests.helpers import TestLocker
1920
from tests.helpers import get_dependency
2021
from tests.helpers import get_package
@@ -1245,6 +1246,21 @@ def test_add_should_fail_circular_dependency(
12451246
assert expected in tester.io.fetch_error()
12461247

12471248

1249+
def test_add_latest_should_strip_out_invalid_pep508_path(
1250+
tester: CommandTester, repo: TestRepository, mocker: MockerFixture
1251+
) -> None:
1252+
spy = mocker.spy(RequirementsParser, "parse")
1253+
repo.add_package(get_package("foo", "1.1.1"))
1254+
repo.add_package(get_package("foo", "1.1.2"))
1255+
tester.execute("foo@latest")
1256+
1257+
assert tester.status_code == 0
1258+
assert "Using version ^1.1.2 for foo" in tester.io.fetch_output()
1259+
1260+
assert spy.call_count == 1
1261+
assert spy.call_args_list[0].args[1] == "foo"
1262+
1263+
12481264
@pytest.mark.parametrize("project_dependencies", [True, False])
12491265
def test_add_latest_should_not_create_duplicate_keys(
12501266
project_factory: ProjectFactory,

0 commit comments

Comments
 (0)