Skip to content

Commit

Permalink
Delegate to template-copying logic when combining install reqs
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Frank <rich@quantopian.com>
  • Loading branch information
atugushev and richafrank committed Jun 29, 2022
1 parent 7d5caa9 commit c5e6ce2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
16 changes: 3 additions & 13 deletions piptools/resolver.py
Expand Up @@ -139,24 +139,14 @@ def combine_install_requirements(
key=lambda x: (len(str(x)), str(x)),
)

combined_ireq = InstallRequirement(
combined_ireq = copy_install_requirement(
template=source_ireqs[0],
req=req,
comes_from=comes_from,
editable=source_ireqs[0].editable,
link=link_attrs["link"],
markers=source_ireqs[0].markers,
use_pep517=source_ireqs[0].use_pep517,
isolated=source_ireqs[0].isolated,
install_options=source_ireqs[0].install_options,
global_options=source_ireqs[0].global_options,
hash_options=source_ireqs[0].hash_options,
constraint=constraint,
extras=extras,
user_supplied=source_ireqs[0].user_supplied,
**link_attrs,
)
# e.g. If the original_link was None, keep it so. Passing `link` as an
# argument to `InstallRequirement` sets it as the original_link:
combined_ireq.original_link = link_attrs["original_link"]
combined_ireq._source_ireqs = source_ireqs

return combined_ireq
Expand Down
54 changes: 35 additions & 19 deletions piptools/utils.py
Expand Up @@ -7,6 +7,7 @@
import shlex
import typing
from typing import (
Any,
Callable,
Dict,
Iterable,
Expand Down Expand Up @@ -457,26 +458,41 @@ def strip_extras(name: str) -> str:
return re.sub(_strip_extras_re, "", name)


def copy_install_requirement(template: InstallRequirement) -> InstallRequirement:
"""Make a copy of an ``InstallRequirement``."""
ireq = InstallRequirement(
req=copy.deepcopy(template.req),
comes_from=template.comes_from,
editable=template.editable,
link=template.link,
markers=template.markers,
use_pep517=template.use_pep517,
isolated=template.isolated,
install_options=template.install_options,
global_options=template.global_options,
hash_options=template.hash_options,
constraint=template.constraint,
extras=template.extras,
user_supplied=template.user_supplied,
)
def copy_install_requirement(
template: InstallRequirement, **extra_kwargs: Any
) -> InstallRequirement:
"""Make a copy of a template ``InstallRequirement`` with extra kwargs."""
# Prepare install requirement kwargs.
kwargs = {
"comes_from": template.comes_from,
"editable": template.editable,
"link": template.link,
"markers": template.markers,
"use_pep517": template.use_pep517,
"isolated": template.isolated,
"install_options": template.install_options,
"global_options": template.global_options,
"hash_options": template.hash_options,
"constraint": template.constraint,
"extras": template.extras,
"user_supplied": template.user_supplied,
}
kwargs.update(extra_kwargs)

# Original link does not belong to install requirements constructor,
# pop it now to update later.
original_link = kwargs.pop("original_link", None)

# Copy template.req if not specified in extra kwargs.
if "req" not in kwargs:
kwargs["req"] = copy.deepcopy(template.req)

ireq = InstallRequirement(**kwargs)

# If the original_link was None, keep it so. Passing `link` as an
# argument to `InstallRequirement` sets it as the original_link:
ireq.original_link = template.original_link
# argument to `InstallRequirement` sets it as the original_link.
ireq.original_link = (
template.original_link if original_link is None else original_link
)

return ireq

0 comments on commit c5e6ce2

Please sign in to comment.