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

1"""Module that defines identifiers.""" 

2from abc import ABC, abstractmethod 

3from typing import Generic, TypeVar 

4 

5T = TypeVar("T") 

6 

7 

8class Identifier(ABC, Generic[T]): 

9 """Abstract and generic class for an identifier.""" 

10 

11 def __init__(self, id_: T): 

12 self._id = id_ 

13 

14 @property 

15 def value(self) -> T: 

16 """Return the id.""" 

17 return self._id 

18 

19 def __eq__(self, other: "Identifier"): 

20 """Check the equality of identifiers.""" 

21 return self._id == other._id 

22 

23 def __hash__(self): 

24 """Create a hash for an identifier.""" 

25 return hash(self._id) 

26 

27 @abstractmethod 

28 def is_empty(self) -> bool: 

29 """Return true when the identifier is not set.""" 

30 raise NotImplementedError 

31 

32 def __str__(self) -> str: 

33 """Return the string representation of the id.""" 

34 return str(self._id) 

35 

36 

37class IntIdentifier(Identifier[int]): 

38 """Class that implements an identifier with an integer.""" 

39 

40 def __init__(self, id_: int = 0): 

41 super().__init__(id_) 

42 

43 def is_empty(self) -> bool: 

44 """Return True when the id equals 0.""" 

45 return self.value == 0 

46 

47 def __repr__(self): 

48 """Return a string representation of the identifier.""" 

49 return f"IntIdentifier={self.value}"