]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Allow comparing GenericVersion() against int and str values
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 10 Oct 2023 12:37:52 +0000 (14:37 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 11 Oct 2023 09:11:56 +0000 (11:11 +0200)
mkosi/versioncomp.py
tests/test_versioncomp.py

index da87bd30a3615e829677765d0368fcd5d1eb2eb8..9025a5a7adcb056f06c14005a0f8a17e7df0e1e0 100644 (file)
@@ -139,32 +139,44 @@ class GenericVersion:
             v2 = v2.removeprefix(v2_letter_prefix)
 
     def __eq__(self, other: object) -> bool:
-        if not isinstance(other, GenericVersion):
+        if isinstance(other, (str, int)):
+            other = GenericVersion(str(other))
+        elif not isinstance(other, GenericVersion):
             return False
         return self.compare_versions(self._version, other._version) == self._EQUAL
 
     def __ne__(self, other: object) -> bool:
-        if not isinstance(other, GenericVersion):
+        if isinstance(other, (str, int)):
+            other = GenericVersion(str(other))
+        elif not isinstance(other, GenericVersion):
             return False
         return self.compare_versions(self._version, other._version) != self._EQUAL
 
     def __lt__(self, other: object) -> bool:
-        if not isinstance(other, GenericVersion):
+        if isinstance(other, (str, int)):
+            other = GenericVersion(str(other))
+        elif not isinstance(other, GenericVersion):
             return False
         return self.compare_versions(self._version, other._version) == self._LEFT_SMALLER
 
     def __le__(self, other: object) -> bool:
-        if not isinstance(other, GenericVersion):
+        if isinstance(other, (str, int)):
+            other = GenericVersion(str(other))
+        elif not isinstance(other, GenericVersion):
             return False
         return self.compare_versions(self._version, other._version) in (self._EQUAL, self._LEFT_SMALLER)
 
     def __gt__(self, other: object) -> bool:
-        if not isinstance(other, GenericVersion):
+        if isinstance(other, (str, int)):
+            other = GenericVersion(str(other))
+        elif not isinstance(other, GenericVersion):
             return False
         return self.compare_versions(self._version, other._version) == self._RIGHT_SMALLER
 
     def __ge__(self, other: object) -> bool:
-        if not isinstance(other, GenericVersion):
+        if isinstance(other, (str, int)):
+            other = GenericVersion(str(other))
+        elif not isinstance(other, GenericVersion):
             return False
         return self.compare_versions(self._version, other._version) in (self._EQUAL, self._RIGHT_SMALLER)
 
index 98ec9d2849c64cf2e539305e006e56bda6ec5708..265e60ce9a4ecfd6417d80abaf25f7a5f87e667e 100644 (file)
@@ -6,6 +6,14 @@ import pytest
 from mkosi.versioncomp import GenericVersion
 
 
+def test_conversion() -> None:
+    assert GenericVersion("1") < 2
+    assert GenericVersion("1") < "2"
+    assert GenericVersion("2") > 1
+    assert GenericVersion("2") > "1"
+    assert GenericVersion("1") == "1"
+
+
 def test_generic_version_systemd() -> None:
     """Same as the first block of systemd/test/test-compare-versions.sh"""
     assert GenericVersion("1") < GenericVersion("2")