From 7879f6fcc43b985e292a2a7f098df00890b8f152 Mon Sep 17 00:00:00 2001 From: Lanqing Huang Date: Mon, 24 Oct 2022 15:54:11 +0800 Subject: [PATCH] Fix incorrect `Response` type for `AsyncPaginator` and revert formatting from `black` --- tweepy/asynchronous/pagination.py | 22 ++++++++++++---------- tweepy/pagination.py | 24 +++++++++++++----------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/tweepy/asynchronous/pagination.py b/tweepy/asynchronous/pagination.py index d394fd9..59d5596 100644 --- a/tweepy/asynchronous/pagination.py +++ b/tweepy/asynchronous/pagination.py @@ -4,7 +4,7 @@ from math import inf -import requests +import aiohttp from tweepy.client import Response @@ -17,9 +17,10 @@ class AsyncPaginator: .. note:: - When passing ``return_type=requests.Response`` to :class:`Client` for - pagination, payload of response will be deserialized implicitly to get - ``meta`` attribute every requests, which may affect performance. + When the returned response from the method being passed is of type + :class:`aiohttp.ClientResponse`, it will be deserialized in order to parse + the pagination tokens, likely negating any potential performance + benefits from using a :class:`aiohttp.ClientResponse` return type. Parameters ---------- @@ -68,8 +69,10 @@ class AsyncPaginator: class AsyncPaginationIterator: + def __init__( - self, method, *args, limit=inf, pagination_token=None, reverse=False, **kwargs + self, method, *args, limit=inf, pagination_token=None, reverse=False, + **kwargs ): self.method = method self.args = args @@ -100,9 +103,8 @@ class AsyncPaginationIterator: # https://twittercommunity.com/t/why-does-timeline-use-pagination-token-while-search-uses-next-token/150963 if self.method.__name__ in ( - "search_all_tweets", - "search_recent_tweets", - "get_all_tweets_count", + "search_all_tweets", "search_recent_tweets", + "get_all_tweets_count" ): self.kwargs["next_token"] = pagination_token else: @@ -114,8 +116,8 @@ class AsyncPaginationIterator: meta = response.meta elif isinstance(response, dict): meta = response.get("meta", {}) - elif isinstance(response, requests.Response): - meta = response.json().get("meta", {}) + elif isinstance(response, aiohttp.ClientResponse): + meta = (await response.json()).get("meta", {}) else: raise NotImplementedError( f"Unknown {type(response)} return type for {self.method}" diff --git a/tweepy/pagination.py b/tweepy/pagination.py index 4c3f6b8..fd4d95e 100644 --- a/tweepy/pagination.py +++ b/tweepy/pagination.py @@ -17,9 +17,10 @@ class Paginator: .. note:: - When passing ``return_type=requests.Response`` to :class:`Client` for - pagination, payload of response will be deserialized implicitly to get - ``meta`` attribute every requests, which may affect performance. + When the returned response from the method being passed is of type + :class:`requests.Response`, it will be deserialized in order to parse + the pagination tokens, likely negating any potential performance + benefits from using a :class:`requests.Response` return type. Parameters ---------- @@ -40,7 +41,8 @@ class Paginator: return PaginationIterator(self.method, *self.args, **self.kwargs) def __reversed__(self): - return PaginationIterator(self.method, *self.args, reverse=True, **self.kwargs) + return PaginationIterator(self.method, *self.args, reverse=True, + **self.kwargs) def flatten(self, limit=inf): """Flatten paginated data @@ -54,7 +56,8 @@ class Paginator: return count = 0 - for response in PaginationIterator(self.method, *self.args, **self.kwargs): + for response in PaginationIterator(self.method, *self.args, + **self.kwargs): if response.data is not None: for data in response.data: yield data @@ -64,9 +67,9 @@ class Paginator: class PaginationIterator: - def __init__( - self, method, *args, limit=inf, pagination_token=None, reverse=False, **kwargs - ): + + def __init__(self, method, *args, limit=inf, pagination_token=None, + reverse=False, **kwargs): self.method = method self.args = args self.limit = limit @@ -96,9 +99,8 @@ class PaginationIterator: # https://twittercommunity.com/t/why-does-timeline-use-pagination-token-while-search-uses-next-token/150963 if self.method.__name__ in ( - "search_all_tweets", - "search_recent_tweets", - "get_all_tweets_count", + "search_all_tweets", "search_recent_tweets", + "get_all_tweets_count" ): self.kwargs["next_token"] = pagination_token else: -- 2.25.1