Skip to content

Commit 0d3cf69

Browse files
authored
Merge branch 'main' into intdot-scheme
2 parents 168e7a6 + f365e4e commit 0d3cf69

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
Version v31.0.0
5+
----------------
6+
7+
- Raise instead of returning ``NotImplementedError`` in ``version_range.VersionRange`` methods. https://github.com/aboutcode-org/univers/pull/158
8+
49
Version v30.12.1
510
----------------
611

src/univers/py.typed

Whitespace-only changes.

src/univers/version_range.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from univers.utils import remove_spaces
2222
from univers.version_constraint import VersionConstraint
2323
from univers.version_constraint import contains_version
24+
from univers.versions import AllVersion
25+
from univers.versions import NoneVersion
2426

2527

2628
class InvalidVersionRange(Exception):
@@ -76,15 +78,15 @@ def from_native(cls, string):
7678
Return a VersionRange built from a scheme-specific, native version range
7779
``string``. Subclasses can implement.
7880
"""
79-
return NotImplementedError
81+
raise NotImplementedError
8082

8183
@classmethod
8284
def from_natives(cls, strings):
8385
"""
8486
Return a VersionRange built from a ``strings`` list of scheme-
8587
specific native version range strings. Subclasses can implement.
8688
"""
87-
return NotImplementedError
89+
raise NotImplementedError
8890

8991
def to_native(self, *args, **kwargs):
9092
"""
@@ -93,7 +95,7 @@ def to_native(self, *args, **kwargs):
9395
extra arguments (such as a package name that some scheme may require
9496
like for deb and rpm.)
9597
"""
96-
return NotImplementedError
98+
raise NotImplementedError
9799

98100
@classmethod
99101
def from_string(cls, vers, simplify=False, validate=False):
@@ -174,7 +176,7 @@ def from_versions(cls, sequence):
174176
such as ["3.0.0", "1.0.1b", "3.0.2", "0.9.7a", "1.1.1ka"]
175177
"""
176178
if not cls.scheme or not cls.version_class:
177-
return NotImplementedError
179+
raise NotImplementedError
178180

179181
constraints = []
180182
for version in sequence:
@@ -220,6 +222,13 @@ def __contains__(self, version):
220222
object. A version is contained in a VersionRange if it satisfies its
221223
constraints according to ``vers`` rules.
222224
"""
225+
226+
if self.version_class is AllVersion:
227+
return True
228+
229+
if self.version_class is NoneVersion:
230+
return False
231+
223232
if not isinstance(version, self.version_class):
224233
raise TypeError(
225234
f"{version!r} is not of expected type: {self.version_class!r}",
@@ -712,9 +721,9 @@ class PypiVersionRange(VersionRange):
712721
def from_native(cls, string):
713722
"""
714723
Return a VersionRange built from a PyPI PEP440 version specifiers ``string``.
715-
Raise an a univers.versions.InvalidVersion
724+
Raise a univers.versions.InvalidVersion
716725
"""
717-
# TODO: environment markers are yet supported
726+
# TODO: environment markers are not yet supported
718727
# TODO: handle .* version, ~= and === operators
719728

720729
if ";" in string:
@@ -1182,6 +1191,16 @@ class MattermostVersionRange(VersionRange):
11821191
version_class = versions.SemverVersion
11831192

11841193

1194+
class AllVersionRange(VersionRange):
1195+
scheme = "all"
1196+
version_class = versions.AllVersion
1197+
1198+
1199+
class NoneVersionRange(VersionRange):
1200+
scheme = "none"
1201+
version_class = versions.NoneVersion
1202+
1203+
11851204
def from_gitlab_native(gitlab_scheme, string):
11861205
purl_scheme = gitlab_scheme
11871206
if gitlab_scheme not in PURL_TYPE_BY_GITLAB_SCHEME.values():
@@ -1425,6 +1444,8 @@ def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List])
14251444
"mattermost": MattermostVersionRange,
14261445
"conan": ConanVersionRange,
14271446
"intdot": IntdotVersionRange,
1447+
"all": AllVersionRange,
1448+
"none": NoneVersionRange,
14281449
}
14291450

14301451
PURL_TYPE_BY_GITLAB_SCHEME = {

src/univers/versions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def build_value(self, string):
124124

125125
def satisfies(self, constraint):
126126
"""
127-
Return True is this Version satisfies the ``constraint``
127+
Return True if this Version satisfies the ``constraint``
128128
VersionConstraint. Satisfying means that this version is "within" the
129129
``constraint``.
130130
"""
@@ -144,6 +144,18 @@ def is_valid(cls, string):
144144
return intdot.IntdotVersion.is_valid(string)
145145

146146

147+
class AllVersion(Version):
148+
@classmethod
149+
def is_valid(cls, string):
150+
return string == "vers:all/*"
151+
152+
153+
class NoneVersion(Version):
154+
@classmethod
155+
def is_valid(cls, string):
156+
return string == "vers:none/*"
157+
158+
147159
class GenericVersion(Version):
148160
@classmethod
149161
def is_valid(cls, string):

tests/test_version_range.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from univers.versions import PypiVersion
3131
from univers.versions import RubygemsVersion
3232
from univers.versions import SemverVersion
33+
from univers.versions import Version
3334

3435

3536
class TestVersionRange(TestCase):
@@ -557,3 +558,25 @@ def test_version_range_intdot():
557558
assert IntdotVersion("1.3.3alpha") in intdot_range
558559
assert IntdotVersion("1.2.2.pre") not in intdot_range
559560
assert IntdotVersion("1010.23.234203.0") in IntdotVersionRange.from_string("vers:intdot/*")
561+
562+
563+
def test_version_range_all():
564+
all_vers = VersionRange.from_string("vers:all/*")
565+
assert all_vers.contains(Version("1.2.3"))
566+
assert PypiVersion("2.0.3") in all_vers
567+
# test for invalid all range specification
568+
with pytest.raises(Exception):
569+
VersionRange.from_string("vers:all/>1.2.3")
570+
with pytest.raises(Exception):
571+
VersionRange.from_string("vers:all/*|>1.2.3")
572+
573+
574+
def test_version_range_none():
575+
none_vers = VersionRange.from_string("vers:none/*")
576+
assert not none_vers.contains(Version("1.2.3"))
577+
assert PypiVersion("2.0.3") not in none_vers
578+
# test for invalid all range specification
579+
with pytest.raises(Exception):
580+
VersionRange.from_string("vers:none/!1.2.3")
581+
with pytest.raises(Exception):
582+
VersionRange.from_string("vers:none/*|>1.2.3")

0 commit comments

Comments
 (0)