Skip to content

Commit

Permalink
Handle different method return types in flatten methods for pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Oct 27, 2022
1 parent e31be15 commit 381bf91
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
21 changes: 15 additions & 6 deletions tweepy/asynchronous/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ async def flatten(self, limit=inf):
async for response in AsyncPaginationIterator(
self.method, *self.args, **self.kwargs
):
if response.data is not None:
for data in response.data:
yield data
count += 1
if count == limit:
return
if isinstance(response, Response):
response_data = response.data or []
elif isinstance(response, dict):
response_data = response.get("data", [])
else:
raise RuntimeError(
"AsyncPaginator.flatten does not support the "
f"{type(response)} return type for "
f"{self.method.__qualname__}"
)
for data in response_data:
yield data
count += 1
if count == limit:
return


class AsyncPaginationIterator:
Expand Down
25 changes: 17 additions & 8 deletions tweepy/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,23 @@ def flatten(self, limit=inf):
return

count = 0
for response in PaginationIterator(self.method, *self.args,
**self.kwargs):
if response.data is not None:
for data in response.data:
yield data
count += 1
if count == limit:
return
for response in PaginationIterator(
self.method, *self.args, **self.kwargs
):
if isinstance(response, Response):
response_data = response.data or []
elif isinstance(response, dict):
response_data = response.get("data", [])
else:
raise RuntimeError(
f"Paginator.flatten does not support the {type(response)} "
f"return type for {self.method.__qualname__}"
)
for data in response_data:
yield data
count += 1
if count == limit:
return


class PaginationIterator:
Expand Down

0 comments on commit 381bf91

Please sign in to comment.