From a2678c0bef5f21cee1c922336e0d608db50916e1 Mon Sep 17 00:00:00 2001 From: Arnesh Date: Mon, 31 Aug 2026 18:47:16 +0530 Subject: [PATCH] fix: implement __le__ and __ge__ for GentooVersion GentooVersion only defined __eq__, __lt__ and __gt__, so <= and >= fell back to the attrs generated ones that compare raw version strings. This was wrong whenever string order differs from version order, for example GentooVersion("1.2.0-r0") <= GentooVersion("1.10.0-r0") returned False. AlpineLinuxVersion inherits from GentooVersion and had the same problem. fixes: https://github.com/aboutcode-org/univers/issues/172 Signed-off-by: Arnesh --- src/univers/versions.py | 10 ++++++++++ tests/test_versions.py | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/univers/versions.py b/src/univers/versions.py index 5d6101ac..c761d13d 100644 --- a/src/univers/versions.py +++ b/src/univers/versions.py @@ -429,6 +429,16 @@ def __gt__(self, other): return NotImplemented return gentoo.vercmp(self.value, other.value) == 1 + def __le__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return gentoo.vercmp(self.value, other.value) <= 0 + + def __ge__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return gentoo.vercmp(self.value, other.value) >= 0 + class AlpineLinuxVersion(GentooVersion): @classmethod diff --git a/tests/test_versions.py b/tests/test_versions.py index ab3007d3..2d728131 100644 --- a/tests/test_versions.py +++ b/tests/test_versions.py @@ -170,6 +170,14 @@ def test_rpm_version(): def test_gentoo_version(): assert GentooVersion("1.2.3") == GentooVersion("1.2.3") assert GentooVersion("1.2.3") != GentooVersion("1.2.4") + assert GentooVersion("1.2.0-r0") < GentooVersion("1.10.0-r0") + assert GentooVersion("1.2.0-r0") <= GentooVersion("1.10.0-r0") + assert GentooVersion("1.10.0-r0") > GentooVersion("1.2.0-r0") + assert GentooVersion("1.10.0-r0") >= GentooVersion("1.2.0-r0") + assert not GentooVersion("1.10.0-r0") <= GentooVersion("1.2.0-r0") + assert not GentooVersion("1.2.0-r0") >= GentooVersion("1.10.0-r0") + assert GentooVersion("1.2.0-r0") <= GentooVersion("1.2.0-r0") + assert GentooVersion("1.2.0-r0") >= GentooVersion("1.2.0-r0") assert GentooVersion.is_valid("1.2.3") assert not GentooVersion.is_valid("1.2.3a-1-a") @@ -181,6 +189,10 @@ def test_alpine_linux_version(): assert AlpineLinuxVersion("1.2.3-r1") < AlpineLinuxVersion("1.2.3-r2") assert AlpineLinuxVersion("1.2.3-r1") >= AlpineLinuxVersion("1.2.3-r1") assert AlpineLinuxVersion("1.2.3-r1") <= AlpineLinuxVersion("1.2.3-r1") + assert AlpineLinuxVersion("1.2.0-r0") <= AlpineLinuxVersion("1.10.0-r0") + assert AlpineLinuxVersion("1.10.0-r0") >= AlpineLinuxVersion("1.2.0-r0") + assert not AlpineLinuxVersion("1.10.0-r0") <= AlpineLinuxVersion("1.2.0-r0") + assert not AlpineLinuxVersion("1.2.0-r0") >= AlpineLinuxVersion("1.10.0-r0") assert AlpineLinuxVersion.is_valid("1.2.3-r1") assert not AlpineLinuxVersion.is_valid("007")