Skip to content

Conversation

@TrevisGordan
Copy link

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:

  1. Enhanced Parsing for Partial Semantic Versions:
    By updating and using the following patterns:
version_pattern = re.compile(r"^[vV](?P<major>[0-9]+)(?:\.(?P<minor>[0-9]+)(?:\.(?P<build>[0-9]+))?)?$")
stripped_version_pattern = re.compile(r"^(?P<major>[0-9]+)(?:\.(?P<minor>[0-9]+)(?:\.(?P<build>[0-9]+))?)?$")

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

  1. Introduction of from_number for Backwards Compatibility:
    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:
Version.from_number(2001)  # -> v0.2.1 (legacy logic)
  1. 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

  2. Minor changes:

  • Implements f-string instead of format.
  • Added / Updated tests.

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

Partial Removes number_version & pattern. Adds from_number method.
Replaces Number comparison logic for tuple comparison logic.
Adds .venv to gitignore file
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.

1 participant