Skip to content

Commit af92b41

Browse files
authored
Merge pull request #13001 from notatallshaw/vendor-resolvelib-1.1.0
Vendor resolvelib 1.1.0
2 parents de44d99 + b4c2ffd commit af92b41

26 files changed

+624
-625
lines changed

docs/html/topics/more-dependency-resolution.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ follows:
160160
explicit URL.
161161
* If equal, prefer if any requirement is "pinned", i.e. contains
162162
operator ``===`` or ``==``.
163-
* If equal, calculate an approximate "depth" and resolve requirements
164-
closer to the user-specified requirements first.
165163
* Order user-specified requirements by the order they are specified.
166164
* If equal, prefers "non-free" requirements, i.e. contains at least one
167165
operator, such as ``>=`` or ``<``.

news/13001.bugfix.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Resolvelib 1.1.0 fixes a known issue where pip would report a
2+
ResolutionImpossible error even though there is a valid solution.
3+
However, some very complex dependency resolutions that previously
4+
resolved may resolve slower or fail with an ResolutionTooDeep error.

news/resolvelib.vendor.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Upgrade resolvelib to 1.1.0.

src/pip/_internal/resolution/resolvelib/factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ def get_installation_error(
748748
# The simplest case is when we have *one* cause that can't be
749749
# satisfied. We just report that case.
750750
if len(e.causes) == 1:
751-
req, parent = e.causes[0]
751+
req, parent = next(iter(e.causes))
752752
if req.name not in constraints:
753753
return self._report_single_requirement_conflict(req, parent)
754754

src/pip/_internal/resolution/resolvelib/provider.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import collections
21
import math
32
from functools import lru_cache
43
from typing import (
@@ -100,7 +99,6 @@ def __init__(
10099
self._ignore_dependencies = ignore_dependencies
101100
self._upgrade_strategy = upgrade_strategy
102101
self._user_requested = user_requested
103-
self._known_depths: Dict[str, float] = collections.defaultdict(lambda: math.inf)
104102

105103
def identify(self, requirement_or_candidate: Union[Requirement, Candidate]) -> str:
106104
return requirement_or_candidate.name
@@ -124,10 +122,6 @@ def get_preference(
124122
explicit URL.
125123
* If equal, prefer if any requirement is "pinned", i.e. contains
126124
operator ``===`` or ``==``.
127-
* If equal, calculate an approximate "depth" and resolve requirements
128-
closer to the user-specified requirements first. If the depth cannot
129-
by determined (eg: due to no matching parents), it is considered
130-
infinite.
131125
* Order user-specified requirements by the order they are specified.
132126
* If equal, prefers "non-free" requirements, i.e. contains at least one
133127
operator, such as ``>=`` or ``<``.
@@ -157,23 +151,6 @@ def get_preference(
157151
direct = candidate is not None
158152
pinned = any(op[:2] == "==" for op in operators)
159153
unfree = bool(operators)
160-
161-
try:
162-
requested_order: Union[int, float] = self._user_requested[identifier]
163-
except KeyError:
164-
requested_order = math.inf
165-
if has_information:
166-
parent_depths = (
167-
self._known_depths[parent.name] if parent is not None else 0.0
168-
for _, parent in information[identifier]
169-
)
170-
inferred_depth = min(d for d in parent_depths) + 1.0
171-
else:
172-
inferred_depth = math.inf
173-
else:
174-
inferred_depth = 1.0
175-
self._known_depths[identifier] = inferred_depth
176-
177154
requested_order = self._user_requested.get(identifier, math.inf)
178155

179156
# Requires-Python has only one candidate and the check is basically
@@ -190,7 +167,6 @@ def get_preference(
190167
not direct,
191168
not pinned,
192169
not backtrack_cause,
193-
inferred_depth,
194170
requested_order,
195171
not unfree,
196172
identifier,

src/pip/_internal/resolution/resolvelib/reporter.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import defaultdict
22
from logging import getLogger
3-
from typing import Any, DefaultDict
3+
from typing import Any, DefaultDict, Optional
44

55
from pip._vendor.resolvelib.reporters import BaseReporter
66

@@ -9,7 +9,7 @@
99
logger = getLogger(__name__)
1010

1111

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

@@ -55,7 +55,7 @@ def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:
5555
logger.debug(msg)
5656

5757

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

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

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

7779
def rejecting_candidate(self, criterion: Any, candidate: Candidate) -> None:

src/pip/_internal/resolution/resolvelib/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def resolve(
8282
user_requested=collected.user_requested,
8383
)
8484
if "PIP_RESOLVER_DEBUG" in os.environ:
85-
reporter: BaseReporter = PipDebuggingReporter()
85+
reporter: BaseReporter[Requirement, Candidate, str] = PipDebuggingReporter()
8686
else:
8787
reporter = PipReporter()
8888
resolver: RLResolver[Requirement, Candidate, str] = RLResolver(

src/pip/_vendor/resolvelib/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
"ResolutionTooDeep",
1212
]
1313

14-
__version__ = "1.0.1"
14+
__version__ = "1.1.0"
1515

1616

17-
from .providers import AbstractProvider, AbstractResolver
17+
from .providers import AbstractProvider
1818
from .reporters import BaseReporter
1919
from .resolvers import (
20+
AbstractResolver,
2021
InconsistentCandidate,
2122
RequirementsConflicted,
2223
ResolutionError,

src/pip/_vendor/resolvelib/__init__.pyi

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/pip/_vendor/resolvelib/compat/__init__.py

Whitespace-only changes.

src/pip/_vendor/resolvelib/compat/collections_abc.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/pip/_vendor/resolvelib/compat/collections_abc.pyi

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,68 @@
1-
class AbstractProvider(object):
1+
from __future__ import annotations
2+
3+
from typing import (
4+
TYPE_CHECKING,
5+
Generic,
6+
Iterable,
7+
Iterator,
8+
Mapping,
9+
Sequence,
10+
)
11+
12+
from .structs import CT, KT, RT, Matches, RequirementInformation
13+
14+
if TYPE_CHECKING:
15+
from typing import Any, Protocol
16+
17+
class Preference(Protocol):
18+
def __lt__(self, __other: Any) -> bool: ...
19+
20+
21+
class AbstractProvider(Generic[RT, CT, KT]):
222
"""Delegate class to provide the required interface for the resolver."""
323

4-
def identify(self, requirement_or_candidate):
5-
"""Given a requirement, return an identifier for it.
24+
def identify(self, requirement_or_candidate: RT | CT) -> KT:
25+
"""Given a requirement or candidate, return an identifier for it.
626
7-
This is used to identify a requirement, e.g. whether two requirements
8-
should have their specifier parts merged.
27+
This is used to identify, e.g. whether two requirements
28+
should have their specifier parts merged or a candidate matches a
29+
requirement via ``find_matches()``.
930
"""
1031
raise NotImplementedError
1132

1233
def get_preference(
1334
self,
14-
identifier,
15-
resolutions,
16-
candidates,
17-
information,
18-
backtrack_causes,
19-
):
35+
identifier: KT,
36+
resolutions: Mapping[KT, CT],
37+
candidates: Mapping[KT, Iterator[CT]],
38+
information: Mapping[KT, Iterator[RequirementInformation[RT, CT]]],
39+
backtrack_causes: Sequence[RequirementInformation[RT, CT]],
40+
) -> Preference:
2041
"""Produce a sort key for given requirement based on preference.
2142
43+
As this is a sort key it will be called O(n) times per backtrack
44+
step, where n is the number of `identifier`s, if you have a check
45+
which is expensive in some sense. E.g. It needs to make O(n) checks
46+
per call or takes significant wall clock time, consider using
47+
`narrow_requirement_selection` to filter the `identifier`s, which
48+
is applied before this sort key is called.
49+
2250
The preference is defined as "I think this requirement should be
2351
resolved first". The lower the return value is, the more preferred
2452
this group of arguments is.
2553
2654
:param identifier: An identifier as returned by ``identify()``. This
27-
identifies the dependency matches which should be returned.
55+
identifies the requirement being considered.
2856
:param resolutions: Mapping of candidates currently pinned by the
2957
resolver. Each key is an identifier, and the value is a candidate.
3058
The candidate may conflict with requirements from ``information``.
3159
:param candidates: Mapping of each dependency's possible candidates.
3260
Each value is an iterator of candidates.
3361
:param information: Mapping of requirement information of each package.
3462
Each value is an iterator of *requirement information*.
35-
:param backtrack_causes: Sequence of requirement information that were
36-
the requirements that caused the resolver to most recently backtrack.
63+
:param backtrack_causes: Sequence of *requirement information* that are
64+
the requirements that caused the resolver to most recently
65+
backtrack.
3766
3867
A *requirement information* instance is a named tuple with two members:
3968
@@ -60,15 +89,21 @@ def get_preference(
6089
"""
6190
raise NotImplementedError
6291

63-
def find_matches(self, identifier, requirements, incompatibilities):
92+
def find_matches(
93+
self,
94+
identifier: KT,
95+
requirements: Mapping[KT, Iterator[RT]],
96+
incompatibilities: Mapping[KT, Iterator[CT]],
97+
) -> Matches[CT]:
6498
"""Find all possible candidates that satisfy the given constraints.
6599
66-
:param identifier: An identifier as returned by ``identify()``. This
67-
identifies the dependency matches of which should be returned.
100+
:param identifier: An identifier as returned by ``identify()``. All
101+
candidates returned by this method should produce the same
102+
identifier.
68103
:param requirements: A mapping of requirements that all returned
69104
candidates must satisfy. Each key is an identifier, and the value
70105
an iterator of requirements for that dependency.
71-
:param incompatibilities: A mapping of known incompatibilities of
106+
:param incompatibilities: A mapping of known incompatibile candidates of
72107
each dependency. Each key is an identifier, and the value an
73108
iterator of incompatibilities known to the resolver. All
74109
incompatibilities *must* be excluded from the return value.
@@ -89,7 +124,7 @@ def find_matches(self, identifier, requirements, incompatibilities):
89124
"""
90125
raise NotImplementedError
91126

92-
def is_satisfied_by(self, requirement, candidate):
127+
def is_satisfied_by(self, requirement: RT, candidate: CT) -> bool:
93128
"""Whether the given requirement can be satisfied by a candidate.
94129
95130
The candidate is guaranteed to have been generated from the
@@ -100,34 +135,62 @@ def is_satisfied_by(self, requirement, candidate):
100135
"""
101136
raise NotImplementedError
102137

103-
def get_dependencies(self, candidate):
138+
def get_dependencies(self, candidate: CT) -> Iterable[RT]:
104139
"""Get dependencies of a candidate.
105140
106141
This should return a collection of requirements that `candidate`
107142
specifies as its dependencies.
108143
"""
109144
raise NotImplementedError
110145

146+
def narrow_requirement_selection(
147+
self,
148+
identifiers: Iterable[KT],
149+
resolutions: Mapping[KT, CT],
150+
candidates: Mapping[KT, Iterator[CT]],
151+
information: Mapping[KT, Iterator[RequirementInformation[RT, CT]]],
152+
backtrack_causes: Sequence[RequirementInformation[RT, CT]],
153+
) -> Iterable[KT]:
154+
"""
155+
An optional method to narrow the selection of requirements being
156+
considered during resolution. This method is called O(1) time per
157+
backtrack step.
158+
159+
:param identifiers: An iterable of `identifiers` as returned by
160+
``identify()``. These identify all requirements currently being
161+
considered.
162+
:param resolutions: A mapping of candidates currently pinned by the
163+
resolver. Each key is an identifier, and the value is a candidate
164+
that may conflict with requirements from ``information``.
165+
:param candidates: A mapping of each dependency's possible candidates.
166+
Each value is an iterator of candidates.
167+
:param information: A mapping of requirement information for each package.
168+
Each value is an iterator of *requirement information*.
169+
:param backtrack_causes: A sequence of *requirement information* that are
170+
the requirements causing the resolver to most recently
171+
backtrack.
111172
112-
class AbstractResolver(object):
113-
"""The thing that performs the actual resolution work."""
114-
115-
base_exception = Exception
116-
117-
def __init__(self, provider, reporter):
118-
self.provider = provider
119-
self.reporter = reporter
120-
121-
def resolve(self, requirements, **kwargs):
122-
"""Take a collection of constraints, spit out the resolution result.
123-
124-
This returns a representation of the final resolution state, with one
125-
guarenteed attribute ``mapping`` that contains resolved candidates as
126-
values. The keys are their respective identifiers.
127-
128-
:param requirements: A collection of constraints.
129-
:param kwargs: Additional keyword arguments that subclasses may accept.
173+
A *requirement information* instance is a named tuple with two members:
130174
131-
:raises: ``self.base_exception`` or its subclass.
175+
* ``requirement`` specifies a requirement contributing to the current
176+
list of candidates.
177+
* ``parent`` specifies the candidate that provides (is depended on for)
178+
the requirement, or ``None`` to indicate a root requirement.
179+
180+
Must return a non-empty subset of `identifiers`, with the default
181+
implementation being to return `identifiers` unchanged. Those `identifiers`
182+
will then be passed to the sort key `get_preference` to pick the most
183+
prefered requirement to attempt to pin, unless `narrow_requirement_selection`
184+
returns only 1 requirement, in which case that will be used without
185+
calling the sort key `get_preference`.
186+
187+
This method is designed to be used by the provider to optimize the
188+
dependency resolution, e.g. if a check cost is O(m) and it can be done
189+
against all identifiers at once then filtering the requirement selection
190+
here will cost O(m) but making it part of the sort key in `get_preference`
191+
will cost O(m*n), where n is the number of `identifiers`.
192+
193+
Returns:
194+
Iterable[KT]: A non-empty subset of `identifiers`.
132195
"""
133-
raise NotImplementedError
196+
return identifiers

0 commit comments

Comments
 (0)