Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(add): do not pass @latest to parser #10069

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading