VersionRange: improve error message for empty range (#40345)

This commit is contained in:
Harmen Stoppels 2023-10-06 23:19:49 +02:00 committed by GitHub
parent b027d7d0de
commit d341be83e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 4 deletions

View File

@ -17,6 +17,7 @@
import spack.package_base import spack.package_base
import spack.spec import spack.spec
from spack.version import ( from spack.version import (
EmptyRangeError,
GitVersion, GitVersion,
StandardVersion, StandardVersion,
Version, Version,
@ -695,9 +696,9 @@ def test_version_range_nonempty():
def test_empty_version_range_raises(): def test_empty_version_range_raises():
with pytest.raises(ValueError): with pytest.raises(EmptyRangeError, match="2:1.0 is an empty range"):
assert VersionRange("2", "1.0") assert VersionRange("2", "1.0")
with pytest.raises(ValueError): with pytest.raises(EmptyRangeError, match="2:1.0 is an empty range"):
assert ver("2:1.0") assert ver("2:1.0")

View File

@ -16,6 +16,7 @@
""" """
from .common import ( from .common import (
EmptyRangeError,
VersionChecksumError, VersionChecksumError,
VersionError, VersionError,
VersionLookupError, VersionLookupError,
@ -54,5 +55,6 @@
"VersionError", "VersionError",
"VersionChecksumError", "VersionChecksumError",
"VersionLookupError", "VersionLookupError",
"EmptyRangeError",
"any_version", "any_version",
] ]

View File

@ -35,3 +35,7 @@ class VersionChecksumError(VersionError):
class VersionLookupError(VersionError): class VersionLookupError(VersionError):
"""Raised for errors looking up git commits as versions.""" """Raised for errors looking up git commits as versions."""
class EmptyRangeError(VersionError):
"""Raised when constructing an empty version range."""

View File

@ -12,6 +12,7 @@
from .common import ( from .common import (
COMMIT_VERSION, COMMIT_VERSION,
EmptyRangeError,
VersionLookupError, VersionLookupError,
infinity_versions, infinity_versions,
is_git_version, is_git_version,
@ -595,14 +596,17 @@ def up_to(self, index) -> StandardVersion:
class ClosedOpenRange: class ClosedOpenRange:
def __init__(self, lo: StandardVersion, hi: StandardVersion): def __init__(self, lo: StandardVersion, hi: StandardVersion):
if hi < lo: if hi < lo:
raise ValueError(f"{lo}:{hi} is an empty range") raise EmptyRangeError(f"{lo}..{hi} is an empty range")
self.lo: StandardVersion = lo self.lo: StandardVersion = lo
self.hi: StandardVersion = hi self.hi: StandardVersion = hi
@classmethod @classmethod
def from_version_range(cls, lo: StandardVersion, hi: StandardVersion): def from_version_range(cls, lo: StandardVersion, hi: StandardVersion):
"""Construct ClosedOpenRange from lo:hi range.""" """Construct ClosedOpenRange from lo:hi range."""
try:
return ClosedOpenRange(lo, next_version(hi)) return ClosedOpenRange(lo, next_version(hi))
except EmptyRangeError as e:
raise EmptyRangeError(f"{lo}:{hi} is an empty range") from e
def __str__(self): def __str__(self):
# This simplifies 3.1:<3.2 to 3.1:3.1 to 3.1 # This simplifies 3.1:<3.2 to 3.1:3.1 to 3.1