Skip to content

Commit 94fdffd

Browse files
committed
Make checking if direct and getting install requirements a one-pass
1 parent 1a83f72 commit 94fdffd

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
Iterable,
77
Iterator,
88
Mapping,
9+
Optional,
910
Sequence,
11+
Tuple,
1012
TypeVar,
1113
Union,
1214
)
1315

1416
from pip._vendor.resolvelib.providers import AbstractProvider
1517

18+
from pip._internal.req.req_install import InstallRequirement
19+
1620
from .base import Candidate, Constraint, Requirement
1721
from .candidates import REQUIRES_PYTHON_IDENTIFIER
1822
from .factory import Factory
@@ -180,21 +184,27 @@ def get_preference(
180184
else:
181185
has_information = True
182186

183-
if has_information:
184-
lookups = (r.get_candidate_lookup() for r, _ in information[identifier])
185-
_icandidates, ireqs = zip(*lookups)
187+
if not has_information:
188+
direct = False
189+
ireqs: Tuple[Optional[InstallRequirement], ...] = ()
186190
else:
187-
_icandidates, ireqs = (), ()
191+
# Go through the information and for each requirement,
192+
# check if it's explicit (e.g., a direct link) and get the
193+
# InstallRequirement (the second element) from get_candidate_lookup()
194+
directs, ireqs = zip(
195+
*(
196+
(isinstance(r, ExplicitRequirement), r.get_candidate_lookup()[1])
197+
for r, _ in information[identifier]
198+
)
199+
)
200+
direct = any(directs)
188201

189202
operators: list[tuple[str, str]] = [
190203
(specifier.operator, specifier.version)
191204
for specifier_set in (ireq.specifier for ireq in ireqs if ireq)
192205
for specifier in specifier_set
193206
]
194207

195-
direct = any(
196-
isinstance(r, ExplicitRequirement) for r, _ in information[identifier]
197-
)
198208
pinned = any(((op[:2] == "==") and ("*" not in ver)) for op, ver in operators)
199209
unfree = bool(operators)
200210
requested_order = self._user_requested.get(identifier, math.inf)

0 commit comments

Comments
 (0)