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

make intersection of two Versions symmetrical #395

Merged
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
6 changes: 6 additions & 0 deletions src/poetry/core/semver/empty_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ def flatten(self) -> list[VersionRangeConstraint]:

def __str__(self) -> str:
return "<empty>"

def __eq__(self, other: object) -> bool:
if not isinstance(other, VersionConstraint):
return False

return other.is_empty()
3 changes: 3 additions & 0 deletions src/poetry/core/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def intersect(self, other: VersionConstraint) -> Version | EmptyConstraint:
if other.allows(self):
return self

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

return EmptyConstraint()

def union(self, other: VersionConstraint) -> VersionConstraint:
Expand Down
51 changes: 38 additions & 13 deletions tests/semver/test_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from poetry.core.semver.empty_constraint import EmptyConstraint
Expand All @@ -9,6 +11,10 @@
from poetry.core.version.pep440 import ReleaseTag


if TYPE_CHECKING:
from poetry.core.semver.version_constraint import VersionConstraint


@pytest.mark.parametrize(
"text,version",
[
Expand Down Expand Up @@ -163,19 +169,38 @@ def test_allows_any() -> None:
assert not v.allows_any(EmptyConstraint())


def test_intersect() -> None:
v = Version.parse("1.2.3")

assert v.intersect(v) == v
assert v.intersect(Version.parse("1.1.4")).is_empty()
assert (
v.intersect(VersionRange(Version.parse("1.1.4"), Version.parse("1.2.4"))) == v
)
assert (
Version.parse("1.1.4")
.intersect(VersionRange(v, Version.parse("1.2.4")))
.is_empty()
)
@pytest.mark.parametrize(
("version1", "version2", "expected"),
[
(
Version.parse("1.2.3"),
Version.parse("1.1.4"),
EmptyConstraint(),
),
(
Version.parse("1.2.3"),
VersionRange(Version.parse("1.1.4"), Version.parse("1.2.4")),
Version.parse("1.2.3"),
),
(
Version.parse("1.1.4"),
VersionRange(Version.parse("1.2.3"), Version.parse("1.2.4")),
EmptyConstraint(),
),
(
Version.parse("1.2.3"),
Version.parse("1.2.3.post0"),
Version.parse("1.2.3.post0"),
),
],
)
def test_intersect(
version1: VersionConstraint,
version2: VersionConstraint,
expected: VersionConstraint,
) -> None:
assert version1.intersect(version2) == expected
assert version2.intersect(version1) == expected


def test_union() -> None:
Expand Down