Skip to content

Commit

Permalink
Partially revert "Fix a regression where multiple declarations of a d…
Browse files Browse the repository at this point in the history
…ependency"

(commit d720fe6)

This brings back copy_ireq_dependencies and combines its use with that of the
proxies-for-naming. Editable and url_requirement ireqs are not cached by the
Repository, so when dependencies are queried for them once, no dependencies
will be found later for the copy made by combine_install_requirements.

copy_ireq_dependencies links those dependencies at the time of copy.
  • Loading branch information
richafrank committed May 25, 2022
1 parent 2537562 commit 828e2d6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions piptools/repositories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def allow_all_wheels(self) -> Iterator[None]:
Monkey patches pip.Wheel to allow wheels from all platforms and Python versions.
"""

@abstractmethod
def copy_ireq_dependencies(
self, source: InstallRequirement, dest: InstallRequirement
) -> None:
"""
Notifies the repository that `dest` is a copy of `source`, and so it
has the same dependencies. Otherwise, once we prepare an ireq to assign
it its name, we would lose track of those dependencies on combining
that ireq with others.
"""

@property
@abstractmethod
def options(self) -> optparse.Values:
Expand Down
5 changes: 5 additions & 0 deletions piptools/repositories/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@ def get_hashes(self, ireq: InstallRequirement) -> Set[str]:
def allow_all_wheels(self) -> Iterator[None]:
with self.repository.allow_all_wheels():
yield

def copy_ireq_dependencies(
self, source: InstallRequirement, dest: InstallRequirement
) -> None:
self.repository.copy_ireq_dependencies(source, dest)
9 changes: 9 additions & 0 deletions piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ def get_dependencies(self, ireq: InstallRequirement) -> Set[InstallRequirement]:

return self._dependencies_cache[ireq]

def copy_ireq_dependencies(
self, source: InstallRequirement, dest: InstallRequirement
) -> None:
try:
self._dependencies_cache[dest] = self._dependencies_cache[source]
except KeyError:
# `source` may not be in cache yet.
pass

def _get_project(self, ireq: InstallRequirement) -> Any:
"""
Return a dict of a project info from PyPI JSON API for a given
Expand Down
5 changes: 5 additions & 0 deletions piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ def combine_install_requirements(

# deepcopy the accumulator so as to not modify the inputs
combined_ireq = copy.deepcopy(source_ireqs[0])
repository.copy_ireq_dependencies(source_ireqs[0], combined_ireq)

for ireq in source_ireqs[1:]:
# NOTE we may be losing some info on dropped reqs here
if combined_ireq.req is not None and ireq.req is not None:
combined_ireq.req.specifier &= ireq.req.specifier
if combined_ireq.constraint:
# We don't find dependencies for constraint ireqs, so copy them
# from non-constraints:
repository.copy_ireq_dependencies(ireq, combined_ireq)
combined_ireq.constraint &= ireq.constraint
combined_ireq.extras = {*combined_ireq.extras, *ireq.extras}
if combined_ireq.req is not None:
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def allow_all_wheels(self):
# No need to do an actual pip.Wheel mock here.
yield

def copy_ireq_dependencies(self, source, dest):
# No state to update.
pass

@property
def options(self) -> optparse.Values:
"""Not used"""
Expand Down
25 changes: 25 additions & 0 deletions tests/test_repository_local.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import copy

import pytest

from piptools.repositories.local import LocalRequirementsRepository
from piptools.utils import key_from_ireq
from tests.conftest import FakeRepository

EXPECTED = {"sha256:5e6071ee6e4c59e0d0408d366fe9b66781d2cf01be9a6e19a2433bb3c5336330"}

Expand Down Expand Up @@ -54,3 +57,25 @@ def test_toggle_reuse_hashes_local_repository(
captured = capsys.readouterr()
assert captured.out == ""
assert captured.err == ""


class FakeRepositoryChecksForCopy(FakeRepository):
def __init__(self):
super().__init__()
self.copied = []

def copy_ireq_dependencies(self, source, dest):
self.copied.append(source)


def test_local_repository_copy_ireq_dependencies(from_line):
# Ensure that local repository forwards any messages to update its state
# of ireq dependencies.
checker = FakeRepositoryChecksForCopy()
local_repository = LocalRequirementsRepository({}, checker)

src = from_line("small-fake-a==0.1")
dest = copy.deepcopy(src)
local_repository.copy_ireq_dependencies(src, dest)

assert src in checker.copied
19 changes: 19 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ def test_iter_dependencies_ignores_constraints(resolver, from_line):
next(res._iter_dependencies(ireq))


def test_iter_dependencies_after_combine_install_requirements(
pypi_repository, base_resolver, make_package, from_line
):
res = base_resolver([], repository=pypi_repository)

sub_deps = ["click"]
package_a = make_package("package-a", install_requires=sub_deps)
package_b = make_package("package-b", install_requires=["package-a"])

local_package_a = from_line(path_to_url(package_a))
assert [dep.name for dep in res._iter_dependencies(local_package_a)] == sub_deps

package_a_from_b = from_line("package-a", comes_from=path_to_url(package_b))
combined = combine_install_requirements(
pypi_repository, [local_package_a, package_a_from_b]
)
assert [dep.name for dep in res._iter_dependencies(combined)] == sub_deps


def test_combine_install_requirements(repository, from_line):
celery30 = from_line("celery>3.0", comes_from="-r requirements.in")
celery31 = from_line("celery==3.1.1", comes_from=from_line("fake-package"))
Expand Down

0 comments on commit 828e2d6

Please sign in to comment.