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

Update ResolveLib to 0.4.0 #8177

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/pip/_vendor/resolvelib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"ResolutionTooDeep",
]

__version__ = "0.3.0"
__version__ = "0.4.0"


from .providers import AbstractProvider, AbstractResolver
Expand Down
Empty file.
6 changes: 6 additions & 0 deletions src/pip/_vendor/resolvelib/compat/collections_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__all__ = ["Sequence"]

try:
from collections.abc import Sequence
except ImportError:
from collections import Sequence
52 changes: 20 additions & 32 deletions src/pip/_vendor/resolvelib/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_preference(self, resolution, candidates, information):

* `requirement` specifies a requirement contributing to the current
candidate list
* `parent` specifies the candidate that provids (dependend on) the
* `parent` specifies the candidate that provides (dependend on) the
requirement, or `None` to indicate a root requirement.

The preference could depend on a various of issues, including (not
Expand All @@ -48,23 +48,28 @@ def get_preference(self, resolution, candidates, information):
"""
raise NotImplementedError

def find_matches(self, requirement):
"""Find all possible candidates that satisfy a requirement.
def find_matches(self, requirements):
"""Find all possible candidates that satisfy the given requirements.

This should try to get candidates based on the requirement's type.
This should try to get candidates based on the requirements' types.
For VCS, local, and archive requirements, the one-and-only match is
returned, and for a "named" requirement, the index(es) should be
consulted to find concrete candidates for this requirement.

The returned candidates should be sorted by reversed preference, e.g.
the most preferred should be LAST. This is done so list-popping can be
as efficient as possible.
:param requirements: A collection of requirements which all of the the
returned candidates must match. All requirements are guaranteed to
have the same identifier. The collection is never empty.
:returns: An iterable that orders candidates by preference, e.g. the
most preferred candidate should come first.
"""
raise NotImplementedError

def is_satisfied_by(self, requirement, candidate):
"""Whether the given requirement can be satisfied by a candidate.

The candidate is guarenteed to have been generated from the
requirement.

A boolean should be returned to indicate whether `candidate` is a
viable solution to the requirement.
"""
Expand Down Expand Up @@ -92,30 +97,13 @@ def __init__(self, provider, reporter):
def resolve(self, requirements, **kwargs):
"""Take a collection of constraints, spit out the resolution result.

Parameters
----------
requirements : Collection
A collection of constraints
kwargs : optional
Additional keyword arguments that subclasses may accept.

Raises
------
self.base_exception
Any raised exception is guaranteed to be a subclass of
self.base_exception. The string representation of an exception
should be human readable and provide context for why it occurred.

Returns
-------
retval : object
A representation of the final resolution state. It can be any object
with a `mapping` attribute that is a Mapping. Other attributes can
be used to provide resolver-specific information.

The `mapping` attribute MUST be key-value pair is an identifier of a
requirement (as returned by the provider's `identify` method) mapped
to the resolved candidate (chosen from the return value of the
provider's `find_matches` method).
This returns a representation of the final resolution state, with one
guarenteed attribute ``mapping`` that contains resolved candidates as
values. The keys are their respective identifiers.

:param requirements: A collection of constraints.
:param kwargs: Additional keyword arguments that subclasses may accept.

:raises: ``self.base_exception`` or its subclass.
"""
raise NotImplementedError
12 changes: 9 additions & 3 deletions src/pip/_vendor/resolvelib/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ def ending(self, state):
"""Called before the resolution ends successfully.
"""

def adding_requirement(self, requirement):
"""Called when the resolver adds a new requirement into the resolve criteria.
def adding_requirement(self, requirement, parent):
"""Called when adding a new requirement into the resolve criteria.

:param requirement: The additional requirement to be applied to filter
the available candidaites.
:param parent: The candidate that requires ``requirement`` as a
dependency, or None if ``requirement`` is one of the root
requirements passed in from ``Resolver.resolve()``.
"""

def backtracking(self, candidate):
"""Called when the resolver rejects a candidate during backtracking.
"""Called when rejecting a candidate during backtracking.
"""

def pinning(self, candidate):
Expand Down
64 changes: 39 additions & 25 deletions src/pip/_vendor/resolvelib/resolvers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections

from .compat import collections_abc
from .providers import AbstractResolver
from .structs import DirectedGraph

Expand Down Expand Up @@ -68,16 +69,18 @@ def __init__(self, candidates, information, incompatibilities):

def __repr__(self):
requirements = ", ".join(
"{!r} from {!r}".format(req, parent)
"({!r}, via={!r})".format(req, parent)
for req, parent in self.information
)
return "<Criterion {}>".format(requirements)
return "Criterion({})".format(requirements)

@classmethod
def from_requirement(cls, provider, requirement, parent):
"""Build an instance from a requirement.
"""
candidates = provider.find_matches(requirement)
candidates = provider.find_matches([requirement])
if not isinstance(candidates, collections_abc.Sequence):
candidates = list(candidates)
criterion = cls(
candidates=candidates,
information=[RequirementInformation(requirement, parent)],
Expand All @@ -98,11 +101,9 @@ def merged_with(self, provider, requirement, parent):
"""
infos = list(self.information)
infos.append(RequirementInformation(requirement, parent))
candidates = [
c
for c in self.candidates
if provider.is_satisfied_by(requirement, c)
]
candidates = provider.find_matches([r for r, _ in infos])
if not isinstance(candidates, collections_abc.Sequence):
candidates = list(candidates)
criterion = type(self)(candidates, infos, list(self.incompatibilities))
if not candidates:
raise RequirementsConflicted(criterion)
Expand Down Expand Up @@ -179,7 +180,7 @@ def _push_new_state(self):
self._states.append(state)

def _merge_into_criterion(self, requirement, parent):
self._r.adding_requirement(requirement)
self._r.adding_requirement(requirement, parent)
name = self._p.identify(requirement)
try:
crit = self.state.criteria[name]
Expand Down Expand Up @@ -218,51 +219,64 @@ def _get_criteria_to_update(self, candidate):

def _attempt_to_pin_criterion(self, name, criterion):
causes = []
for candidate in reversed(criterion.candidates):
for candidate in criterion.candidates:
try:
criteria = self._get_criteria_to_update(candidate)
except RequirementsConflicted as e:
causes.append(e.criterion)
continue

# Check the newly-pinned candidate actually works. This should
# always pass under normal circumstances, but in the case of a
# faulty provider, we will raise an error to notify the implementer
# to fix find_matches() and/or is_satisfied_by().
satisfied = all(
self._p.is_satisfied_by(r, candidate)
for r in criterion.iter_requirement()
)
if not satisfied:
raise InconsistentCandidate(candidate, criterion)

# Put newly-pinned candidate at the end. This is essential because
# backtracking looks at this mapping to get the last pin.
self._r.pinning(candidate)
self.state.mapping.pop(name, None)
self.state.mapping[name] = candidate
self.state.criteria.update(criteria)

# Check the newly-pinned candidate actually works. This should
# always pass under normal circumstances, but in the case of a
# faulty provider, we will raise an error to notify the implementer
# to fix find_matches() and/or is_satisfied_by().
if not self._is_current_pin_satisfying(name, criterion):
raise InconsistentCandidate(candidate, criterion)

return []

# All candidates tried, nothing works. This criterion is a dead
# end, signal for backtracking.
return causes

def _backtrack(self):
# We need at least 3 states here:
# (a) One known not working, to drop.
# (b) One to backtrack to.
# (c) One to restore state (b) to its state prior to candidate-pinning,
# Drop the current state, it's known not to work.
del self._states[-1]

# We need at least 2 states here:
# (a) One to backtrack to.
# (b) One to restore state (a) to its state prior to candidate-pinning,
# so we can pin another one instead.
while len(self._states) >= 3:
del self._states[-1]

# Retract the last candidate pin, and create a new (b).
name, candidate = self._states.pop().mapping.popitem()
while len(self._states) >= 2:
# Retract the last candidate pin.
prev_state = self._states.pop()
try:
name, candidate = prev_state.mapping.popitem()
except KeyError:
continue
self._r.backtracking(candidate)

# Create a new state to work on, with the newly known not-working
# candidate excluded.
self._push_new_state()

# Mark the retracted candidate as incompatible.
criterion = self.state.criteria[name].excluded_of(candidate)
if criterion is None:
# This state still does not work. Try the still previous state.
del self._states[-1]
continue
self.state.criteria[name] = criterion

Expand Down
2 changes: 1 addition & 1 deletion src/pip/_vendor/vendor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ requests==2.23.0
chardet==3.0.4
idna==2.9
urllib3==1.25.8
resolvelib==0.3.0
resolvelib==0.4.0
retrying==1.3.3
setuptools==44.0.0
six==1.14.0
Expand Down