Skip to content

Commit

Permalink
fix(add): do not pass @latest to parser
Browse files Browse the repository at this point in the history
We should not pass in front-end specific `@latest` descriptor to the
core requirement parser.

Relates-to: #10068
  • Loading branch information
abn committed Jan 17, 2025
1 parent 1b04d3c commit 6fda3ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import re

from collections.abc import Mapping
from contextlib import suppress
from pathlib import Path
Expand Down Expand Up @@ -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 = {}
Expand Down
16 changes: 16 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6fda3ae

Please sign in to comment.