Skip to content

Commit c5e6ce2

Browse files
atugushevrichafrank
andcommitted
Delegate to template-copying logic when combining install reqs
Co-authored-by: Richard Frank <[email protected]>
1 parent 7d5caa9 commit c5e6ce2

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

piptools/resolver.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,14 @@ def combine_install_requirements(
139139
key=lambda x: (len(str(x)), str(x)),
140140
)
141141

142-
combined_ireq = InstallRequirement(
142+
combined_ireq = copy_install_requirement(
143+
template=source_ireqs[0],
143144
req=req,
144145
comes_from=comes_from,
145-
editable=source_ireqs[0].editable,
146-
link=link_attrs["link"],
147-
markers=source_ireqs[0].markers,
148-
use_pep517=source_ireqs[0].use_pep517,
149-
isolated=source_ireqs[0].isolated,
150-
install_options=source_ireqs[0].install_options,
151-
global_options=source_ireqs[0].global_options,
152-
hash_options=source_ireqs[0].hash_options,
153146
constraint=constraint,
154147
extras=extras,
155-
user_supplied=source_ireqs[0].user_supplied,
148+
**link_attrs,
156149
)
157-
# e.g. If the original_link was None, keep it so. Passing `link` as an
158-
# argument to `InstallRequirement` sets it as the original_link:
159-
combined_ireq.original_link = link_attrs["original_link"]
160150
combined_ireq._source_ireqs = source_ireqs
161151

162152
return combined_ireq

piptools/utils.py

+35-19
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import shlex
88
import typing
99
from typing import (
10+
Any,
1011
Callable,
1112
Dict,
1213
Iterable,
@@ -457,26 +458,41 @@ def strip_extras(name: str) -> str:
457458
return re.sub(_strip_extras_re, "", name)
458459

459460

460-
def copy_install_requirement(template: InstallRequirement) -> InstallRequirement:
461-
"""Make a copy of an ``InstallRequirement``."""
462-
ireq = InstallRequirement(
463-
req=copy.deepcopy(template.req),
464-
comes_from=template.comes_from,
465-
editable=template.editable,
466-
link=template.link,
467-
markers=template.markers,
468-
use_pep517=template.use_pep517,
469-
isolated=template.isolated,
470-
install_options=template.install_options,
471-
global_options=template.global_options,
472-
hash_options=template.hash_options,
473-
constraint=template.constraint,
474-
extras=template.extras,
475-
user_supplied=template.user_supplied,
476-
)
461+
def copy_install_requirement(
462+
template: InstallRequirement, **extra_kwargs: Any
463+
) -> InstallRequirement:
464+
"""Make a copy of a template ``InstallRequirement`` with extra kwargs."""
465+
# Prepare install requirement kwargs.
466+
kwargs = {
467+
"comes_from": template.comes_from,
468+
"editable": template.editable,
469+
"link": template.link,
470+
"markers": template.markers,
471+
"use_pep517": template.use_pep517,
472+
"isolated": template.isolated,
473+
"install_options": template.install_options,
474+
"global_options": template.global_options,
475+
"hash_options": template.hash_options,
476+
"constraint": template.constraint,
477+
"extras": template.extras,
478+
"user_supplied": template.user_supplied,
479+
}
480+
kwargs.update(extra_kwargs)
481+
482+
# Original link does not belong to install requirements constructor,
483+
# pop it now to update later.
484+
original_link = kwargs.pop("original_link", None)
485+
486+
# Copy template.req if not specified in extra kwargs.
487+
if "req" not in kwargs:
488+
kwargs["req"] = copy.deepcopy(template.req)
489+
490+
ireq = InstallRequirement(**kwargs)
477491

478492
# If the original_link was None, keep it so. Passing `link` as an
479-
# argument to `InstallRequirement` sets it as the original_link:
480-
ireq.original_link = template.original_link
493+
# argument to `InstallRequirement` sets it as the original_link.
494+
ireq.original_link = (
495+
template.original_link if original_link is None else original_link
496+
)
481497

482498
return ireq

0 commit comments

Comments
 (0)