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

fix(core): Typing in version #701

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions core/testcontainers/core/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@


class ComparableVersion:
def __init__(self, version):
"""A wrapper around packaging.version.Version that allows for comparison with strings"""

def __init__(self, version: str) -> None:
self.version = Version(version)

def __lt__(self, other: str):
def __lt__(self, other: object) -> bool:
return self._apply_op(other, lambda x, y: x < y)

def __le__(self, other: str):
def __le__(self, other: object) -> bool:
return self._apply_op(other, lambda x, y: x <= y)

def __eq__(self, other: str):
def __eq__(self, other: object) -> bool:
return self._apply_op(other, lambda x, y: x == y)

def __ne__(self, other: str):
def __ne__(self, other: object) -> bool:
return self._apply_op(other, lambda x, y: x != y)

def __gt__(self, other: str):
def __gt__(self, other: object) -> bool:
return self._apply_op(other, lambda x, y: x > y)

def __ge__(self, other: str):
def __ge__(self, other: object) -> bool:
return self._apply_op(other, lambda x, y: x >= y)

def _apply_op(self, other: str, op: Callable[[Version, Version], bool]):
other = Version(other)
def _apply_op(self, other: object, op: Callable[[Version, Version], bool]) -> bool:
other = Version(str(other))
return op(self.version, other)