Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect comparison of two versions #341

Closed
goravsingal opened this issue Oct 16, 2020 · 3 comments
Closed

Incorrect comparison of two versions #341

goravsingal opened this issue Oct 16, 2020 · 3 comments

Comments

@goravsingal
Copy link

goravsingal commented Oct 16, 2020

I found an issue while comparing two versions. Below are two cases, one is correct other is not.

Case-1 (Correct)

from packaging import version
>>> a = version.parse('8.0.3-a6754d8441bf')
>>> b = version.parse('8.0.3-a6754d8441bg')
>>> a < b
True

Case-2 (Incorrect)

from packaging import version
>>> a = version.parse('8.0.3-a6754d8441bf')
>>> b = version.parse('8.0.0')
>>> a < b
True

In second example, "8.0.3-a6754d8441bf" is definitely greater than "8.0.0", but library is not reporting this.

Please check.

@pradyunsg
Copy link
Member

>>> from packaging import version
>>> a = version.parse('8.0.3-a6754d8441bf')
>>> a
<LegacyVersion('8.0.3-a6754d8441bf')>
>>> b = version.parse('8.0.0')
>>> b
<Version('8.0.0')>
>>> a < b
True

The issue here is that you're getting a LegacyVersion for "a" and a proper Version for "b". The behavior of LegacyVersion < Version is intentional. This is happening because a isn't a proper version number according to PEP 440:

>>> version.Version('8.0.3-a6754d8441bf')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/pradyunsg/Projects/packaging/packaging/version.py", line 277, in __init__
    raise InvalidVersion("Invalid version: '{0}'".format(version))
packaging.version.InvalidVersion: Invalid version: '8.0.3-a6754d8441bf'

I'd recommend either:

  1. changing the versioning scheme to be compatible with PEP 440, by using the local version specifier for the (commit?) hash you're adding: version.Version('8.0.3+a6754d8441bf').
  2. OR, using the LegacyVersion constructor directly in your case, if you don't want to use the modern PEP 440 logic.

PS: Your comment seemed to be poorly formatted. I've edited it -- I suggest you read https://help.github.com/articles/getting-started-with-writing-and-formatting-on-github/.

@brettcannon
Copy link
Member

Since we aren't going to be changing the logic I vote for closing this issue.

@di
Copy link
Member

di commented Oct 19, 2020

Ultimately I think this is a duplicate of #321 as well.

@di di closed this as completed Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants