From 6136242be69c8043e7ee2bf043a2a9cbbdc1dc2f Mon Sep 17 00:00:00 2001 From: Jarkko Jaakola Date: Wed, 5 Jun 2024 21:51:53 +0300 Subject: [PATCH] refactor: remove int inheritance from Version --- karapace/dependency.py | 2 +- karapace/in_memory_database.py | 4 ++-- karapace/schema_reader.py | 2 +- karapace/schema_references.py | 2 +- karapace/schema_registry.py | 6 ++--- karapace/typing.py | 38 ++++++++++++++++++++++++++------ tests/unit/test_schema_models.py | 6 ++--- 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/karapace/dependency.py b/karapace/dependency.py index 074263af7..a289e1f25 100644 --- a/karapace/dependency.py +++ b/karapace/dependency.py @@ -45,7 +45,7 @@ def to_dict(self) -> JsonData: return { "name": self.name, "subject": self.subject, - "version": self.version, + "version": self.version.value, } def identifier(self) -> str: diff --git a/karapace/in_memory_database.py b/karapace/in_memory_database.py index 81bf474d4..01e0153c1 100644 --- a/karapace/in_memory_database.py +++ b/karapace/in_memory_database.py @@ -101,7 +101,7 @@ def _get_from_hash_cache(self, *, typed_schema: TypedSchema) -> TypedSchema: return self._hash_to_schema.setdefault(typed_schema.fingerprint(), typed_schema) def get_next_version(self, *, subject: Subject) -> Version: - return Versioner.V(max(self.subjects[subject].schemas) + 1) + return Versioner.V(max(self.subjects[subject].schemas).value + 1) def insert_schema_version( self, @@ -224,7 +224,7 @@ def find_subject_schemas(self, *, subject: Subject, include_deleted: bool) -> di return self.subjects[subject].schemas with self.schema_lock_thread: return { - Versioner.V(version_id): schema_version + version_id: schema_version for version_id, schema_version in self.subjects[subject].schemas.items() if schema_version.deleted is False } diff --git a/karapace/schema_reader.py b/karapace/schema_reader.py index f591ac3a5..9e9931a1a 100644 --- a/karapace/schema_reader.py +++ b/karapace/schema_reader.py @@ -501,7 +501,7 @@ def _handle_msg_schema(self, key: dict, value: dict | None) -> None: schema_str = value["schema"] schema_subject = value["subject"] schema_id = value["id"] - schema_version = value["version"] + schema_version = Version(value["version"]) schema_deleted = value.get("deleted", False) schema_references = value.get("references", None) resolved_references: list[Reference] | None = None diff --git a/karapace/schema_references.py b/karapace/schema_references.py index 9973b0ccb..0eae47141 100644 --- a/karapace/schema_references.py +++ b/karapace/schema_references.py @@ -60,7 +60,7 @@ def to_dict(self) -> JsonData: return { "name": self.name, "subject": self.subject, - "version": self.version, + "version": self.version.value, } @staticmethod diff --git a/karapace/schema_registry.py b/karapace/schema_registry.py index 2c6d46bfd..6312ab7b3 100644 --- a/karapace/schema_registry.py +++ b/karapace/schema_registry.py @@ -248,7 +248,7 @@ def subject_version_get(self, subject: Subject, version: Version, *, include_del ret: JsonObject = { "subject": subject, - "version": resolved_version, + "version": resolved_version.value, "id": schema_id, "schema": schema.schema_str, } @@ -427,11 +427,11 @@ def send_schema_message( deleted: bool, references: Sequence[Reference] | None, ) -> None: - key = {"subject": subject, "version": version, "magic": 1, "keytype": "SCHEMA"} + key = {"subject": subject, "version": version.value, "magic": 1, "keytype": "SCHEMA"} if schema: value = { "subject": subject, - "version": version, + "version": version.value, "id": schema_id, "schema": str(schema), "deleted": deleted, diff --git a/karapace/typing.py b/karapace/typing.py index 40b29fa2d..c7e702201 100644 --- a/karapace/typing.py +++ b/karapace/typing.py @@ -59,23 +59,47 @@ class Mode(StrEnum): readwrite = "READWRITE" -class Version(int): +class Version: LATEST_VERSION_TAG: ClassVar[str] = "latest" MINUS_1_VERSION_TAG: ClassVar[int] = -1 - def __new__(cls, version: int) -> Version: + def __init__(self, version: int) -> None: if not isinstance(version, int): raise InvalidVersion(f"Invalid version {version}") - if (version < cls.MINUS_1_VERSION_TAG) or (version == 0): + if (version < Version.MINUS_1_VERSION_TAG) or (version == 0): raise InvalidVersion(f"Invalid version {version}") - return super().__new__(cls, version) + self._value = version def __str__(self) -> str: - return f"{int(self)}" + return f"{int(self._value)}" def __repr__(self) -> str: - return f"Version={int(self)}" + return f"Version({int(self._value)})" + + def __gt__(self, other: Version) -> bool: + return self._value > other.value + + def __ge__(self, other: Version) -> bool: + return self._value >= other.value + + def __lt__(self, other: Version) -> bool: + return self._value < other.value + + def __le__(self, other: Version) -> bool: + return self._value <= other.value + + def __eq__(self, other: object) -> bool: + if isinstance(other, Version): + return self._value == other.value + return False + + def __hash__(self) -> int: + return hash(self._value) + + @property + def value(self) -> int: + return self._value @property def is_latest(self) -> bool: - return self == self.MINUS_1_VERSION_TAG + return self.value == self.MINUS_1_VERSION_TAG diff --git a/tests/unit/test_schema_models.py b/tests/unit/test_schema_models.py index fc1590da9..9b27d27a0 100644 --- a/tests/unit/test_schema_models.py +++ b/tests/unit/test_schema_models.py @@ -25,9 +25,9 @@ def version(self): return Versioner.V(1) def test_version(self, version: Version): - assert version == 1 + assert version == Version(1) assert isinstance(version, Version) - assert issubclass(Version, int) + assert isinstance(version.value, int) def test_tags(self, version: Version): assert version.LATEST_VERSION_TAG == "latest" @@ -47,7 +47,7 @@ def test_is_latest(self, version: Version, is_latest: bool): def test_text_formating(self, version: Version): assert f"{version}" == "1" - assert f"{version!r}" == "Version=1" + assert f"{version!r}" == "Version(1)" @pytest.mark.parametrize( "version, to_compare, comparer, valid",