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 "direct" check in PipProvider.get_preference #12973

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions news/12973.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix prefering explicit requirements when backtracking.
4 changes: 2 additions & 2 deletions src/pip/_internal/resolution/resolvelib/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ def get_preference(
lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
candidate, ireqs = zip(*lookups)
else:
candidate, ireqs = None, ()
candidate, ireqs = (None,), ()

operators = [
specifier.operator
for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
for specifier in specifier_set
]

direct = candidate is not None
direct = any(candidate)
pinned = any(op[:2] == "==" for op in operators)
unfree = bool(operators)

Expand Down
105 changes: 103 additions & 2 deletions tests/unit/resolution_resolvelib/test_provider.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
from typing import TYPE_CHECKING, List, Optional
from typing import (
TYPE_CHECKING,
Dict,
Iterable,
Iterator,
List,
Mapping,
Optional,
Sequence,
)
from unittest.mock import Mock

import pytest
from pip._vendor.resolvelib.resolvers import RequirementInformation

from pip._internal.models.candidate import InstallationCandidate
from pip._internal.models.link import Link
from pip._internal.req.constructors import install_req_from_req_string
from pip._internal.resolution.resolvelib.base import Candidate, Constraint, Requirement
from pip._internal.resolution.resolvelib.candidates import (
REQUIRES_PYTHON_IDENTIFIER,
RequiresPythonCandidate,
)
from pip._internal.resolution.resolvelib.factory import Factory
from pip._internal.resolution.resolvelib.provider import PipProvider
from pip._internal.resolution.resolvelib.requirements import SpecifierRequirement

if TYPE_CHECKING:
from pip._internal.resolution.resolvelib.provider import PreferenceInformation
from pip._vendor.resolvelib.providers import Preference

PreferenceInformation = RequirementInformation[Requirement, Candidate]


def build_requirement_information(
Expand Down Expand Up @@ -76,3 +94,86 @@ def test_provider_known_depths(factory: Factory) -> None:
transitive_requirement_name: 2.0,
root_requirement_name: 1.0,
}


def create_mock_factory() -> Factory:
# Mock the required components for the Factory initialization
finder = Mock()
preparer = Mock()
make_install_req = Mock()
wheel_cache = Mock()
use_user_site = False
force_reinstall = False
ignore_installed = False
ignore_requires_python = False

# Create a Factory instance with mock components
return Factory(
finder=finder,
preparer=preparer,
make_install_req=make_install_req,
wheel_cache=wheel_cache,
use_user_site=use_user_site,
force_reinstall=force_reinstall,
ignore_installed=ignore_installed,
ignore_requires_python=ignore_requires_python,
)


@pytest.mark.parametrize(
"identifier, resolutions, candidates, information, backtrack_causes, expected",
[
(
REQUIRES_PYTHON_IDENTIFIER,
{},
{REQUIRES_PYTHON_IDENTIFIER: iter([RequiresPythonCandidate((3, 7))])},
{REQUIRES_PYTHON_IDENTIFIER: build_requirement_information("python", None)},
[],
(
False,
True,
True,
True,
1.0,
float("inf"),
True,
REQUIRES_PYTHON_IDENTIFIER,
),
),
],
)
def test_get_preference(
identifier: str,
resolutions: Mapping[str, Candidate],
candidates: Mapping[str, Iterator[Candidate]],
information: Mapping[str, Iterable["PreferenceInformation"]],
backtrack_causes: Sequence["PreferenceInformation"],
expected: "Preference",
) -> None:
# Create the factory with mock components
factory = create_mock_factory()
constraints: Dict[str, Constraint] = {}
user_requested = {"requested-package": 0}
ignore_dependencies = False
upgrade_strategy = "to-satisfy-only"

# Initialize PipProvider
provider = PipProvider(
factory=factory,
constraints=constraints,
ignore_dependencies=ignore_dependencies,
upgrade_strategy=upgrade_strategy,
user_requested=user_requested,
)

# Get the preference for the test case
preference = provider.get_preference(
identifier,
resolutions,
candidates,
information,
backtrack_causes,
)

# Assert the calculated preference matches the expected preference
assert preference == expected, f"Expected {expected}, got {preference}"
Loading