Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mixology: choose direct ref deps when merging #5654

Merged
merged 2 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/poetry/mixology/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ def intersect(self, other: Term) -> Term | None:
negative = other if self.is_positive() else self

return self._non_empty_term(
positive.constraint.difference(negative.constraint), True
positive.constraint.difference(negative.constraint), True, other
)
elif self.is_positive():
# foo ^1.0.0 ∩ foo >=1.5.0 <3.0.0 → foo ^1.5.0
return self._non_empty_term(
self.constraint.intersect(other.constraint), True
self.constraint.intersect(other.constraint), True, other
)
else:
# not foo ^1.0.0 ∩ not foo >=1.5.0 <3.0.0 → not foo >=1.0.0 <3.0.0
return self._non_empty_term(
self.constraint.union(other.constraint), False
self.constraint.union(other.constraint), False, other
)
elif self.is_positive() != other.is_positive():
return self if self.is_positive() else other
Expand All @@ -151,21 +151,38 @@ def difference(self, other: Term) -> Term | None:
"""
return self.intersect(other.inverse)

@staticmethod
def _is_direct_origin(dependency: Dependency) -> bool:
return dependency.source_type in ["directory", "file", "url", "git"]

def _compatible_dependency(self, other: Dependency) -> bool:
compatible: bool = (
return (
self.dependency.is_root
or other.is_root
or other.is_same_package_as(self.dependency)
or (
# we do this here to indicate direct origin dependencies are
# compatible with NVR dependencies
self.dependency.complete_name == other.complete_name
and self._is_direct_origin(self.dependency)
!= self._is_direct_origin(other)
)
)
return compatible

def _non_empty_term(
self, constraint: VersionConstraint, is_positive: bool
self, constraint: VersionConstraint, is_positive: bool, other: Term
) -> Term | None:
if constraint.is_empty():
return None

return Term(self.dependency.with_constraint(constraint), is_positive)
# when creating a new term prefer direct-reference dependencies
dependency = (
other.dependency
if not self._is_direct_origin(self.dependency)
and self._is_direct_origin(other.dependency)
else self.dependency
)
return Term(dependency.with_constraint(constraint), is_positive)

def __str__(self) -> str:
prefix = "not " if not self.is_positive() else ""
Expand Down
6 changes: 5 additions & 1 deletion tests/puzzle/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from poetry.repositories.pool import Pool
from poetry.repositories.repository import Repository
from poetry.utils.env import MockEnv
from tests.compat import is_poetry_core_1_1_0a7_compat
from tests.helpers import get_dependency
from tests.helpers import get_package
from tests.repositories.test_legacy_repository import (
Expand Down Expand Up @@ -1383,7 +1384,10 @@ def test_solver_duplicate_dependencies_different_sources_types_are_preserved(

assert len(complete_package.all_requires) == 2

pypi, git = complete_package.all_requires
if is_poetry_core_1_1_0a7_compat:
pypi, git = complete_package.all_requires
else:
git, pypi = complete_package.all_requires

assert isinstance(pypi, Dependency)
assert pypi == dependency_pypi
Expand Down