Skip to content

Commit

Permalink
Fix type hint errors due to new resolvelib
Browse files Browse the repository at this point in the history
  • Loading branch information
notatallshaw committed Oct 31, 2024
1 parent 88ceab2 commit d309e2c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def get_installation_error(
# The simplest case is when we have *one* cause that can't be
# satisfied. We just report that case.
if len(e.causes) == 1:
req, parent = e.causes[0]
req, parent = next(iter(e.causes))
if req.name not in constraints:
return self._report_single_requirement_conflict(req, parent)

Expand Down
15 changes: 10 additions & 5 deletions src/pip/_internal/resolution/resolvelib/reporter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from collections import defaultdict
from logging import getLogger
from typing import Any, DefaultDict
from typing import Any, DefaultDict, Optional

from pip._vendor.resolvelib.reporters import BaseReporter
from pip._vendor.resolvelib.resolvers import Criterion

from .base import Candidate, Requirement

logger = getLogger(__name__)


class PipReporter(BaseReporter):
class PipReporter(BaseReporter[Requirement, Candidate, str]):
def __init__(self) -> None:
self.reject_count_by_package: DefaultDict[str, int] = defaultdict(int)

Expand All @@ -32,7 +33,9 @@ def __init__(self) -> None:
),
}

def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:
def rejecting_candidate(
self, criterion: Criterion[Requirement, Candidate], candidate: Candidate
) -> None:
self.reject_count_by_package[candidate.name] += 1

count = self.reject_count_by_package[candidate.name]
Expand All @@ -55,7 +58,7 @@ def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:
logger.debug(msg)


class PipDebuggingReporter(BaseReporter):
class PipDebuggingReporter(BaseReporter[Requirement, Candidate, str]):
"""A reporter that does an info log for every event it sees."""

def starting(self) -> None:
Expand All @@ -71,7 +74,9 @@ def ending_round(self, index: int, state: Any) -> None:
def ending(self, state: Any) -> None:
logger.info("Reporter.ending(%r)", state)

def adding_requirement(self, requirement: Requirement, parent: Candidate) -> None:
def adding_requirement(
self, requirement: Requirement, parent: Optional[Candidate]
) -> None:
logger.info("Reporter.adding_requirement(%r, %r)", requirement, parent)

def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/resolution/resolvelib/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def resolve(
user_requested=collected.user_requested,
)
if "PIP_RESOLVER_DEBUG" in os.environ:
reporter: BaseReporter = PipDebuggingReporter()
reporter: BaseReporter[Requirement, Candidate, str] = PipDebuggingReporter()
else:
reporter = PipReporter()
resolver: RLResolver[Requirement, Candidate, str] = RLResolver(
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/resolution_resolvelib/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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
from pip._internal.resolution.resolvelib.factory import Factory
from pip._internal.resolution.resolvelib.provider import PipProvider
from pip._internal.resolution.resolvelib.requirements import SpecifierRequirement
Expand All @@ -14,13 +15,13 @@


def build_requirement_information(
name: str, parent: Optional[InstallationCandidate]
name: str, parent: Optional[Candidate]
) -> List["PreferenceInformation"]:
install_requirement = install_req_from_req_string(name)
# RequirementInformation is typed as a tuple, but it is a namedtupled.
# https://github.com/sarugaku/resolvelib/blob/7bc025aa2a4e979597c438ad7b17d2e8a08a364e/src/resolvelib/resolvers.pyi#L20-L22
requirement_information: PreferenceInformation = RequirementInformation(
requirement=SpecifierRequirement(install_requirement), # type: ignore[call-arg]
requirement=SpecifierRequirement(install_requirement),
parent=parent,
)
return [requirement_information]
Expand All @@ -29,7 +30,7 @@ def build_requirement_information(
def test_provider_known_depths(factory: Factory) -> None:
# Root requirement is specified by the user
# therefore has an inferred depth of 1
root_requirement_name = "my-package"
root_requirement_name: str = "my-package"
provider = PipProvider(
factory=factory,
constraints={},
Expand Down Expand Up @@ -60,7 +61,8 @@ def test_provider_known_depths(factory: Factory) -> None:
transitive_requirement_name = "my-transitive-package"

transitive_package_information = build_requirement_information(
name=transitive_requirement_name, parent=root_package_candidate
name=transitive_requirement_name,
parent=root_package_candidate, # type: ignore
)
provider.get_preference(
identifier=transitive_requirement_name,
Expand Down

0 comments on commit d309e2c

Please sign in to comment.