Skip to content

Commit

Permalink
Use the caching variant of is_satified_by in Factory.find_candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Jan 15, 2024
1 parent 8de2842 commit 7adf2d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from typing import (
TYPE_CHECKING,
Callable,
Dict,
FrozenSet,
Iterable,
Expand Down Expand Up @@ -391,6 +392,7 @@ def find_candidates(
incompatibilities: Mapping[str, Iterator[Candidate]],
constraint: Constraint,
prefers_installed: bool,
is_satisfied_by: Callable[[Requirement, Candidate], bool],
) -> Iterable[Candidate]:
# Collect basic lookup information from the requirements.
explicit_candidates: Set[Candidate] = set()
Expand Down Expand Up @@ -456,7 +458,7 @@ def find_candidates(
for c in explicit_candidates
if id(c) not in incompat_ids
and constraint.is_satisfied_by(c)
and all(req.is_satisfied_by(c) for req in requirements[identifier])
and all(is_satisfied_by(req, c) for req in requirements[identifier])
)

def _make_requirements_from_install_req(
Expand Down
1 change: 1 addition & 0 deletions src/pip/_internal/resolution/resolvelib/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def _eligible_for_upgrade(identifier: str) -> bool:
constraint=constraint,
prefers_installed=(not _eligible_for_upgrade(identifier)),
incompatibilities=incompatibilities,
is_satisfied_by=self.is_satisfied_by,
)

@lru_cache(maxsize=None)
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/resolution_resolvelib/test_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
# Editables


def _is_satisfied_by(requirement: Requirement, candidate: Candidate) -> bool:
"""A helper function to check if a requirement is satisfied by a candidate.
Used for mocking PipProvider.is_satified_by.
"""
return requirement.is_satisfied_by(candidate)


@pytest.fixture
def test_cases(data: TestData) -> Iterator[List[Tuple[str, str, int]]]:
def _data_file(name: str) -> Path:
Expand Down Expand Up @@ -80,6 +88,7 @@ def test_new_resolver_correct_number_of_matches(
{},
Constraint.empty(),
prefers_installed=False,
is_satisfied_by=_is_satisfied_by,
)
assert sum(1 for _ in matches) == match_count

Expand All @@ -98,6 +107,7 @@ def test_new_resolver_candidates_match_requirement(
{},
Constraint.empty(),
prefers_installed=False,
is_satisfied_by=_is_satisfied_by,
)
for c in candidates:
assert isinstance(c, Candidate)
Expand Down

0 comments on commit 7adf2d1

Please sign in to comment.