Coverage for kwai/core/domain/value_objects/identifier.py: 95%
22 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Module that defines identifiers."""
2from abc import ABC, abstractmethod
3from typing import Generic, TypeVar
5T = TypeVar("T")
8class Identifier(ABC, Generic[T]):
9 """Abstract and generic class for an identifier."""
11 def __init__(self, id_: T):
12 self._id = id_
14 @property
15 def value(self) -> T:
16 """Return the id."""
17 return self._id
19 def __eq__(self, other: "Identifier"):
20 """Check the equality of identifiers."""
21 return self._id == other._id
23 def __hash__(self):
24 """Create a hash for an identifier."""
25 return hash(self._id)
27 @abstractmethod
28 def is_empty(self) -> bool:
29 """Return true when the identifier is not set."""
30 raise NotImplementedError
32 def __str__(self) -> str:
33 """Return the string representation of the id."""
34 return str(self._id)
37class IntIdentifier(Identifier[int]):
38 """Class that implements an identifier with an integer."""
40 def __init__(self, id_: int = 0):
41 super().__init__(id_)
43 def is_empty(self) -> bool:
44 """Return True when the id equals 0."""
45 return self.value == 0
47 def __repr__(self):
48 """Return a string representation of the identifier."""
49 return f"IntIdentifier={self.value}"