PyUpdater icon indicating copy to clipboard operation
PyUpdater copied to clipboard

remove_previous_versions() compares strings instead of Version objects

Open dennisvang opened this issue 3 years ago • 4 comments

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

dennisvang avatar Dec 23 '21 15:12 dennisvang

The same holds for PackageHandler._add_package_to_config().

dennisvang avatar Dec 23 '21 16:12 dennisvang

@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?

JessicaTegner avatar Dec 29 '21 14:12 JessicaTegner

@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.

dennisvang avatar Dec 29 '21 19:12 dennisvang

aha now I understand... Yeah.. That's definitely a bug.

JessicaTegner avatar Dec 29 '21 20:12 JessicaTegner