Skip to content

Commit b2e11e7

Browse files
author
Matt Sokoloff
committed
mypy pagination
1 parent dfd6f3a commit b2e11e7

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

labelbox/pagination.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Size of a single page in a paginated query.
2+
from abc import ABC, abstractmethod
3+
from labelbox.orm.db_object import DbObject
4+
from typing import Any, Dict, List, Optional
5+
from labelbox import Client
6+
27
_PAGE_SIZE = 100
38

49

@@ -12,13 +17,13 @@ class PaginatedCollection:
1217
"""
1318

1419
def __init__(self,
15-
client,
16-
query,
17-
params,
18-
dereferencing,
19-
obj_class,
20-
cursor_path=None,
21-
beta=False):
20+
client: Client,
21+
query: str,
22+
params: Dict[str, str],
23+
dereferencing: Dict[str, Any],
24+
obj_class: DbObject,
25+
cursor_path: Optional[Dict[str, Any]] = None,
26+
beta: bool = False):
2227
""" Creates a PaginatedCollection.
2328
2429
Args:
@@ -43,13 +48,10 @@ def __init__(self,
4348
self.beta = beta
4449

4550
self._fetched_all = False
46-
self._data = []
51+
self._data: List[Dict[str, Any]] = []
4752
self._data_ind = 0
48-
49-
if cursor_path:
50-
self.paginator = _CursorPagination(client, cursor_path)
51-
else:
52-
self.paginator = _OffsetPagination(client)
53+
self.cursor_path = _CursorPagination(
54+
client, cursor_path) if cursor_path else _OffsetPagination(client)
5355

5456
def __iter__(self):
5557
self._data_ind = 0
@@ -81,39 +83,53 @@ def __next__(self):
8183
return rval
8284

8385

84-
class _CursorPagination:
86+
class _Pagination(ABC):
87+
88+
@abstractmethod
89+
def fetched_all(self, n_items: int, results: List[Dict[str, Any]]) -> bool:
90+
...
91+
92+
@abstractmethod
93+
def fetch_results(self, query: str, params: Dict[str, Any],
94+
beta: bool) -> Dict[str, Any]:
95+
...
96+
97+
98+
class _CursorPagination(_Pagination):
8599

86-
def __init__(self, client, cursor_path):
100+
def __init__(self, client: Client, cursor_path: Dict[str, Any]):
87101
self.client = client
88102
self.cursor_path = cursor_path
89-
self.next_cursor = None
103+
self.next_cursor: Optional[str] = None
90104

91-
def get_next_cursor(self, results):
105+
def get_next_cursor(self, results) -> Optional[str]:
92106
for path in self.cursor_path:
93107
results = results[path]
94108
return results
95109

96-
def fetched_all(self, n_items, results):
110+
def fetched_all(self, n_items: int, results: List[Dict[str, Any]]) -> bool:
97111
self.next_cursor = self.get_next_cursor(results)
98-
return self.next_cursor is None
112+
return bool(self.next_cursor is None)
99113

100-
def fetch_results(self, query, params, beta):
114+
def fetch_results(self, query: str, params: Dict[str, Any],
115+
beta: bool) -> Dict[str, Any]:
101116
params.update({'from': self.next_cursor, 'first': _PAGE_SIZE})
102117
return self.client.execute(query, params, beta=beta)
103118

104119

105-
class _OffsetPagination:
120+
class _OffsetPagination(_Pagination):
106121

107122
def __init__(self, client):
108123
self.client = client
109124
self._fetched_pages = 0
110125

111-
def fetched_all(self, n_items, results):
126+
def fetched_all(self, n_items: int, results: List[Dict[str, Any]]) -> bool:
112127
self._fetched_pages += 1
113128
if n_items < _PAGE_SIZE:
114129
return True
115130
return False
116131

117-
def fetch_results(self, query, params, beta):
132+
def fetch_results(self, query: str, params: Dict[str, Any],
133+
beta: bool) -> Dict[str, Any]:
118134
query = query % (self._fetched_pages * _PAGE_SIZE, _PAGE_SIZE)
119135
return self.client.execute(query, params, beta=beta)

0 commit comments

Comments
 (0)