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

Add DistroVersion class to compare distro versions #3078

Merged
merged 5 commits into from
Mar 6, 2024

Conversation

narrieta
Copy link
Member

@narrieta narrieta commented Mar 4, 2024

A previous PR added a copy of distuls/version.py as a temporary workaround to use LooseVersion (since it is deprecated and will be removed from Python). This PR replaces that copy with a new class: DistroVersion that implements the same behavior in terms of comparing versions.

The results of some of the comparisons done with LooseVersion/DistroVersion are not always intuitive and can lead to subtle code bugs (e.g. "1" != "1.0" != "1.0.0", etc). There are a couple of places in the code where we use similar comparisons; be careful with those.

Also, we have another class that deals with versions, FlexibleVersion, with its own behavior. This creates confusion as to what class to use when comparing versions.

I'll do another iteration of this code to merge DistroVersion and FlexibleVersion.

@@ -68,23 +67,6 @@ def test_remove_bom(self):
data = textutil.remove_bom(data)
self.assertEqual(u" ", data)

def test_version_compare(self):
Copy link
Member Author

Choose a reason for hiding this comment

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

moved to test_distro_version.py

Copy link

codecov bot commented Mar 4, 2024

Codecov Report

Attention: Patch coverage is 82.97872% with 8 lines in your changes are missing coverage. Please review.

Project coverage is 71.91%. Comparing base (68b77af) to head (c07fca7).

Files Patch % Lines
azurelinuxagent/common/utils/distro_version.py 86.66% 3 Missing and 1 partial ⚠️
azurelinuxagent/common/osutil/factory.py 90.00% 0 Missing and 1 partial ⚠️
azurelinuxagent/ga/exthandlers.py 0.00% 1 Missing ⚠️
azurelinuxagent/pa/deprovision/factory.py 50.00% 0 Missing and 1 partial ⚠️
azurelinuxagent/pa/rdma/suse.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3078      +/-   ##
===========================================
+ Coverage    71.62%   71.91%   +0.28%     
===========================================
  Files          110      110              
  Lines        16433    16395      -38     
  Branches      2352     2342      -10     
===========================================
+ Hits         11770    11790      +20     
+ Misses        4112     4054      -58     
  Partials       551      551              

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


# AttributeError: 'FlexibleVersion' object has no attribute '_fragments'
with self.assertRaises(AttributeError):
_ = DistroVersion("1.0.0.0") == FlexibleVersion("1.0.0.0")
Copy link
Contributor

Choose a reason for hiding this comment

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

you mean it will fail with Attribute error If I use FlexibleVersion in the code. I didn't this comment

Copy link
Member Author

@narrieta narrieta Mar 6, 2024

Choose a reason for hiding this comment

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

this comparison raises AttributeError. the comment describes the message in the exception

self.assertTrue(DistroVersion("FFFF") < DistroVersion("h"))
self.assertTrue(DistroVersion("None") < DistroVersion("n/a"))

if sys.version_info[0] == 2:
Copy link
Contributor

Choose a reason for hiding this comment

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

That means in prod there a chance that code can hit typeerror and break the agent?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, the current code with LooseVersion has this issue

self.assertTrue(DistroVersion("1.0.0") == DistroVersion("1.0.0"))
self.assertTrue(DistroVersion("1.0.0") != DistroVersion("2.0.0"))

self.assertTrue(DistroVersion("13") != DistroVersion("13.0"))
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks to me DistroVersion is also variant of strict version and Flexible version also documented as same. Is that means both does same thing?

13 != 13.0 !=13.0.0 is true in both modules?

Copy link
Member Author

Choose a reason for hiding this comment

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

DistroVersion is not a variant of StrictVersion, but of LooseVersion

Copy link
Member Author

Choose a reason for hiding this comment

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

13 != 13.0 !=13.0.0 is true in both modules?

FlexibleVersion implements the comparison as expected:

>>> FlexibleVersion("13") == FlexibleVersion("13.0")
True
>>> FlexibleVersion("13") == FlexibleVersion("13.0.0")
True
>>> FlexibleVersion("13") == FlexibleVersion("13.0.1")
False

@narrieta narrieta merged commit 3e4fb9a into Azure:develop Mar 6, 2024
13 checks passed
@narrieta narrieta deleted the version branch March 6, 2024 18:41
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.

3 participants