Skip to content

Commit

Permalink
Remove transparent mapping
Browse files Browse the repository at this point in the history
Authored-by: davfsa <[email protected]>
  • Loading branch information
A5rocks committed Aug 5, 2021
1 parent feedf4c commit 45b8e22
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions hikari/internal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,6 @@
"""Type-hint for mapping values."""


@attr.define()
class _DataTMapping(typing.Mapping[KeyT, ValueT], typing.Generic[KeyT, ValueT, DataT]):
inner: typing.Mapping[KeyT, DataT]
builder: typing.Callable[[DataT], ValueT]

def __getitem__(self, key: KeyT) -> ValueT:
return self.builder(self.inner[key])

def __iter__(self) -> typing.Iterator[KeyT]:
return iter(self.inner)

def __len__(self) -> int:
return len(self.inner)


class CacheMappingView(cache.CacheView[KeyT, ValueT]):
"""A cache mapping view implementation used for representing cached data.
Expand All @@ -111,12 +96,8 @@ class CacheMappingView(cache.CacheView[KeyT, ValueT]):
mapping. This is used to cover the case when items stores `DataT` objects.
"""

__slots__: typing.Sequence[str] = ("_data", "_had_builder")
__slots__: typing.Sequence[str] = ("_data", "_builder")

_data: typing.Mapping[KeyT, ValueT]

# _had_builder is an optimization to decrease copy-s.
_had_builder: bool

@typing.overload
def __init__(
Expand All @@ -140,15 +121,8 @@ def __init__(
*,
builder: typing.Optional[typing.Callable[[DataT], ValueT]] = None,
) -> None:
# this should probably use singledispatch at some point
if builder:
# items is a typing.Mapping[KeyT, DataT]
self._data = _DataTMapping[KeyT, ValueT, DataT](items, builder) # type: ignore[arg-type]
self._had_builder = True
else:
# items is a typing.Mapping[KeyT, ValueT]
self._data = items # type: ignore[assignment]
self._had_builder = False
self._builder = builder
self._data = items

@staticmethod
def _copy(value: ValueT) -> ValueT:
Expand All @@ -160,10 +134,10 @@ def __contains__(self, key: typing.Any) -> bool:
def __getitem__(self, key: KeyT) -> ValueT:
entry = self._data[key]

if self._had_builder:
return entry
if self._builder:
return self._builder(entry) # type: ignore[arg-type]
else:
return self._copy(entry)
return self._copy(entry) # type: ignore[arg-type]

def __iter__(self) -> typing.Iterator[KeyT]:
return iter(self._data)
Expand Down

0 comments on commit 45b8e22

Please sign in to comment.