Skip to content

Commit 440f9ba

Browse files
chore(internal): minor pagination restructuring (#287)
1 parent 0e6398c commit 440f9ba

File tree

1 file changed

+72
-30
lines changed

1 file changed

+72
-30
lines changed

src/finch/pagination.py

+72-30
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class SyncSinglePage(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
3131

3232
@override
3333
def _get_page_items(self) -> List[_T]:
34-
return self.items
34+
items = self.items
35+
if not items:
36+
return []
37+
return items
3538

3639
@override
3740
def next_page_info(self) -> None:
@@ -56,7 +59,10 @@ class AsyncSinglePage(BaseAsyncPage[_T], BasePage[_T], Generic[_T]):
5659

5760
@override
5861
def _get_page_items(self) -> List[_T]:
59-
return self.items
62+
items = self.items
63+
if not items:
64+
return []
65+
return items
6066

6167
@override
6268
def next_page_info(self) -> None:
@@ -81,7 +87,10 @@ class SyncResponsesPage(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
8187

8288
@override
8389
def _get_page_items(self) -> List[_T]:
84-
return self.responses
90+
responses = self.responses
91+
if not responses:
92+
return []
93+
return responses
8594

8695
@override
8796
def next_page_info(self) -> None:
@@ -97,7 +106,10 @@ class AsyncResponsesPage(BaseAsyncPage[_T], BasePage[_T], Generic[_T]):
97106

98107
@override
99108
def _get_page_items(self) -> List[_T]:
100-
return self.responses
109+
responses = self.responses
110+
if not responses:
111+
return []
112+
return responses
101113

102114
@override
103115
def next_page_info(self) -> None:
@@ -110,107 +122,137 @@ def next_page_info(self) -> None:
110122

111123
class SyncIndividualsPage(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
112124
individuals: List[_T]
125+
"""The array of employees."""
113126
paging: Paging
114127

115128
@override
116129
def _get_page_items(self) -> List[_T]:
117-
return self.individuals
130+
individuals = self.individuals
131+
if not individuals:
132+
return []
133+
return individuals
118134

119135
@override
120136
def next_page_info(self) -> Optional[PageInfo]:
121-
offset = self.paging.offset
137+
offset = None
138+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
139+
offset = self.paging.offset
122140
if offset is None:
123141
return None
124142

125-
length = len(self.individuals)
143+
length = len(self._get_page_items())
126144
current_count = offset + length
127145

128-
total_count = self.paging.count
129-
if total_count is None:
146+
count = None
147+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
148+
count = self.paging.count
149+
if count is None:
130150
return None
131151

132-
if current_count < total_count:
152+
if current_count < count:
133153
return PageInfo(params={"offset": current_count})
134154

135155
return None
136156

137157

138158
class AsyncIndividualsPage(BaseAsyncPage[_T], BasePage[_T], Generic[_T]):
139159
individuals: List[_T]
160+
"""The array of employees."""
140161
paging: Paging
141162

142163
@override
143164
def _get_page_items(self) -> List[_T]:
144-
return self.individuals
165+
individuals = self.individuals
166+
if not individuals:
167+
return []
168+
return individuals
145169

146170
@override
147171
def next_page_info(self) -> Optional[PageInfo]:
148-
offset = self.paging.offset
172+
offset = None
173+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
174+
offset = self.paging.offset
149175
if offset is None:
150176
return None
151177

152-
length = len(self.individuals)
178+
length = len(self._get_page_items())
153179
current_count = offset + length
154180

155-
total_count = self.paging.count
156-
if total_count is None:
181+
count = None
182+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
183+
count = self.paging.count
184+
if count is None:
157185
return None
158186

159-
if current_count < total_count:
187+
if current_count < count:
160188
return PageInfo(params={"offset": current_count})
161189

162190
return None
163191

164192

165193
class SyncPage(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
166-
paging: Paging
167194
data: List[_T]
195+
paging: Paging
168196

169197
@override
170198
def _get_page_items(self) -> List[_T]:
171-
return self.data
199+
data = self.data
200+
if not data:
201+
return []
202+
return data
172203

173204
@override
174205
def next_page_info(self) -> Optional[PageInfo]:
175-
offset = self.paging.offset
206+
offset = None
207+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
208+
offset = self.paging.offset
176209
if offset is None:
177210
return None
178211

179-
length = len(self.data)
212+
length = len(self._get_page_items())
180213
current_count = offset + length
181214

182-
total_count = self.paging.count
183-
if total_count is None:
215+
count = None
216+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
217+
count = self.paging.count
218+
if count is None:
184219
return None
185220

186-
if current_count < total_count:
221+
if current_count < count:
187222
return PageInfo(params={"offset": current_count})
188223

189224
return None
190225

191226

192227
class AsyncPage(BaseAsyncPage[_T], BasePage[_T], Generic[_T]):
193-
paging: Paging
194228
data: List[_T]
229+
paging: Paging
195230

196231
@override
197232
def _get_page_items(self) -> List[_T]:
198-
return self.data
233+
data = self.data
234+
if not data:
235+
return []
236+
return data
199237

200238
@override
201239
def next_page_info(self) -> Optional[PageInfo]:
202-
offset = self.paging.offset
240+
offset = None
241+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
242+
offset = self.paging.offset
203243
if offset is None:
204244
return None
205245

206-
length = len(self.data)
246+
length = len(self._get_page_items())
207247
current_count = offset + length
208248

209-
total_count = self.paging.count
210-
if total_count is None:
249+
count = None
250+
if self.paging is not None: # pyright: ignore[reportUnnecessaryComparison]
251+
count = self.paging.count
252+
if count is None:
211253
return None
212254

213-
if current_count < total_count:
255+
if current_count < count:
214256
return PageInfo(params={"offset": current_count})
215257

216258
return None

0 commit comments

Comments
 (0)