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

fix allows_any() with local versions #579

Merged
merged 1 commit into from
May 9, 2023
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
19 changes: 10 additions & 9 deletions src/poetry/core/constraints/version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,20 @@ def allows_all(self, other: VersionConstraint) -> bool:
)

def allows_any(self, other: VersionConstraint) -> bool:
if isinstance(other, Version):
return self.allows(other)
intersection = self.intersect(other)
return not intersection.is_empty()

return other.allows(self)
def intersect(self, other: VersionConstraint) -> VersionConstraint:
if isinstance(other, Version):
if self.allows(other):
return other

def intersect(self, other: VersionConstraint) -> Version | EmptyConstraint:
if other.allows(self):
return self
if other.allows(self):
return self

if isinstance(other, Version) and self.allows(other):
return other
return EmptyConstraint()

return EmptyConstraint()
return other.intersect(self)

def union(self, other: VersionConstraint) -> VersionConstraint:
from poetry.core.constraints.version.version_range import VersionRange
Expand Down
21 changes: 19 additions & 2 deletions src/poetry/core/constraints/version/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,14 @@ def allows_any(self, other: VersionConstraint) -> bool:
return False

if isinstance(other, Version):
return self.allows(other)
if self.allows(other):
return True

# Although `>=1.2.3+local` does not allow the exact version `1.2.3`, both of
# those versions do allow `1.2.3+local`.
return (
self.min is not None and self.min.is_local() and other.allows(self.min)
)

if isinstance(other, VersionUnion):
return any(self.allows_any(constraint) for constraint in other.ranges)
Expand All @@ -143,11 +150,21 @@ def intersect(self, other: VersionConstraint) -> VersionConstraint:
if isinstance(other, VersionUnion):
return other.intersect(self)

# A range and a Version just yields the version if it's in the range.
if isinstance(other, Version):
# A range and a Version just yields the version if it's in the range.
if self.allows(other):
return other

# `>=1.2.3+local` intersects `1.2.3` to return `>=1.2.3+local,<1.2.4`.
if self.min is not None and self.min.is_local() and other.allows(self.min):
upper = other.stable.next_patch()
radoering marked this conversation as resolved.
Show resolved Hide resolved
return VersionRange(
min=self.min,
max=upper,
include_min=self.include_min,
include_max=False,
)

return EmptyConstraint()

if not isinstance(other, VersionRangeConstraint):
Expand Down
17 changes: 16 additions & 1 deletion tests/constraints/version/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,15 @@ def test_allows_all() -> None:
Version.parse("1.2.3+cpu"),
True,
),
(
Version.parse("1.2.3"),
VersionRange(Version.parse("1.2.3+local"), include_min=True),
True,
),
(
Version.parse("1.2.3+cpu"),
Version.parse("1.2.3"),
False,
True,
),
(
Version.parse("1.2.3"),
Expand Down Expand Up @@ -438,6 +443,16 @@ def test_allows_any(
Version.parse("1.2.3+local"),
Version.parse("1.2.3+local"),
),
(
Version.parse("1.2.3"),
VersionRange(Version.parse("1.2.3+local"), include_min=True),
VersionRange(
Version.parse("1.2.3+local"),
Version.parse("1.2.4"),
include_min=True,
include_max=False,
),
),
],
)
def test_intersect(
Expand Down