Skip to content

Commit

Permalink
Temporary fix for semver-unstable sort
Browse files Browse the repository at this point in the history
    - Revert this once this is fixed in upstream
    - rbarrois/python-semanticversion#132

Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed May 25, 2022
1 parent df34260 commit 0fc20b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/univers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download.

import semantic_version


def remove_spaces(string):
return "".join(string.split())
Expand All @@ -27,3 +29,15 @@ def cmp(x, y):
else:
# note that this is the minimal replacement function
return (x > y) - (x < y)


class SortableSemverVersion(semantic_version.Version):
"""
TODO: This is temporary workaround for unstable sort
Revert this and associated changes once the upstream is fixed.
https://github.com/rbarrois/python-semanticversion/issues/132
"""

@property
def precedence_key(self):
return super().precedence_key + (self.build,)
10 changes: 5 additions & 5 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import functools

import attr
import semantic_version
from univers.utils import SortableSemverVersion
from packaging import version as packaging_version

from univers import arch
Expand Down Expand Up @@ -209,7 +209,7 @@ class SemverVersion(Version):

@classmethod
def build_value(cls, string):
return semantic_version.Version.coerce(string)
return SortableSemverVersion.coerce(string)

@classmethod
def is_valid(cls, string):
Expand Down Expand Up @@ -431,14 +431,14 @@ def __gt__(self, other):
class ComposerVersion(SemverVersion):
@classmethod
def build_value(cls, string):
return semantic_version.Version.coerce(string.lstrip("vV"))
return SortableSemverVersion.coerce(string.lstrip("vV"))


@attr.s(frozen=True, order=False, eq=False, hash=True)
class GolangVersion(SemverVersion):
@classmethod
def build_value(cls, string):
return semantic_version.Version.coerce(string.lstrip("vV"))
return SortableSemverVersion.coerce(string.lstrip("vV"))


@attr.s(frozen=True, order=False, eq=False, hash=True)
Expand Down Expand Up @@ -605,7 +605,7 @@ def is_valid_new(cls, string):
True
"""
if SemverVersion.is_valid(string):
sem = semantic_version.Version.coerce(string)
sem = SortableSemverVersion.coerce(string)
return sem.major >= 3

@classmethod
Expand Down

0 comments on commit 0fc20b6

Please sign in to comment.