Skip to content

Commit e326ca3

Browse files
streamline the RequirementSetWithCandidates invocation
1 parent db601c6 commit e326ca3

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

src/pip/_internal/commands/download.py

+26-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pip._internal.cli.status_codes import SUCCESS
1414
from pip._internal.models.link import Link, LinkHash
1515
from pip._internal.req.req_tracker import get_requirement_tracker
16+
from pip._internal.resolution.base import RequirementSetWithCandidates
1617
from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
1718
from pip._internal.utils.temp_dir import TempDirectory
1819

@@ -236,24 +237,32 @@ def run(self, options: Values, args: List[str]) -> int:
236237
downloaded.append(req.name)
237238

238239
download_infos = DownloadInfos()
239-
assert requirement_set.candidates is not None
240-
for candidate in requirement_set.candidates.mapping.values():
241-
# This will occur for the python version requirement, for example.
242-
if candidate.name not in requirement_set.requirements:
243-
download_infos.implicit_requirements.append(
244-
candidate.as_serializable_requirement()
240+
if options.print_download_urls:
241+
if isinstance(requirement_set, RequirementSetWithCandidates):
242+
for candidate in requirement_set.candidates.mapping.values():
243+
# This will occur for the python version requirement, for example.
244+
if candidate.name not in requirement_set.requirements:
245+
download_infos.implicit_requirements.append(
246+
candidate.as_serializable_requirement()
247+
)
248+
continue
249+
req = requirement_set.requirements[candidate.name]
250+
assert req.name is not None
251+
assert req.link is not None
252+
assert req.name not in download_infos.resolution
253+
download_infos.resolution[
254+
req.name
255+
] = RequirementDownloadInfo.from_req_and_link(
256+
req=candidate.as_serializable_requirement(),
257+
link=req.link,
258+
)
259+
else:
260+
logger.warning(
261+
"--print-download-urls is being used with the legacy resolver. "
262+
"The legacy resolver does not retain detailed dependency "
263+
"information, so all the fields in the output JSON file "
264+
"will be empty."
245265
)
246-
continue
247-
req = requirement_set.requirements[candidate.name]
248-
assert req.name is not None
249-
assert req.link is not None
250-
assert req.name not in download_infos.resolution
251-
download_infos.resolution[
252-
req.name
253-
] = RequirementDownloadInfo.from_req_and_link(
254-
req=candidate.as_serializable_requirement(),
255-
link=req.link,
256-
)
257266

258267
if downloaded:
259268
write_output("Successfully downloaded %s", " ".join(downloaded))

src/pip/_internal/resolution/base.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import abc
12
from typing import TYPE_CHECKING, Callable, List, Optional
23

34
from pip._internal.req.req_install import InstallRequirement
@@ -16,18 +17,24 @@
1617

1718

1819
class RequirementSetWithCandidates(RequirementSet):
19-
def __init__(self, check_supported_wheels: bool = True) -> None:
20+
def __init__(
21+
self,
22+
candidates: "Result",
23+
check_supported_wheels: bool = True,
24+
) -> None:
25+
self.candidates = candidates
2026
super().__init__(check_supported_wheels=check_supported_wheels)
21-
self.candidates: Optional[Result] = None
2227

2328

24-
class BaseResolver:
29+
class BaseResolver(metaclass=abc.ABCMeta):
30+
@abc.abstractmethod
2531
def resolve(
2632
self, root_reqs: List[InstallRequirement], check_supported_wheels: bool
27-
) -> RequirementSetWithCandidates:
28-
raise NotImplementedError()
33+
) -> RequirementSet:
34+
...
2935

36+
@abc.abstractmethod
3037
def get_installation_order(
3138
self, req_set: RequirementSet
3239
) -> List[InstallRequirement]:
33-
raise NotImplementedError()
40+
...

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ def resolve(
107107
raise error from e
108108

109109
req_set = RequirementSetWithCandidates(
110-
check_supported_wheels=check_supported_wheels
110+
candidates=result, check_supported_wheels=check_supported_wheels
111111
)
112-
req_set.candidates = result
113112
for candidate in result.mapping.values():
114113
ireq = candidate.get_install_requirement()
115114
if ireq is None:

0 commit comments

Comments
 (0)