packaging icon indicating copy to clipboard operation
packaging copied to clipboard

`Specifier` Greater than comparison returns incorrect result for a version with dev+local parts

Open rng-zz opened this issue 1 year ago • 0 comments

When a using a Specifier with a > comparison, comparing returns an incorrect result for the following situation:

  • the base version of the specifier and compared version are equal
  • the compared version includes a local segment
  • the compared version differs in its dev segment.

My understanding from PEP440 is that the entire public portion of the version (including the dev segment) should be included in the comparison? I think this is due to this line: https://github.com/pypa/packaging/blob/main/src/packaging/specifiers.py#L489 where it should instead be:

        if prospective.local is not None:
            if Version(prospective.public) == Version(spec.public):
                return False

This is related, but not exactly the same as https://github.com/pypa/packaging/issues/519 (as this issue is specifically about comparing two dev versions).

Example:

>>> from pip._vendor.packaging.specifiers import Specifier
>>> spec = Specifier(">4.1.0a2.dev1234")

# Correct comparisons with no local segment
>>> spec.contains("4.1.0a2.dev1234", prereleases=True)
False
>>> spec.contains("4.1.0a2.dev1235", prereleases=True)
True

# Incorrect comparison when including a local segment
>>> spec.contains("4.1.0a2.dev1234+local", prereleases=True)
False
>>> spec.contains("4.1.0a2.dev1235+local", prereleases=True)
False

rng-zz avatar Jun 04 '24 14:06 rng-zz