From aedd145e2f0bca47d88f0f20e8926d84dda5ab0e Mon Sep 17 00:00:00 2001 From: Lanqing Huang Date: Wed, 13 Apr 2022 16:06:01 +0800 Subject: [PATCH] Fix `AttributeError` by accessing `meta` field when return_type is dict --- tweepy/pagination.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tweepy/pagination.py b/tweepy/pagination.py index f61c192..1aea5a7 100644 --- a/tweepy/pagination.py +++ b/tweepy/pagination.py @@ -2,8 +2,13 @@ # Copyright 2009-2022 Joshua Roesslein # See LICENSE for details. +from typing import Union from math import inf +import requests + +from tweepy.client import Response + class Paginator: """:class:`Paginator` can be used to paginate for any :class:`Client` @@ -77,7 +82,7 @@ class PaginationIterator: def __iter__(self): return self - def __next__(self): + def __next__(self) -> Union[Response, dict, requests.Response]: if self.reverse: pagination_token = self.previous_token else: @@ -97,8 +102,17 @@ class PaginationIterator: 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", {}) + else: + raise NotImplementedError("Unknown `response` type to parse `meta` field") + + self.previous_token = meta.get("previous_token") + self.next_token = meta.get("next_token") self.count += 1 return response -- 2.25.1