Skip to content

Commit

Permalink
chore: use property declarations for resource members (#1047)
Browse files Browse the repository at this point in the history
This will speedup client instantiation in certain cases.
  • Loading branch information
stainless-bot committed Jan 4, 2024
1 parent d94b4d3 commit 131f6bc
Show file tree
Hide file tree
Showing 26 changed files with 285 additions and 394 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies = [
"anyio>=3.5.0, <5",
"distro>=1.7.0, <2",
"sniffio",
"cached-property; python_version < '3.8'",
"tqdm > 4"
]
requires-python = ">= 3.7.1"
Expand Down
10 changes: 10 additions & 0 deletions src/openai/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,13 @@ class GenericModel(pydantic.BaseModel):

class GenericModel(pydantic.generics.GenericModel, pydantic.BaseModel):
...


# cached properties
if TYPE_CHECKING:
cached_property = property
else:
try:
from functools import cached_property as cached_property
except ImportError:
from cached_property import cached_property as cached_property
56 changes: 30 additions & 26 deletions src/openai/resources/audio/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from .speech import Speech, AsyncSpeech, SpeechWithRawResponse, AsyncSpeechWithRawResponse
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from .translations import Translations, AsyncTranslations, TranslationsWithRawResponse, AsyncTranslationsWithRawResponse
from .transcriptions import (
Expand All @@ -14,38 +13,43 @@
AsyncTranscriptionsWithRawResponse,
)

if TYPE_CHECKING:
from ..._client import OpenAI, AsyncOpenAI

__all__ = ["Audio", "AsyncAudio"]


class Audio(SyncAPIResource):
transcriptions: Transcriptions
translations: Translations
speech: Speech
with_raw_response: AudioWithRawResponse
@cached_property
def transcriptions(self) -> Transcriptions:
return Transcriptions(self._client)

@cached_property
def translations(self) -> Translations:
return Translations(self._client)

@cached_property
def speech(self) -> Speech:
return Speech(self._client)

def __init__(self, client: OpenAI) -> None:
super().__init__(client)
self.transcriptions = Transcriptions(client)
self.translations = Translations(client)
self.speech = Speech(client)
self.with_raw_response = AudioWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AudioWithRawResponse:
return AudioWithRawResponse(self)


class AsyncAudio(AsyncAPIResource):
transcriptions: AsyncTranscriptions
translations: AsyncTranslations
speech: AsyncSpeech
with_raw_response: AsyncAudioWithRawResponse

def __init__(self, client: AsyncOpenAI) -> None:
super().__init__(client)
self.transcriptions = AsyncTranscriptions(client)
self.translations = AsyncTranslations(client)
self.speech = AsyncSpeech(client)
self.with_raw_response = AsyncAudioWithRawResponse(self)
@cached_property
def transcriptions(self) -> AsyncTranscriptions:
return AsyncTranscriptions(self._client)

@cached_property
def translations(self) -> AsyncTranslations:
return AsyncTranslations(self._client)

@cached_property
def speech(self) -> AsyncSpeech:
return AsyncSpeech(self._client)

@cached_property
def with_raw_response(self) -> AsyncAudioWithRawResponse:
return AsyncAudioWithRawResponse(self)


class AudioWithRawResponse:
Expand Down
22 changes: 8 additions & 14 deletions src/openai/resources/audio/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Union
from typing import Union
from typing_extensions import Literal

import httpx
Expand All @@ -15,6 +15,7 @@
NotGiven,
)
from ..._utils import maybe_transform
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
from ...types.audio import speech_create_params
Expand All @@ -23,18 +24,13 @@
make_request_options,
)

if TYPE_CHECKING:
from ..._client import OpenAI, AsyncOpenAI

__all__ = ["Speech", "AsyncSpeech"]


class Speech(SyncAPIResource):
with_raw_response: SpeechWithRawResponse

def __init__(self, client: OpenAI) -> None:
super().__init__(client)
self.with_raw_response = SpeechWithRawResponse(self)
@cached_property
def with_raw_response(self) -> SpeechWithRawResponse:
return SpeechWithRawResponse(self)

def create(
self,
Expand Down Expand Up @@ -99,11 +95,9 @@ def create(


class AsyncSpeech(AsyncAPIResource):
with_raw_response: AsyncSpeechWithRawResponse

def __init__(self, client: AsyncOpenAI) -> None:
super().__init__(client)
self.with_raw_response = AsyncSpeechWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AsyncSpeechWithRawResponse:
return AsyncSpeechWithRawResponse(self)

async def create(
self,
Expand Down
22 changes: 8 additions & 14 deletions src/openai/resources/audio/transcriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Union, Mapping, cast
from typing import Union, Mapping, cast
from typing_extensions import Literal

import httpx
Expand All @@ -16,25 +16,21 @@
FileTypes,
)
from ..._utils import extract_files, maybe_transform, deepcopy_minimal
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
from ...types.audio import Transcription, transcription_create_params
from ..._base_client import (
make_request_options,
)

if TYPE_CHECKING:
from ..._client import OpenAI, AsyncOpenAI

__all__ = ["Transcriptions", "AsyncTranscriptions"]


class Transcriptions(SyncAPIResource):
with_raw_response: TranscriptionsWithRawResponse

def __init__(self, client: OpenAI) -> None:
super().__init__(client)
self.with_raw_response = TranscriptionsWithRawResponse(self)
@cached_property
def with_raw_response(self) -> TranscriptionsWithRawResponse:
return TranscriptionsWithRawResponse(self)

def create(
self,
Expand Down Expand Up @@ -117,11 +113,9 @@ def create(


class AsyncTranscriptions(AsyncAPIResource):
with_raw_response: AsyncTranscriptionsWithRawResponse

def __init__(self, client: AsyncOpenAI) -> None:
super().__init__(client)
self.with_raw_response = AsyncTranscriptionsWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AsyncTranscriptionsWithRawResponse:
return AsyncTranscriptionsWithRawResponse(self)

async def create(
self,
Expand Down
22 changes: 8 additions & 14 deletions src/openai/resources/audio/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Union, Mapping, cast
from typing import Union, Mapping, cast
from typing_extensions import Literal

import httpx
Expand All @@ -16,25 +16,21 @@
FileTypes,
)
from ..._utils import extract_files, maybe_transform, deepcopy_minimal
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
from ...types.audio import Translation, translation_create_params
from ..._base_client import (
make_request_options,
)

if TYPE_CHECKING:
from ..._client import OpenAI, AsyncOpenAI

__all__ = ["Translations", "AsyncTranslations"]


class Translations(SyncAPIResource):
with_raw_response: TranslationsWithRawResponse

def __init__(self, client: OpenAI) -> None:
super().__init__(client)
self.with_raw_response = TranslationsWithRawResponse(self)
@cached_property
def with_raw_response(self) -> TranslationsWithRawResponse:
return TranslationsWithRawResponse(self)

def create(
self,
Expand Down Expand Up @@ -110,11 +106,9 @@ def create(


class AsyncTranslations(AsyncAPIResource):
with_raw_response: AsyncTranslationsWithRawResponse

def __init__(self, client: AsyncOpenAI) -> None:
super().__init__(client)
self.with_raw_response = AsyncTranslationsWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AsyncTranslationsWithRawResponse:
return AsyncTranslationsWithRawResponse(self)

async def create(
self,
Expand Down
30 changes: 14 additions & 16 deletions src/openai/resources/beta/assistants/assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional
from typing import List, Optional
from typing_extensions import Literal

import httpx
Expand All @@ -16,6 +16,7 @@
NotGiven,
)
from ...._utils import maybe_transform
from ...._compat import cached_property
from ...._resource import SyncAPIResource, AsyncAPIResource
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
from ....pagination import SyncCursorPage, AsyncCursorPage
Expand All @@ -31,20 +32,17 @@
make_request_options,
)

if TYPE_CHECKING:
from ...._client import OpenAI, AsyncOpenAI

__all__ = ["Assistants", "AsyncAssistants"]


class Assistants(SyncAPIResource):
files: Files
with_raw_response: AssistantsWithRawResponse
@cached_property
def files(self) -> Files:
return Files(self._client)

def __init__(self, client: OpenAI) -> None:
super().__init__(client)
self.files = Files(client)
self.with_raw_response = AssistantsWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AssistantsWithRawResponse:
return AssistantsWithRawResponse(self)

def create(
self,
Expand Down Expand Up @@ -331,13 +329,13 @@ def delete(


class AsyncAssistants(AsyncAPIResource):
files: AsyncFiles
with_raw_response: AsyncAssistantsWithRawResponse
@cached_property
def files(self) -> AsyncFiles:
return AsyncFiles(self._client)

def __init__(self, client: AsyncOpenAI) -> None:
super().__init__(client)
self.files = AsyncFiles(client)
self.with_raw_response = AsyncAssistantsWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AsyncAssistantsWithRawResponse:
return AsyncAssistantsWithRawResponse(self)

async def create(
self,
Expand Down
28 changes: 8 additions & 20 deletions src/openai/resources/beta/assistants/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from typing_extensions import Literal

import httpx
Expand All @@ -15,32 +14,23 @@
NotGiven,
)
from ...._utils import maybe_transform
from ...._compat import cached_property
from ...._resource import SyncAPIResource, AsyncAPIResource
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
from ....pagination import SyncCursorPage, AsyncCursorPage
from ...._base_client import (
AsyncPaginator,
make_request_options,
)
from ....types.beta.assistants import (
AssistantFile,
FileDeleteResponse,
file_list_params,
file_create_params,
)

if TYPE_CHECKING:
from ...._client import OpenAI, AsyncOpenAI
from ....types.beta.assistants import AssistantFile, FileDeleteResponse, file_list_params, file_create_params

__all__ = ["Files", "AsyncFiles"]


class Files(SyncAPIResource):
with_raw_response: FilesWithRawResponse

def __init__(self, client: OpenAI) -> None:
super().__init__(client)
self.with_raw_response = FilesWithRawResponse(self)
@cached_property
def with_raw_response(self) -> FilesWithRawResponse:
return FilesWithRawResponse(self)

def create(
self,
Expand Down Expand Up @@ -215,11 +205,9 @@ def delete(


class AsyncFiles(AsyncAPIResource):
with_raw_response: AsyncFilesWithRawResponse

def __init__(self, client: AsyncOpenAI) -> None:
super().__init__(client)
self.with_raw_response = AsyncFilesWithRawResponse(self)
@cached_property
def with_raw_response(self) -> AsyncFilesWithRawResponse:
return AsyncFilesWithRawResponse(self)

async def create(
self,
Expand Down
Loading

0 comments on commit 131f6bc

Please sign in to comment.