Fix incorrect `Response` type for `AsyncPaginator` and revert formatting from `black`
authorLanqing Huang <lqhuang@outlook.com>
Mon, 24 Oct 2022 07:54:11 +0000 (15:54 +0800)
committerLanqing Huang <lqhuang@outlook.com>
Mon, 24 Oct 2022 07:54:11 +0000 (15:54 +0800)
tweepy/asynchronous/pagination.py
tweepy/pagination.py

index d394fd9a638032c177072bee10dceea7b2c2c52c..59d5596b5f45ecd11648df4536dd59196d587b00 100644 (file)
@@ -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}"
index 4c3f6b8d54b5d8a07b542d69f8188d0cbc3fb6e8..fd4d95e746ad369aa60b3ded0421a7972ba2d999 100644 (file)
@@ -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: