Skip to content

Commit

Permalink
make TypeID hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Sep 19, 2023
1 parent 04f20ae commit 9ab5e17
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tests/test_typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,15 @@ def test_construct_type_from_uuid_with_prefix() -> None:
assert isinstance(typeid, TypeID)
assert typeid.prefix == "prefix"
assert isinstance(typeid.suffix, str)


def test_hash_type_id() -> None:
prefix = "plov"
suffix = "00041061050r3gg28a1c60t3gf"

typeid_1 = TypeID(prefix=prefix, suffix=suffix)
typeid_2 = TypeID(prefix=prefix, suffix=suffix)
typeid_3 = TypeID(suffix=suffix)

assert hash(typeid_1) == hash(typeid_2)
assert hash(typeid_3) != hash(typeid_1)
3 changes: 3 additions & 0 deletions typeid/typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def __eq__(self, value: object) -> bool:
return False
return value.prefix == self.prefix and value.suffix == self.suffix

def __hash__(self) -> int:
return hash((self.prefix, self.suffix))


def from_string(string: str) -> TypeID:
prefix, suffix = get_prefix_and_suffix(string=string)
Expand Down

0 comments on commit 9ab5e17

Please sign in to comment.