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.spec
from spack.version import (
EmptyRangeError,
GitVersion,
StandardVersion,
Version,
@ -695,9 +696,9 @@ def test_version_range_nonempty():
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")
with pytest.raises(ValueError):
with pytest.raises(EmptyRangeError, match="2:1.0 is an empty range"):
assert ver("2:1.0")

View file

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

View file

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