Skip to content

Commit 364f025

Browse files
committed
pep440: support post/local for semver allows
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`
1 parent f5c6b64 commit 364f025

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

poetry/core/semver/version.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,29 @@ def is_empty(self) -> bool:
8080
return False
8181

8282
def allows(self, version: "Version") -> bool:
83-
return self == version
83+
if version is None:
84+
return False
85+
86+
_this, _other = self, version
87+
88+
# allow weak equality to allow `3.0.0+local.1` for `3.0.0`
89+
if not _this.is_local() and _other.is_local():
90+
_other = _other.without_local()
91+
elif _this.is_local() and not _other.is_local():
92+
_this = _this.without_local()
93+
94+
# allow weak equality to allow `3.0.0-1` for `3.0.0`
95+
if not _this.is_postrelease() and _other.is_postrelease():
96+
_other = _other.without_postrelease()
97+
elif _this.without_postrelease() and not _other.without_postrelease():
98+
_this = _this.without_postrelease()
99+
100+
return _this == _other
84101

85102
def allows_all(self, other: "VersionTypes") -> bool:
86-
return other.is_empty() or other == self
103+
return other.is_empty() or (
104+
self.allows(other) if isinstance(other, self.__class__) else other == self
105+
)
87106

88107
def allows_any(self, other: "VersionTypes") -> bool:
89108
return other.allows(self)
@@ -101,15 +120,15 @@ def union(self, other: "VersionTypes") -> "VersionTypes":
101120
return other
102121

103122
if isinstance(other, VersionRangeConstraint):
104-
if other.min == self:
123+
if self.allows(other.min):
105124
return VersionRange(
106125
other.min,
107126
other.max,
108127
include_min=True,
109128
include_max=other.include_max,
110129
)
111130

112-
if other.max == self:
131+
if self.allows(other.max):
113132
return VersionRange(
114133
other.min,
115134
other.max,

tests/semver/test_version.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,29 @@ def test_allows():
113113
assert not v.allows(Version.parse("1.3.3"))
114114
assert not v.allows(Version.parse("1.2.4"))
115115
assert not v.allows(Version.parse("1.2.3-dev"))
116-
assert not v.allows(Version.parse("1.2.3+build"))
116+
assert v.allows(Version.parse("1.2.3+build"))
117+
assert v.allows(Version.parse("1.2.3-1"))
118+
assert v.allows(Version.parse("1.2.3-1+build"))
119+
120+
121+
def test_allows_with_local():
122+
v = Version.parse("1.2.3+build.1")
123+
assert v.allows(v)
124+
assert not v.allows(Version.parse("1.3.3"))
125+
assert not v.allows(Version.parse("1.2.3-dev"))
126+
assert not v.allows(Version.parse("1.2.3+build.2"))
127+
assert v.allows(Version.parse("1.2.3-1"))
128+
assert v.allows(Version.parse("1.2.3-1+build.1"))
129+
130+
131+
def test_allows_with_post():
132+
v = Version.parse("1.2.3-1")
133+
assert v.allows(v)
134+
assert not v.allows(Version.parse("1.2.3"))
135+
assert not v.allows(Version.parse("2.2.3"))
136+
assert not v.allows(Version.parse("1.2.3-dev"))
137+
assert not v.allows(Version.parse("1.2.3+build.2"))
138+
assert v.allows(Version.parse("1.2.3-1+build.1"))
117139

118140

119141
def test_allows_all():

0 commit comments

Comments
 (0)