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

Fix AttributeError by accessing meta field when return_type is dict #1861

Merged
merged 5 commits into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions tweepy/asynchronous/pagination.py
Expand Up @@ -4,13 +4,24 @@

from math import inf

import aiohttp

from tweepy.client import Response


class AsyncPaginator:
""":class:`AsyncPaginator` can be used to paginate for any
:class:`AsyncClient` methods that support pagination

.. versionadded:: 4.11

.. note::

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.
lqhuang marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
method
Expand Down Expand Up @@ -101,8 +112,19 @@ async def __anext__(self):

response = await self.method(*self.args, **self.kwargs)

self.previous_token = response.meta.get("previous_token")
self.next_token = response.meta.get("next_token")
if isinstance(response, Response):
meta = response.meta
elif isinstance(response, dict):
meta = response.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}"
)

self.previous_token = meta.get("previous_token")
self.next_token = meta.get("next_token")
self.count += 1

return response
26 changes: 24 additions & 2 deletions tweepy/pagination.py
Expand Up @@ -4,13 +4,24 @@

from math import inf

import requests

from tweepy.client import Response


class Paginator:
""":class:`Paginator` can be used to paginate for any :class:`Client`
methods that support pagination

.. versionadded:: 4.0

.. note::

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.
lqhuang marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
method
Expand Down Expand Up @@ -97,8 +108,19 @@ def __next__(self):

response = self.method(*self.args, **self.kwargs)

self.previous_token = response.meta.get("previous_token")
self.next_token = response.meta.get("next_token")
if isinstance(response, Response):
meta = response.meta
elif isinstance(response, dict):
meta = response.get("meta", {})
elif isinstance(response, requests.Response):
meta = response.json().get("meta", {})
lqhuang marked this conversation as resolved.
Show resolved Hide resolved
else:
raise NotImplementedError(
f"Unknown {type(response)} return type for {self.method}"
)

self.previous_token = meta.get("previous_token")
self.next_token = meta.get("next_token")
self.count += 1

return response