Make changes to the default limit in client.get_list methods

This commit is contained in:
Lonami Exo 2021-09-17 20:04:57 +02:00
parent e9f9994f4a
commit be3ed894c6
4 changed files with 49 additions and 12 deletions

View File

@ -80,6 +80,31 @@ The removed methods are:
The only exception to this rule is ``iter_download``. The only exception to this rule is ``iter_download``.
Additionally, when using ``await``, if the method was called with a limit of 1 (either through
setting just one value to fetch, or setting the limit to one), either ``None`` or a single item
(outside of a ``list``) will be returned. This used to be the case only for ``get_messages``,
but now all methods behave in the same way for consistency.
When using ``async for``, the default limit will be ``None``, meaning all items will be fetched.
When using ``await``, the default limit will be ``1``, meaning the latest item will be fetched.
If you want to use ``await`` but still get a list, use the ``.collect()`` method to collect the
results into a list:
.. code-block:: python
chat = ...
# will iterate over all (default limit=None)
async for message in client.get_messages(chat):
...
# will return either a single Message or None if there is not any (limit=1)
message = await client.get_messages(chat)
# will collect all messages into a list (default limit=None). will also take long!
all_messages = await client.get_messages(chat).collect()
// TODO keep providing the old ``iter_`` versions? it doesn't really hurt, even if the recommended way changed // TODO keep providing the old ``iter_`` versions? it doesn't really hurt, even if the recommended way changed
// TODO does the download really need to be special? get download is kind of weird though // TODO does the download really need to be special? get download is kind of weird though

View File

@ -167,11 +167,13 @@ def get_drafts(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntitiesLike' = None entity: 'hints.EntitiesLike' = None
) -> _DraftsIter: ) -> _DraftsIter:
if entity and not utils.is_list_like(entity): limit = None
if entity:
if not utils.is_list_like(entity):
entity = (entity,) entity = (entity,)
limit = len(entity)
# TODO Passing a limit here makes no sense return _DraftsIter(self, limit, entities=entity)
return _DraftsIter(self, None, entities=entity)
async def edit_folder( async def edit_folder(

View File

@ -719,7 +719,7 @@ class TelegramClient:
def get_participants( def get_participants(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
limit: float = None, limit: float = (),
*, *,
search: str = '', search: str = '',
filter: '_tl.TypeChannelParticipantsFilter' = None, filter: '_tl.TypeChannelParticipantsFilter' = None,
@ -794,7 +794,7 @@ class TelegramClient:
def get_admin_log( def get_admin_log(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
limit: float = None, limit: float = (),
*, *,
max_id: int = 0, max_id: int = 0,
min_id: int = 0, min_id: int = 0,
@ -925,7 +925,7 @@ class TelegramClient:
def get_profile_photos( def get_profile_photos(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
limit: int = None, limit: int = (),
*, *,
offset: int = 0, offset: int = 0,
max_id: int = 0) -> chats._ProfilePhotoIter: max_id: int = 0) -> chats._ProfilePhotoIter:
@ -1396,7 +1396,7 @@ class TelegramClient:
def get_dialogs( def get_dialogs(
self: 'TelegramClient', self: 'TelegramClient',
limit: float = None, limit: float = (),
*, *,
offset_date: 'hints.DateLike' = None, offset_date: 'hints.DateLike' = None,
offset_id: int = 0, offset_id: int = 0,
@ -1963,7 +1963,7 @@ class TelegramClient:
def get_messages( def get_messages(
self: 'TelegramClient', self: 'TelegramClient',
entity: 'hints.EntityLike', entity: 'hints.EntityLike',
limit: float = None, limit: float = (),
*, *,
offset_date: 'hints.DateLike' = None, offset_date: 'hints.DateLike' = None,
offset_id: int = 0, offset_id: int = 0,

View File

@ -28,12 +28,13 @@ class RequestIter(abc.ABC):
self.reverse = reverse self.reverse = reverse
self.wait_time = wait_time self.wait_time = wait_time
self.kwargs = kwargs self.kwargs = kwargs
self.limit = max(float('inf') if limit is None else limit, 0) self.limit = max(float('inf') if limit is None or limit == () else limit, 0)
self.left = self.limit self.left = self.limit
self.buffer = None self.buffer = None
self.index = 0 self.index = 0
self.total = None self.total = None
self.last_load = 0 self.last_load = 0
self.return_single = limit == 1 or limit == ()
async def _init(self, **kwargs): async def _init(self, **kwargs):
""" """
@ -86,11 +87,20 @@ class RequestIter(abc.ABC):
self.left = self.limit self.left = self.limit
return self return self
async def collect(self): async def collect(self, force_list=True):
""" """
Create a `self` iterator and collect it into a `TotalList` Create a `self` iterator and collect it into a `TotalList`
(a normal list with a `.total` attribute). (a normal list with a `.total` attribute).
If ``force_list`` is ``False`` and ``self.return_single`` is ``True``, no list
will be returned. Instead, either a single item or ``None`` will be returned.
""" """
if not force_list and self.return_single:
self.limit = 1
async for message in self:
return message
return None
result = helpers.TotalList() result = helpers.TotalList()
async for message in self: async for message in self:
result.append(message) result.append(message)
@ -116,4 +126,4 @@ class RequestIter(abc.ABC):
return self # __aiter__ will be called after, too return self # __aiter__ will be called after, too
def __await__(self): def __await__(self):
return self.collect().__await__() return self.collect(force_list=False).__await__()