Skip to content

Commit

Permalink
refactor: remove int inheritance from Version
Browse files Browse the repository at this point in the history
  • Loading branch information
jjaakola-aiven committed Jun 5, 2024
1 parent a189931 commit 6136242
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion karapace/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions karapace/in_memory_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion karapace/schema_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion karapace/schema_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def to_dict(self) -> JsonData:
return {
"name": self.name,
"subject": self.subject,
"version": self.version,
"version": self.version.value,
}

@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions karapace/schema_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 31 additions & 7 deletions karapace/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions tests/unit/test_schema_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down

0 comments on commit 6136242

Please sign in to comment.