Skip to content

Commit

Permalink
pep440: support post/local for semver allows
Browse files Browse the repository at this point in the history
This change ensures that post and local releases are taken into
consideration when checking if semver version instance allows
post and local build releases.

The following conditions now hold `poetry.core.semver.Version.allows`.

- `3.0.0` allows `3.0.0+local.1`, `3.0.0-1`
- `3.0.0+local.1` disallows `3.0.0+local.2`, allows `3.0.0-1`
- `3.0.0-1` disallows ``3.0.0`, `3.0.0+local.1`, allows `3.0.0-1+local.1`
  • Loading branch information
abn committed Apr 3, 2021
1 parent f5c6b64 commit 0ccfec4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
22 changes: 18 additions & 4 deletions poetry/core/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,24 @@ def is_empty(self) -> bool:
return False

def allows(self, version: "Version") -> bool:
return self == version
_this, _other = self, version

# 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():
_other = _other.without_postrelease()
elif _this.without_postrelease() and not _other.without_postrelease():
_this = _this.without_postrelease()

return _this == _other

def allows_all(self, other: "VersionTypes") -> bool:
return other.is_empty() or other == self
return other.is_empty() or other.allows(self)

def allows_any(self, other: "VersionTypes") -> bool:
return other.allows(self)
Expand All @@ -101,15 +115,15 @@ def union(self, other: "VersionTypes") -> "VersionTypes":
return other

if isinstance(other, VersionRangeConstraint):
if other.min == self:
if self.allows(other.min):
return VersionRange(
other.min,
other.max,
include_min=True,
include_max=other.include_max,
)

if other.max == self:
if self.allows(other.max):
return VersionRange(
other.min,
other.max,
Expand Down
30 changes: 25 additions & 5 deletions tests/semver/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,38 @@ def test_allows():
assert not v.allows(Version.parse("1.3.3"))
assert not v.allows(Version.parse("1.2.4"))
assert not v.allows(Version.parse("1.2.3-dev"))
assert not v.allows(Version.parse("1.2.3+build"))
assert v.allows(Version.parse("1.2.3+build"))
assert v.allows(Version.parse("1.2.3-1"))
assert v.allows(Version.parse("1.2.3-1+build"))


def test_allows_with_local():
v = Version.parse("1.2.3+build.1")
assert v.allows(v)
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 v.allows(Version.parse("1.2.3-1+build.1"))


def test_allows_with_post():
v = Version.parse("1.2.3-1")
assert v.allows(v)
assert not v.allows(Version.parse("1.2.3"))
assert not v.allows(Version.parse("2.2.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+build.1"))


def test_allows_all():
v = Version.parse("1.2.3")

assert v.allows_all(v)
assert not v.allows_all(Version.parse("0.0.3"))
assert not v.allows_all(
VersionRange(Version.parse("1.1.4"), Version.parse("1.2.4"))
)
assert not v.allows_all(VersionRange())
assert v.allows_all(VersionRange(Version.parse("1.1.4"), Version.parse("1.2.4")))
assert v.allows_all(VersionRange())
assert v.allows_all(EmptyConstraint())


Expand Down

0 comments on commit 0ccfec4

Please sign in to comment.