Skip to content

Commit

Permalink
make intersection of two Versions symmetrical
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Jun 7, 2022
1 parent 3bf7ad0 commit aec98be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
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
49 changes: 36 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,36 @@ 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: Version, version2: Version, expected: VersionConstraint
) -> None:
assert version1.intersect(version2) == expected
assert version2.intersect(version1) == expected


def test_union() -> None:
Expand Down

0 comments on commit aec98be

Please sign in to comment.