Skip to content

Commit

Permalink
✨ Make versions comparible
Browse files Browse the repository at this point in the history
Implement comparison functions for version objects `PythonVersion` and `PythonRelease`.
  • Loading branch information
RafaelWO committed Dec 27, 2024
1 parent a755582 commit 1324a44
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
39 changes: 39 additions & 0 deletions pirel/_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from __future__ import annotations

import abc


class VersionLike(abc.ABC):
"""Abstract class for objects that hold a version (`tuple` of `int`s) and
should be comparible based on their version.
"""

@property
@abc.abstractmethod
def version_tuple(self) -> tuple[int, ...]:
pass

def __eq__(self, other: object) -> bool:
if not isinstance(other, VersionLike):
return NotImplemented
return self.version_tuple == other.version_tuple

def __lt__(self, other: object) -> bool:
if not isinstance(other, VersionLike):
return NotImplemented
return self.version_tuple < other.version_tuple

def __le__(self, other: object) -> bool:
if not isinstance(other, VersionLike):
return NotImplemented
return self.version_tuple <= other.version_tuple

def __gt__(self, other: object) -> bool:
if not isinstance(other, VersionLike):
return NotImplemented
return self.version_tuple > other.version_tuple

def __ge__(self, other: object) -> bool:
if not isinstance(other, VersionLike):
return NotImplemented
return self.version_tuple >= other.version_tuple
8 changes: 7 additions & 1 deletion pirel/python_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import sys
from dataclasses import dataclass

from . import _utils

PYTHON_VERSION_RE = re.compile(r"Python ([23])\.(\d+)\.(\d+)")
logger = logging.getLogger("pirel")


@dataclass(frozen=True)
class PythonVersion:
class PythonVersion(_utils.VersionLike):
major: int
minor: int
patch: int
Expand All @@ -34,6 +36,10 @@ def this(cls) -> PythonVersion:
def as_release(self) -> str:
return f"{self.major}.{self.minor}"

@property
def version_tuple(self) -> tuple[int, int, int]:
return (self.major, self.minor, self.patch)

def __str__(self) -> str:
return f"{self.major}.{self.minor}.{self.patch}"

Expand Down
8 changes: 6 additions & 2 deletions pirel/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import typer
from rich.table import Table

from . import _cache, python_cli
from . import _cache, _utils, python_cli

DATE_NOW = datetime.date.today()
RELEASE_CYCLE_URL = "https://raw.githubusercontent.com/python/devguide/refs/heads/main/include/release-cycle.json"
Expand Down Expand Up @@ -82,7 +82,7 @@ def wrap_style(text: str, style: str) -> str:
return f"[{style.strip()}]{text}[/{style.strip()}]"


class PythonRelease:
class PythonRelease(_utils.VersionLike):
def __init__(self, version: str, data: dict[str, Any]):
self._version = version
self._status: str = data["status"]
Expand All @@ -105,6 +105,10 @@ def __str__(self) -> str:
def version(self) -> str:
return self._version

@property
def version_tuple(self) -> tuple[int, ...]:
return tuple(map(int, self._version.split(".")))

@property
def status(self) -> str:
return wrap_style(self._status, status_style(self._status))
Expand Down

0 comments on commit 1324a44

Please sign in to comment.