Skip to content

Commit

Permalink
Repurpose backtracking hook
Browse files Browse the repository at this point in the history
  • Loading branch information
astrojuanlu committed Mar 4, 2022
1 parent f47bc88 commit c2a4343
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 44 deletions.
45 changes: 9 additions & 36 deletions src/pip/_internal/resolution/resolvelib/reporter.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
from collections import defaultdict
from logging import getLogger
from typing import Any, DefaultDict
from typing import Any

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):
def __init__(self) -> None:
self.backtracks_by_package: DefaultDict[str, int] = defaultdict(int)

self._messages_at_backtrack = {
1: (
"pip is looking at multiple versions of {package_name} to "
"determine which version is compatible with other "
"requirements. This could take a while."
),
8: (
"pip is looking at multiple versions of {package_name} to "
"determine which version is compatible with other "
"requirements. This could take a while."
),
13: (
"This is taking longer than usual. You might need to provide "
"the dependency resolver with stricter constraints to reduce "
"runtime. See https://pip.pypa.io/warnings/backtracking for "
"guidance. If you want to abort this run, press Ctrl + C."
),
}

def discarding_conflicted_criterion(self, criterion, candidate) -> None:
def backtracking(
self, criterion: Criterion[Any, Any, Any], candidate: Candidate
) -> None:
msg = "Will try a different candidate, due to conflict:"
for req_info in criterion.information:
req, parent = req_info.requirement, req_info.parent
Expand All @@ -43,17 +23,8 @@ def discarding_conflicted_criterion(self, criterion, candidate) -> None:
else:
msg += "The user requested "
msg += req.format_for_error()
logger.info(msg)

def backtracking(self, candidate: Candidate) -> None:
self.backtracks_by_package[candidate.name] += 1

count = self.backtracks_by_package[candidate.name]
if count not in self._messages_at_backtrack:
return

message = self._messages_at_backtrack[count]
logger.info("INFO: %s", message.format(package_name=candidate.name))
logger.info("INFO: %s", msg)


class PipDebuggingReporter(BaseReporter):
Expand All @@ -74,7 +45,9 @@ def ending(self, state: Any) -> None:
def adding_requirement(self, requirement: Requirement, parent: Candidate) -> None:
logger.info("Reporter.adding_requirement(%r, %r)", requirement, parent)

def backtracking(self, candidate: Candidate) -> None:
def backtracking(
self, criterion: Criterion[Any, Any, Any], candidate: Candidate
) -> None:
logger.info("Reporter.backtracking(%r)", candidate)

def pinning(self, candidate: Candidate) -> None:
Expand Down
5 changes: 1 addition & 4 deletions src/pip/_vendor/resolvelib/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ def adding_requirement(self, requirement, parent):
requirements passed in from ``Resolver.resolve()``.
"""

def discarding_conflicted_criterion(self, criterion, candidate):
"""Called when discarding conflicted requirements."""

def resolving_conflicts(self, causes):
"""Called when starting to attempt requirement conflict resolution.
:param causes: The information on the collision that caused the backtracking.
"""

def backtracking(self, candidate):
def backtracking(self, criterion, candidate):
"""Called when rejecting a candidate during backtracking."""

def pinning(self, candidate):
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_vendor/resolvelib/reporters.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class BaseReporter:
def ending_round(self, index: int, state: Any) -> Any: ...
def ending(self, state: Any) -> Any: ...
def adding_requirement(self, requirement: Any, parent: Any) -> Any: ...
def backtracking(self, candidate: Any) -> Any: ...
def backtracking(self, criterion: Any, candidate: Any) -> Any: ...
def resolving_conflicts(self, causes: Any) -> Any: ...
def pinning(self, candidate: Any) -> Any: ...
4 changes: 1 addition & 3 deletions src/pip/_vendor/resolvelib/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _attempt_to_pin_criterion(self, name):
try:
criteria = self._get_updated_criteria(candidate)
except RequirementsConflicted as e:
self._r.discarding_conflicted_criterion(e.criterion, candidate)
self._r.backtracking(e.criterion, candidate)
causes.append(e.criterion)
continue

Expand Down Expand Up @@ -282,8 +282,6 @@ def _backtrack(self):
# Also mark the newly known incompatibility.
incompatibilities_from_broken.append((name, [candidate]))

self._r.backtracking(candidate=candidate)

# Create a new state from the last known-to-work one, and apply
# the previously gathered incompatibility information.
def _patch_criteria():
Expand Down

0 comments on commit c2a4343

Please sign in to comment.