From c63a2bcddc87e53e30cb566560526cc40ed67788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atte=20Valtoj=C3=A4rvi?= Date: Wed, 10 Apr 2024 15:01:33 +0300 Subject: [PATCH 1/6] Return an instance of cls --- typeid/typeid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typeid/typeid.py b/typeid/typeid.py index 90e6784..8a3edf6 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -27,7 +27,7 @@ def from_string(cls, string: str): @classmethod def from_uuid(cls, suffix: UUID, prefix: Optional[str] = None): suffix_str = _convert_uuid_to_b32(suffix) - return TypeID(suffix=suffix_str, prefix=prefix) + return cls(suffix=suffix_str, prefix=prefix) @property def suffix(self) -> str: From 9336931fa1b185c68b6bd801092b7307ae6706c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atte=20Valtoj=C3=A4rvi?= Date: Wed, 10 Apr 2024 15:02:02 +0300 Subject: [PATCH 2/6] Changed tests to use class methods instead of deprecated functions --- tests/test_spec.py | 6 +++--- tests/test_typeid.py | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/test_spec.py b/tests/test_spec.py index 1165d86..ec47320 100644 --- a/tests/test_spec.py +++ b/tests/test_spec.py @@ -1,13 +1,13 @@ import pytest from uuid6 import UUID -from typeid import from_string, from_uuid +from typeid import TypeID def test_invalid_spec(invalid_spec: list) -> None: for spec in invalid_spec: with pytest.raises(Exception): - from_string(spec["typeid"]) + TypeID.from_string(spec["typeid"]) def test_valid_spec(valid_spec: list) -> None: @@ -15,5 +15,5 @@ def test_valid_spec(valid_spec: list) -> None: prefix = spec["prefix"] uuid = UUID(spec["uuid"]) - typeid = from_uuid(prefix=prefix, suffix=uuid) + typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid) assert str(typeid) == spec["typeid"] diff --git a/tests/test_typeid.py b/tests/test_typeid.py index 60adb31..de09f5f 100644 --- a/tests/test_typeid.py +++ b/tests/test_typeid.py @@ -1,7 +1,7 @@ import pytest from uuid6 import uuid7 -from typeid import TypeID, from_string, from_uuid +from typeid import TypeID from typeid.errors import InvalidTypeIDStringException @@ -57,7 +57,7 @@ def test_construct_type_from_string() -> None: def test_construct_type_from_string_standalone() -> None: string = "00041061050r3gg28a1c60t3gf" - typeid = from_string(string) + typeid = TypeID.from_string(string) assert isinstance(typeid, TypeID) assert typeid.prefix == "" @@ -77,7 +77,7 @@ def test_construct_type_from_string_with_prefix() -> None: def test_construct_type_from_string_with_prefix_standalone() -> None: string = "prefix_00041061050r3gg28a1c60t3gf" - typeid = from_string(string) + typeid = TypeID.from_string(string) assert isinstance(typeid, TypeID) assert typeid.prefix == "prefix" @@ -88,13 +88,13 @@ def test_construct_type_from_invalid_string() -> None: string = "invalid_string_to_typeid" with pytest.raises(InvalidTypeIDStringException): - from_string(string) + TypeID.from_string(string) def test_construct_type_from_uuid() -> None: uuid = uuid7() - typeid = from_uuid(suffix=uuid, prefix="") + typeid = TypeID.from_uuid(suffix=uuid, prefix="") assert isinstance(typeid, TypeID) assert typeid.prefix == "" @@ -105,7 +105,7 @@ def test_construct_type_from_uuid_with_prefix() -> None: uuid = uuid7() prefix = "prefix" - typeid = from_uuid(prefix=prefix, suffix=uuid) + typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid) assert isinstance(typeid, TypeID) assert typeid.prefix == "prefix" From 69c0b3dd7bced1abb153eaeb526153a28b0d369c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atte=20Valtoj=C3=A4rvi?= Date: Wed, 10 Apr 2024 15:02:39 +0300 Subject: [PATCH 3/6] Unused variable removed --- typeid/typeid.py | 1 - 1 file changed, 1 deletion(-) diff --git a/typeid/typeid.py b/typeid/typeid.py index 8a3edf6..abe47a2 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -82,7 +82,6 @@ def from_uuid(suffix: UUID, prefix: Optional[str] = None) -> TypeID: def get_prefix_and_suffix(string: str) -> tuple: parts = string.split("_") - suffix = None prefix = None if len(parts) == 1: suffix = parts[0] From 86fcb54553ae16635d14c2d3afa8b56e865e567f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atte=20Valtoj=C3=A4rvi?= Date: Wed, 10 Apr 2024 15:25:16 +0300 Subject: [PATCH 4/6] Return uuid6.UUID from utility function so that the used UUID version is consistently 7 --- tests/test_typeid.py | 16 +++++++++++++--- typeid/typeid.py | 19 ++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/test_typeid.py b/tests/test_typeid.py index de09f5f..2f4dd26 100644 --- a/tests/test_typeid.py +++ b/tests/test_typeid.py @@ -1,5 +1,5 @@ import pytest -from uuid6 import uuid7 +import uuid6 from typeid import TypeID from typeid.errors import InvalidTypeIDStringException @@ -92,7 +92,7 @@ def test_construct_type_from_invalid_string() -> None: def test_construct_type_from_uuid() -> None: - uuid = uuid7() + uuid = uuid6.uuid7() typeid = TypeID.from_uuid(suffix=uuid, prefix="") @@ -102,7 +102,7 @@ def test_construct_type_from_uuid() -> None: def test_construct_type_from_uuid_with_prefix() -> None: - uuid = uuid7() + uuid = uuid6.uuid7() prefix = "prefix" typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid) @@ -122,3 +122,13 @@ def test_hash_type_id() -> None: assert hash(typeid_1) == hash(typeid_2) assert hash(typeid_3) != hash(typeid_1) + + +def test_uuid_property() -> None: + uuid = uuid6.uuid7() + + typeid = TypeID.from_uuid(suffix=uuid) + + assert isinstance(typeid.uuid, uuid6.UUID) + assert typeid.uuid.version == uuid.version == 7 + assert typeid.uuid.time == uuid.time diff --git a/typeid/typeid.py b/typeid/typeid.py index abe47a2..07cc8c7 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -1,8 +1,7 @@ import warnings from typing import Optional -from uuid import UUID -from uuid6 import uuid7 +import uuid6 from typeid import base32 from typeid.errors import InvalidTypeIDStringException @@ -11,7 +10,7 @@ class TypeID: def __init__(self, prefix: Optional[str] = None, suffix: Optional[str] = None) -> None: - suffix = _convert_uuid_to_b32(uuid7()) if not suffix else suffix + suffix = _convert_uuid_to_b32(uuid6.uuid7()) if not suffix else suffix validate_suffix(suffix=suffix) if prefix: validate_prefix(prefix=prefix) @@ -25,7 +24,7 @@ def from_string(cls, string: str): return cls(suffix=suffix, prefix=prefix) @classmethod - def from_uuid(cls, suffix: UUID, prefix: Optional[str] = None): + def from_uuid(cls, suffix: uuid6.UUID, prefix: Optional[str] = None): suffix_str = _convert_uuid_to_b32(suffix) return cls(suffix=suffix_str, prefix=prefix) @@ -38,7 +37,7 @@ def prefix(self) -> str: return self._prefix @property - def uuid(self) -> UUID: + def uuid(self) -> uuid6.UUID: return _convert_b32_to_uuid(self.suffix) def __str__(self) -> str: @@ -75,7 +74,7 @@ def from_string(string: str) -> TypeID: return TypeID.from_string(string=string) -def from_uuid(suffix: UUID, prefix: Optional[str] = None) -> TypeID: +def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID: warnings.warn("Consider TypeID.from_uuid instead.", DeprecationWarning) return TypeID.from_uuid(suffix=suffix, prefix=prefix) @@ -94,9 +93,11 @@ def get_prefix_and_suffix(string: str) -> tuple: return prefix, suffix -def _convert_uuid_to_b32(uuid_instance: UUID) -> str: +def _convert_uuid_to_b32(uuid_instance: uuid6.UUID) -> str: return base32.encode(list(uuid_instance.bytes)) -def _convert_b32_to_uuid(b32: str) -> UUID: - return UUID(bytes=bytes(base32.decode(b32))) +def _convert_b32_to_uuid(b32: str) -> uuid6.UUID: + uuid_bytes = bytes(base32.decode(b32)) + uuid_int = int.from_bytes(uuid_bytes) + return uuid6.UUID(int=uuid_int, version=7) From 7023b37cfc68e75c5772cfb5e518927e1b06a474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atte=20Valtoj=C3=A4rvi?= Date: Fri, 12 Apr 2024 13:31:01 +0300 Subject: [PATCH 5/6] Added byteorder parameter for python 3.10 (and older) compatibility --- typeid/typeid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typeid/typeid.py b/typeid/typeid.py index 07cc8c7..8bbd91b 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -99,5 +99,5 @@ def _convert_uuid_to_b32(uuid_instance: uuid6.UUID) -> str: def _convert_b32_to_uuid(b32: str) -> uuid6.UUID: uuid_bytes = bytes(base32.decode(b32)) - uuid_int = int.from_bytes(uuid_bytes) + uuid_int = int.from_bytes(uuid_bytes, byteorder='big') return uuid6.UUID(int=uuid_int, version=7) From 010a02e3cabc563ce3a0a2baee026f5e32b91004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atte=20Valtoj=C3=A4rvi?= Date: Fri, 12 Apr 2024 13:37:52 +0300 Subject: [PATCH 6/6] Changed parentheses --- typeid/typeid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typeid/typeid.py b/typeid/typeid.py index 8bbd91b..f0f5da6 100644 --- a/typeid/typeid.py +++ b/typeid/typeid.py @@ -99,5 +99,5 @@ def _convert_uuid_to_b32(uuid_instance: uuid6.UUID) -> str: def _convert_b32_to_uuid(b32: str) -> uuid6.UUID: uuid_bytes = bytes(base32.decode(b32)) - uuid_int = int.from_bytes(uuid_bytes, byteorder='big') + uuid_int = int.from_bytes(uuid_bytes, byteorder="big") return uuid6.UUID(int=uuid_int, version=7)