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

start_backtracking suggestion #81

Merged
merged 10 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions src/resolvelib/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def adding_requirement(self, requirement, parent):
requirements passed in from ``Resolver.resolve()``.
"""

def resolving_conflicts(self, causes):
"""Called when a collision in requirements was detected and
an attempt to resolve them was started
nadavwe marked this conversation as resolved.
Show resolved Hide resolved

:param causes: The information on the collision that caused the backtracking.
"""

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

Expand Down
1 change: 1 addition & 0 deletions src/resolvelib/reporters.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class BaseReporter:
def ending(self, state: Any) -> Any: ...
def adding_requirement(self, requirement: Any, parent: Any) -> Any: ...
def backtracking(self, candidate: Any) -> Any: ...
def resolving_conflicts(self, causes: Any) -> Any: ...
def pinning(self, candidate: Any) -> Any: ...
3 changes: 2 additions & 1 deletion src/resolvelib/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,14 @@ def resolve(self, requirements, max_rounds):
failure_causes = self._attempt_to_pin_criterion(name)

if failure_causes:
causes = [i for c in failure_causes for i in c.information]
# Backtrack if pinning fails. The backtrack process puts us in
# an unpinned state, so we can work on it in the next round.
self._r.resolving_conflicts(causes)
nadavwe marked this conversation as resolved.
Show resolved Hide resolved
success = self._backtrack()

# Dead ends everywhere. Give up.
if not success:
causes = [i for c in failure_causes for i in c.information]
raise ResolutionImpossible(causes)
else:
# Pinning was successful. Push a new state to do another pin.
Expand Down
52 changes: 52 additions & 0 deletions tests/test_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
BaseReporter,
InconsistentCandidate,
Resolver,
ResolutionImpossible,
)


Expand Down Expand Up @@ -91,3 +92,54 @@ def is_satisfied_by(self, requirement, candidate):

assert set(result.mapping) == {"parent", "child"}
assert result.mapping["child"] == ("child", "1", [])


def test_resolving_conflicts():
all_candidates = {
"a": [("a", 1, [("q", {1})]), ("a", 2, [("q", {2})])],
"b": [("b", 1, [("q", {1})])],
"q": [("q", 1, []), ("q", 2, [])],
}

class Reporter(BaseReporter):
def __init__(self):
self.backtracking_causes = None

def resolving_conflicts(self, failure_causes):
self.backtracking_causes = failure_causes

class Provider(AbstractProvider):
def identify(self, requirement_or_candidate):
return requirement_or_candidate[0]

def get_preference(self, **_):
return 0

def get_dependencies(self, candidate):
return candidate[2]

def find_matches(self, identifier, requirements, incompatibilities):
bad_versions = {c[1] for c in incompatibilities[identifier]}
candidates = [
c
for c in all_candidates[identifier]
if all(c[1] in r[1] for r in requirements[identifier])
and c[1] not in bad_versions
]
return sorted(candidates, key=lambda c: c[1], reverse=True)

def is_satisfied_by(self, requirement, candidate):
return candidate[1] in requirement[1]

def run_resolver(*args):
reporter = Reporter()
resolver = Resolver(Provider(), reporter)
try:
resolver.resolve(*args)
return reporter.backtracking_causes
except ResolutionImpossible as e:
return e.causes

backtracking_causes = run_resolver([("a", {1, 2}), ("b", {1})])
exception_causes = run_resolver([("a", {2}), ("b", {1})])
assert exception_causes == backtracking_causes
Comment on lines +143 to +145
Copy link
Contributor Author

@nadavwe nadavwe Jul 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to make it obvious that the backtracking causes are exactly the same as the failure when the "best" version is selected.
@uranusjr do you prefer I give an explicit value instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better IMO if you want to express the causes should be the same.