Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate CacheView.iterator in favour of using itertools #1289

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1289.deprecation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate `CacheView.iterator` in favour of using the `itertools` module.
6 changes: 0 additions & 6 deletions hikari/api/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import abc
import typing

from hikari import iterators

if typing.TYPE_CHECKING:
from hikari import channels
from hikari import emojis
Expand Down Expand Up @@ -69,10 +67,6 @@ def get_item_at(self, index: slice, /) -> typing.Sequence[_ValueT]:
def get_item_at(self, index: typing.Union[slice, int], /) -> typing.Union[_ValueT, typing.Sequence[_ValueT]]:
...

@abc.abstractmethod
def iterator(self) -> iterators.LazyIterator[_ValueT]:
"""Get a lazy iterator of the entities in the view."""


class Cache(abc.ABC):
"""Interface describing the operations a cache component should provide.
Expand Down
11 changes: 11 additions & 0 deletions hikari/internal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from hikari.api import cache
from hikari.internal import attr_extensions
from hikari.internal import collections
from hikari.internal import deprecation

if typing.TYPE_CHECKING:
from hikari import applications
Expand Down Expand Up @@ -156,6 +157,11 @@ def get_item_at(self, index: typing.Union[slice, int], /) -> typing.Union[ValueT
return collections.get_index_or_slice(self, index)

def iterator(self) -> iterators.LazyIterator[ValueT]:
deprecation.warn_deprecated(
"iterator",
removal_version="2.0.0.dev113",
additional_info="Use the 'itertools' module instead",
)
return iterators.FlatLazyIterator(self.values())


Expand All @@ -180,6 +186,11 @@ def get_item_at(self, index: typing.Union[slice, int]) -> typing.NoReturn:
raise IndexError(index)

def iterator(self) -> iterators.LazyIterator[ValueT]:
deprecation.warn_deprecated(
"iterator",
removal_version="2.0.0.dev113",
additional_info="Use the 'itertools' module instead",
)
return iterators.FlatLazyIterator(())


Expand Down
27 changes: 27 additions & 0 deletions tests/hikari/internal/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import copy

import mock
import pytest

from hikari import iterators
from hikari import snowflakes
from hikari import stickers
from hikari.internal import cache
Expand Down Expand Up @@ -73,3 +75,28 @@ def test_from_entity_when_user_not_passed(self) -> None:
assert data.user is refcell.return_value
mock_copy.assert_called_once_with(mock_user)
refcell.assert_called_once_with(mock_copy.return_value)


class TestCacheMappingView:
@pytest.fixture()
def cache_view(self):
return cache.CacheMappingView({"some": "data"})

def test_iterator(self, cache_view):
with mock.patch.object(iterators, "FlatLazyIterator") as flat_lazy_iterator:
with mock.patch.object(cache.CacheMappingView, "values") as values:
assert cache_view.iterator() is flat_lazy_iterator.return_value

flat_lazy_iterator.assert_called_once_with(values.return_value)


class TestEmptyCacheView:
@pytest.fixture()
def cache_view(self):
return cache.EmptyCacheView()

def test_iterator(self, cache_view):
with mock.patch.object(iterators, "FlatLazyIterator") as flat_lazy_iterator:
assert cache_view.iterator() is flat_lazy_iterator.return_value

flat_lazy_iterator.assert_called_once_with(())