Improve Partial Semantic Version Parsing and Enhance Comparison Logic #2
+145
−78
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.
This PR updates the version parsing logic to handle partial semantic versions more gracefully, ensuring that shorter version strings like v2.3 or v1 (and even just 1) are correctly interpreted as v2.3.0 and 1.0.0 respectively. Previously, the parser did not fully support partial versions—it required explicit major.minor.build structures. The new logic leverages updated regex patterns and a simplified parsing strategy to fill in missing parts with default values (e.g., missing build defaults to 0).
Key Changes:
By updating and using the following patterns:
Versions that omit minor or build parts are now parsed with defaults (e.g., major present but no minor means minor=0, similarly for build).
Examples:
• "v1" → v1.0.0
• "v2.3" → v2.3.0
• "1" → 1.0.0
• "2.3" → 2.3.0
Previously, some code relied on interpreting plain numbers as versions (e.g. 2001 -> v0.2.1). To keep this legacy functionality available, I introduced Version.from_number(int_value). This method encapsulates the old numeric parsing logic without cluttering the main constructor.
Example:
Tuple-Based Comparison Instead of Numeric Encoding:
To simplify and clarify version comparisons, the code now compares (major, minor, build) tuples directly rather than converting versions into numeric encodings. Tuple comparison is intuitive and aligns well with semantic versioning principles.
Examples:
• (1,2,0) < (1,2,1) is True
• (1,2,3) < (1,3,0) is True
• (2,2,3) < (2,5,2) is True
• (5,2,3) < (2,4,5) is False
Minor changes:
Why These Changes?
• Flexibility in Parsing: Supporting partial versions (like v2.3 → v2.3.0) was a core goal, making the codebase more robust and user-friendly.
• Clarity and Maintainability: Moving legacy numeric parsing into a dedicated method and using tuples for comparison reduces confusion and makes the version handling more transparent.
• Backwards Compatibility: Existing code relying on numeric versions can still work via Version.from_number(), ensuring a smooth transition.
Missing:
Update to Readme / Docs (i will/can update Docs, if this should get merged)
Add Typing