Skip to content

Commit

Permalink
Make RangeMap keys a TypeVar bound to _SupportsComparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Jul 27, 2024
1 parent 1e3d156 commit 1107201
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions jaraco/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
import jaraco.text

if TYPE_CHECKING:
from _operator import _SupportsComparison

from _typeshed import SupportsKeysAndGetItem
from typing_extensions import Self

_T = TypeVar("_T")
_RangeMapKT = TypeVar("_RangeMapKT", bound=_SupportsComparison)
_VT = TypeVar("_VT")

_Matchable = Union[Callable, Container, Iterable, re.Pattern]
Expand Down Expand Up @@ -126,7 +129,7 @@ def dict_map(function, dictionary):
return dict((key, function(value)) for key, value in dictionary.items())


class RangeMap(Dict[int, _VT]):
class RangeMap(Dict[_RangeMapKT, _VT]):
"""
A dictionary-like object that uses the keys as bounds for a range.
Inclusion of the value for that range is determined by the
Expand Down Expand Up @@ -209,23 +212,28 @@ class RangeMap(Dict[int, _VT]):

def __init__(
self,
source: SupportsKeysAndGetItem[int, _VT] | Iterable[tuple[int, _VT]],
source: (
SupportsKeysAndGetItem[_RangeMapKT, _VT] | Iterable[tuple[_RangeMapKT, _VT]]
),
sort_params: Mapping[str, Any] = {},
key_match_comparator: Callable[[int, int], bool] = operator.le,
key_match_comparator: Callable[[_RangeMapKT, _RangeMapKT], bool] = operator.le,
):
dict.__init__(self, source)
self.sort_params = sort_params
self.match = key_match_comparator

@classmethod
def left(
cls, source: SupportsKeysAndGetItem[int, _VT] | Iterable[tuple[int, _VT]]
cls,
source: (
SupportsKeysAndGetItem[_RangeMapKT, _VT] | Iterable[tuple[_RangeMapKT, _VT]]
),
) -> Self:
return cls(
source, sort_params=dict(reverse=True), key_match_comparator=operator.ge
)

def __getitem__(self, item: int) -> _VT:
def __getitem__(self, item: _RangeMapKT) -> _VT:
sorted_keys = sorted(self.keys(), **self.sort_params)
if isinstance(item, RangeMap.Item):
result = self.__getitem__(sorted_keys[item])
Expand All @@ -237,10 +245,10 @@ def __getitem__(self, item: int) -> _VT:
return result

@overload # type: ignore[override] # Signature simplified over dict and Mapping
def get(self, key: int, default: _T) -> _VT | _T: ...
def get(self, key: _RangeMapKT, default: _T) -> _VT | _T: ...
@overload
def get(self, key: int, default: None = None) -> _VT | None: ...
def get(self, key: int, default: _T | None = None) -> _VT | _T | None:
def get(self, key: _RangeMapKT, default: None = None) -> _VT | None: ...
def get(self, key: _RangeMapKT, default: _T | None = None) -> _VT | _T | None:
"""
Return the value for key if key is in the dictionary, else default.
If default is not given, it defaults to None, so that this method
Expand All @@ -251,15 +259,17 @@ def get(self, key: int, default: _T | None = None) -> _VT | _T | None:
except KeyError:
return default

def _find_first_match_(self, keys: Iterable[int], item: int) -> int:
def _find_first_match_(
self, keys: Iterable[_RangeMapKT], item: _RangeMapKT
) -> _RangeMapKT:
is_match = functools.partial(self.match, item)
matches = filter(is_match, keys)
try:
return next(matches)
except StopIteration:
raise KeyError(item) from None

def bounds(self) -> tuple[int, int]:
def bounds(self) -> tuple[_RangeMapKT, _RangeMapKT]:
sorted_keys = sorted(self.keys(), **self.sort_params)
return (sorted_keys[RangeMap.first_item], sorted_keys[RangeMap.last_item])

Expand Down

0 comments on commit 1107201

Please sign in to comment.