Skip to content

Commit

Permalink
version: fix local label comparison
Browse files Browse the repository at this point in the history
Prior to this change, local label comparison was inconsistent with
PEP 440. This change ensures that the local version label is checked
for equivalence using a strict string equality comparison.

Resolves: python-poetry/poetry#4729
  • Loading branch information
abn committed May 26, 2022
1 parent 6b45d6c commit cfdccfc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/poetry/core/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ def allows(self, version: Version | None) -> bool:
# allow weak equality to allow `3.0.0+local.1` for `3.0.0`
if not _this.is_local() and _other.is_local():
_other = _other.without_local()
elif _this.is_local() and not _other.is_local():
_this = _this.without_local()

# allow weak equality to allow `3.0.0-1` for `3.0.0`
if not _this.is_postrelease() and _other.is_postrelease():
Expand Down
4 changes: 4 additions & 0 deletions src/poetry/core/semver/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def allows(self, other: Version) -> bool:
if not _this.is_local() and _other.is_local():
# allow weak equality to allow `3.0.0+local.1` for `<=3.0.0`
_other = _other.without_local()
elif _this.is_local() and (
not _other.is_local() or _this.local != _other.local
):
return False

if not _this.is_postrelease() and _other.is_postrelease():
# allow weak equality to allow `3.0.0-1` for `<=3.0.0`
Expand Down
3 changes: 2 additions & 1 deletion tests/semver/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def test_allows_with_local() -> None:
assert not v.allows(Version.parse("1.3.3"))
assert not v.allows(Version.parse("1.2.3-dev"))
assert not v.allows(Version.parse("1.2.3+build.2"))
assert v.allows(Version.parse("1.2.3-1"))
assert not v.allows(Version.parse("1.2.3-1"))
assert v.allows(Version.parse("1.2.3-1+build.1"))
assert v.allows(Version.parse("1.2.3-1+build.1"))


Expand Down
22 changes: 14 additions & 8 deletions tests/semver/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def test_allows_post_releases_with_post_and_local_max() -> None:
two = Version.parse("3.0.0-1")
three = Version.parse("3.0.0-1+local.1")
four = Version.parse("3.0.0+local.2")
five = Version.parse("4.0.0+local.2")

assert VersionRange(max=one, include_max=True).allows(two)
assert not VersionRange(max=one, include_max=True).allows(two)
assert VersionRange(max=one, include_max=True).allows(three)
assert not VersionRange(max=one, include_max=True).allows(four)

Expand All @@ -143,12 +144,17 @@ def test_allows_post_releases_with_post_and_local_max() -> None:
assert VersionRange(max=two, include_max=True).allows(four)

assert VersionRange(max=three, include_max=True).allows(one)
assert VersionRange(max=three, include_max=True).allows(two)
assert VersionRange(max=three, include_max=True).allows(four)
assert not VersionRange(max=three, include_max=True).allows(two)
assert not VersionRange(max=three, include_max=True).allows(four)

assert VersionRange(max=four, include_max=True).allows(one)
assert VersionRange(max=four, include_max=True).allows(two)
assert VersionRange(max=four, include_max=True).allows(three)
assert not VersionRange(max=four, include_max=True).allows(one)
assert not VersionRange(max=four, include_max=True).allows(two)
assert not VersionRange(max=four, include_max=True).allows(three)

assert not VersionRange(max=five, include_max=True).allows(one)
assert not VersionRange(max=five, include_max=True).allows(two)
assert not VersionRange(max=five, include_max=True).allows(three)
assert VersionRange(max=five, include_max=True).allows(four)


@pytest.mark.parametrize(
Expand All @@ -161,9 +167,9 @@ def test_allows_post_releases_with_post_and_local_max() -> None:
id="post",
),
pytest.param(
Version.parse("3.0.0"),
Version.parse("3.0.0+local.1"),
Version.parse("3.0.0+local.2"),
Version.parse("3.0.0-1+local.1"),
Version.parse("3.0.0-2+local.1"),
id="local",
),
],
Expand Down

0 comments on commit cfdccfc

Please sign in to comment.