Guarantee a stable ordering with build metadata#133
Merged
Conversation
Sorting any permutation of Version objects should always yield the same result, even if those hold some build metadata. To that end, the "precedence_key" is now used exclusively for sorting; direct comparisons between Version objects still ignores the "build" metadata, using a different precedence key. For performance improvements, both precedence keys are cached. Closes: #132
|
Something isn't adding up ;) Python 3.9.12 (main, Mar 26 2022, 15:51:15)
[Clang 13.1.6 (clang-1316.0.21.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import semantic_version
>>> semantic_version.__version__
'2.10.0'
>>> v1 = semantic_version.Version("2.0.12+123")
>>> v2 = semantic_version.Version("2.0.12+1")
>>> v3 = semantic_version.Version("2.0.12+321")
>>> v4 = semantic_version.Version("2.0.12+22")
>>> sorted([v1,v2,v3,v4])
[Version('2.0.12+123'), Version('2.0.12+1'), Version('2.0.12+321'), Version('2.0.12+22')]
>>> sorted([v2,v1,v3,v4])
[Version('2.0.12+1'), Version('2.0.12+123'), Version('2.0.12+321'), Version('2.0.12+22')]
>>> sorted([v3,v2,v1,v4])
[Version('2.0.12+321'), Version('2.0.12+1'), Version('2.0.12+123'), Version('2.0.12+22')]
>>> sorted([v4,v2,v1,v3])
[Version('2.0.12+22'), Version('2.0.12+1'), Version('2.0.12+123'), Version('2.0.12+321')] |
Owner
Author
|
@keshav-space Use |
|
Is there a specific reason for not having this as a default behavior? |
Owner
Author
|
Yes: it would break the existing documented behaviour (and several tests from the test suite), requiring a major version bump! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sorting any permutation of Version objects should always yield the same
result, even if those hold some build metadata.
To that end, the "precedence_key" is now used exclusively for sorting;
direct comparisons between Version objects still ignores the "build"
metadata, using a different precedence key.
For performance improvements, both precedence keys are cached.
Closes: #132