remove_previous_versions() compares strings instead of Version objects
The remove_previous_versions() function compares version strings, instead of Version objects, as can be seen in the source.
Strings are compared by lexicographical order, as explained in the python docs.
Although this may work in most cases (assuming only versions from the same channel are compared), it can lead to unexpected results.
For example:
"1" < "1.0" # returns True due to length difference
"1A" < "1a" # returns True due to case difference (ord("A") < ord("a"))
I suppose it would be better to compare Version objects:
The Version class from dsdev_utils.helpers, and the one from packaging.version, use version tuples. These tuples are also compared lexicographically, but they always have the same length, and characters are lower case, see e.g. packaging.version source.
from packaging.version import Version
Version("1") < Version("1.0") # returns False as expected
Version("1A") < Version("1a") # returns False as expected
The same holds for PackageHandler._add_package_to_config().
@dennisvang your first comments example, should return false, since 1< is not less than 1a and 1 is not less than 1.0. Am I missing something here, of can you clarify?
@NicklasTegner Both examples should return False.
In the current implementation, which uses string comparison, this is not the case.
The packaging.version.Version comparison, on the other hand, does what it is supposed to do.
aha now I understand... Yeah.. That's definitely a bug.