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(pagination): use correct type hint for .object #943

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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