Skip to content

Commit d0f7a23

Browse files
committed
Rework resolution ordering to consider "depth"
1 parent 7c3abcc commit d0f7a23

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

news/9455.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
New resolver: The order of dependencies resolution has been tweaked to traverse
2+
the dependency graph in a more breadth-first approach.

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

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(
6060
self._ignore_dependencies = ignore_dependencies
6161
self._upgrade_strategy = upgrade_strategy
6262
self._user_requested = user_requested
63+
self._known_depths = {key: 1.0 for key in user_requested}
6364

6465
def identify(self, requirement_or_candidate):
6566
# type: (Union[Requirement, Candidate]) -> str
@@ -79,48 +80,37 @@ def get_preference(
7980
8081
Currently pip considers the followings in order:
8182
82-
* Prefer if any of the known requirements points to an explicit URL.
83-
* If equal, prefer if any requirements contain ``===`` and ``==``.
84-
* If equal, prefer if requirements include version constraints, e.g.
85-
``>=`` and ``<``.
86-
* If equal, prefer user-specified (non-transitive) requirements, and
87-
order user-specified requirements by the order they are specified.
83+
* Prefer if any of the known requirements is "direct", e.g. points to an
84+
explicit URL.
85+
* If equal, prefer if any requirement is "pinned", i.e. contains
86+
operator ``===`` or ``==``.
87+
* If equal, calculate an approximate "depth" and resolve requirements
88+
closer to the user-specified requirements first.
89+
* Order user-specified requirements by the order they are specified.
90+
* If equal, prefers "non-free" requirements, i.e. contains at least one
91+
operator, such as ``>=`` or ``<``.
8892
* If equal, order alphabetically for consistency (helps debuggability).
8993
"""
94+
lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
95+
candidate, ireqs = zip(*lookups)
96+
operators = [
97+
specifier.operator
98+
for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
99+
for specifier in specifier_set
100+
]
101+
102+
direct = candidate is not None
103+
pinned = any(op[:2] == "==" for op in operators)
104+
unfree = bool(operators)
105+
106+
parent_depths = (
107+
self._known_depths.get(parent.name, float("inf")) if parent else 0.0
108+
for _, parent in information[identifier]
109+
)
110+
inferred_depth = min(d for d in parent_depths if d is not None) + 1.0
111+
depth = self._known_depths[identifier] = inferred_depth
90112

91-
def _get_restrictive_rating(requirements):
92-
# type: (Iterable[Requirement]) -> int
93-
"""Rate how restrictive a set of requirements are.
94-
95-
``Requirement.get_candidate_lookup()`` returns a 2-tuple for
96-
lookup. The first element is ``Optional[Candidate]`` and the
97-
second ``Optional[InstallRequirement]``.
98-
99-
* If the requirement is an explicit one, the explicitly-required
100-
candidate is returned as the first element.
101-
* If the requirement is based on a PEP 508 specifier, the backing
102-
``InstallRequirement`` is returned as the second element.
103-
104-
We use the first element to check whether there is an explicit
105-
requirement, and the second for equality operator.
106-
"""
107-
lookups = (r.get_candidate_lookup() for r in requirements)
108-
cands, ireqs = zip(*lookups)
109-
if any(cand is not None for cand in cands):
110-
return 0
111-
spec_sets = (ireq.specifier for ireq in ireqs if ireq)
112-
operators = [
113-
specifier.operator for spec_set in spec_sets for specifier in spec_set
114-
]
115-
if any(op in ("==", "===") for op in operators):
116-
return 1
117-
if operators:
118-
return 2
119-
# A "bare" requirement without any version requirements.
120-
return 3
121-
122-
rating = _get_restrictive_rating(r for r, _ in information[identifier])
123-
order = self._user_requested.get(identifier, float("inf"))
113+
reqeusted_order = self._user_requested.get(identifier, float("inf"))
124114

125115
# Requires-Python has only one candidate and the check is basically
126116
# free, so we always do it first to avoid needless work if it fails.
@@ -136,7 +126,16 @@ def _get_restrictive_rating(requirements):
136126
# while we work on "proper" branch pruning techniques.
137127
delay_this = identifier == "setuptools"
138128

139-
return (not requires_python, delay_this, rating, order, identifier)
129+
return (
130+
not requires_python,
131+
delay_this,
132+
not direct,
133+
not pinned,
134+
depth,
135+
reqeusted_order,
136+
not unfree,
137+
identifier,
138+
)
140139

141140
def find_matches(
142141
self,

0 commit comments

Comments
 (0)