Skip to content

Commit

Permalink
fix(pagination): use correct type hint for .object (#943)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Dec 7, 2023
1 parent 8e5a156 commit 23fe7ee
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/openai/pagination.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# File generated from our OpenAPI spec by Stainless.

from typing import Any, List, Generic, Optional, cast
from typing_extensions import Literal, Protocol, override, runtime_checkable
from typing_extensions import Protocol, override, runtime_checkable

from ._types import ModelT
from ._base_client import BasePage, PageInfo, BaseSyncPage, BaseAsyncPage
Expand All @@ -11,18 +11,21 @@

@runtime_checkable
class CursorPageItem(Protocol):
id: str
id: Optional[str]


class SyncPage(BaseSyncPage[ModelT], BasePage[ModelT], Generic[ModelT]):
"""Note: no pagination actually occurs yet, this is for forwards-compatibility."""

data: List[ModelT]
object: Literal["list"]
object: str

@override
def _get_page_items(self) -> List[ModelT]:
return self.data
data = self.data
if not data:
return []
return data

@override
def next_page_info(self) -> None:
Expand All @@ -37,11 +40,14 @@ class AsyncPage(BaseAsyncPage[ModelT], BasePage[ModelT], Generic[ModelT]):
"""Note: no pagination actually occurs yet, this is for forwards-compatibility."""

data: List[ModelT]
object: Literal["list"]
object: str

@override
def _get_page_items(self) -> List[ModelT]:
return self.data
data = self.data
if not data:
return []
return data

@override
def next_page_info(self) -> None:
Expand All @@ -57,15 +63,19 @@ class SyncCursorPage(BaseSyncPage[ModelT], BasePage[ModelT], Generic[ModelT]):

@override
def _get_page_items(self) -> List[ModelT]:
return self.data
data = self.data
if not data:
return []
return data

@override
def next_page_info(self) -> Optional[PageInfo]:
if not self.data:
data = self.data
if not data:
return None

item = cast(Any, self.data[-1])
if not isinstance(item, CursorPageItem):
item = cast(Any, data[-1])
if not isinstance(item, CursorPageItem) or item.id is None:
# TODO emit warning log
return None

Expand All @@ -77,15 +87,19 @@ class AsyncCursorPage(BaseAsyncPage[ModelT], BasePage[ModelT], Generic[ModelT]):

@override
def _get_page_items(self) -> List[ModelT]:
return self.data
data = self.data
if not data:
return []
return data

@override
def next_page_info(self) -> Optional[PageInfo]:
if not self.data:
data = self.data
if not data:
return None

item = cast(Any, self.data[-1])
if not isinstance(item, CursorPageItem):
item = cast(Any, data[-1])
if not isinstance(item, CursorPageItem) or item.id is None:
# TODO emit warning log
return None

Expand Down

0 comments on commit 23fe7ee

Please sign in to comment.