From: Daan De Meyer Date: Tue, 10 Oct 2023 12:37:52 +0000 (+0200) Subject: Allow comparing GenericVersion() against int and str values X-Git-Tag: v19~87^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=01425677fd0c2faa54be46b752264d93d269b77b;p=thirdparty%2Fmkosi.git Allow comparing GenericVersion() against int and str values --- diff --git a/mkosi/versioncomp.py b/mkosi/versioncomp.py index da87bd30a..9025a5a7a 100644 --- a/mkosi/versioncomp.py +++ b/mkosi/versioncomp.py @@ -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) diff --git a/tests/test_versioncomp.py b/tests/test_versioncomp.py index 98ec9d284..265e60ce9 100644 --- a/tests/test_versioncomp.py +++ b/tests/test_versioncomp.py @@ -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")