Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_internal/utils/models.py: 52%

23 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 06:33 +0000

1"""Utilities for defining models 

2""" 

3 

4import operator 

5from typing import Any, Callable, Type 

6 

7 

8class KeyBasedCompareMixin: 

9 """Provides comparison capabilities that is based on a key""" 

10 

11 __slots__ = ["_compare_key", "_defining_class"] 

12 

13 def __init__(self, key: Any, defining_class: Type["KeyBasedCompareMixin"]) -> None: 

14 self._compare_key = key 

15 self._defining_class = defining_class 

16 

17 def __hash__(self) -> int: 

18 return hash(self._compare_key) 

19 

20 def __lt__(self, other: Any) -> bool: 

21 return self._compare(other, operator.__lt__) 

22 

23 def __le__(self, other: Any) -> bool: 

24 return self._compare(other, operator.__le__) 

25 

26 def __gt__(self, other: Any) -> bool: 

27 return self._compare(other, operator.__gt__) 

28 

29 def __ge__(self, other: Any) -> bool: 

30 return self._compare(other, operator.__ge__) 

31 

32 def __eq__(self, other: Any) -> bool: 

33 return self._compare(other, operator.__eq__) 

34 

35 def _compare(self, other: Any, method: Callable[[Any, Any], bool]) -> bool: 

36 if not isinstance(other, self._defining_class): 

37 return NotImplemented 

38 

39 return method(self._compare_key, other._compare_key)