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

Using packaging.version instead of pkg_resources #2825

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

asp8200
Copy link
Contributor

@asp8200 asp8200 commented Mar 1, 2024

#1984

PR checklist

  • This comment contains a description of changes (with reason)
  • CHANGELOG.md is updated
  • If you've fixed a bug or added code that should be tested, add tests!
  • Documentation in docs is updated

Copy link

codecov bot commented Mar 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.78%. Comparing base (03646eb) to head (3a05c73).

Additional details and impacted files

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@asp8200 asp8200 marked this pull request as ready for review March 15, 2024 11:10
@ewels ewels requested a review from fabianegli March 18, 2024 22:32
version_parser(v)
for v in desired_revisions
if re.match(r"\d+\.\d+(?:\.\d+)*(?:[\w\-_])*", v)
str(Version(v)) for v in desired_revisions if re.match(r"\d+\.\d+(?:\.\d+)*(?:[\w\-_])*", v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with the usage of packaging.Version() as it stands here now is mentioned in the comment above.

desired revisions may contain arbitrary branch names that do not correspond to valid semantic versioning patterns.

And packaging.Version has its own definition of what a valid version identifier is and it is not completely compatible with semantic versions either.

As an example:

>>> [str(Version(v)) for v in ["1.1.1-latest"] if re.match(r"\d+\.\d+(?:\.\d+)*(?:[\w\-_])*", v)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/packaging/version.py", line 298, in __init__
    raise InvalidVersion("Invalid version: '{0}'".format(version))
packaging.version.InvalidVersion: Invalid version: '1.1.1-latest'

This will not work nicely if any of the desired revisions fails validity checking inside Version().

The previous version parser from pkg_resources import parse_version returns a LegacyVersion if it can't parse the identifier, which makes it much better suited in this context than packaging.Version.

>>> [version_parser(v) for v in ["1.1.1", "1.2.3-latest"]]
[<Version('1.1.1')>, <LegacyVersion('1.2.3-latest')>]

I am not versed enough to know what kind of strings can appear in desired_revisions but I believe this code should be able to sort a broader range of version specifiers.

version_parser(v)
for v in desired_revisions
if re.match(r"\d+\.\d+(?:\.\d+)*(?:[\w\-_])*", v)
str(Version(v)) for v in desired_revisions if re.match(r"\d+\.\d+(?:\.\d+)*(?:[\w\-_])*", v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A separate issue is the conversion of the Version instance to a string. It will lead to wrong orderings as demonstrated below with a version number which had a release candidate.

>>> sorted(str(Version(v)) for v in ['1.23.4', '1.23.4-1', '1.23.5', '1.23.5rc'])[-1]
'1.23.5rc0'
>>> sorted(Version(v) for v in ['1.23.4', '1.23.4-1', '1.23.5', '1.23.5rc'])[-1] 
<Version('1.23.5')>

Copy link
Contributor

@fabianegli fabianegli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as the PR stands now with the use of packaging.Version has a good chance of breaking some use cases because it sets tighter restrictions on what a valid revision is and Throws an exception if this stricter requirement isn't met.

The conversion of the Version objects to strings as it currently stands will lead to erroneous sorting of version identifiers and should be dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants