Skip to content

Commit 131f6bc

Browse files
chore: use property declarations for resource members (#1047)
This will speedup client instantiation in certain cases.
1 parent d94b4d3 commit 131f6bc

26 files changed

+285
-394
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"anyio>=3.5.0, <5",
1515
"distro>=1.7.0, <2",
1616
"sniffio",
17+
"cached-property; python_version < '3.8'",
1718
"tqdm > 4"
1819
]
1920
requires-python = ">= 3.7.1"

src/openai/_compat.py

+10
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,13 @@ class GenericModel(pydantic.BaseModel):
173173

174174
class GenericModel(pydantic.generics.GenericModel, pydantic.BaseModel):
175175
...
176+
177+
178+
# cached properties
179+
if TYPE_CHECKING:
180+
cached_property = property
181+
else:
182+
try:
183+
from functools import cached_property as cached_property
184+
except ImportError:
185+
from cached_property import cached_property as cached_property

src/openai/resources/audio/audio.py

+30-26
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
6-
75
from .speech import Speech, AsyncSpeech, SpeechWithRawResponse, AsyncSpeechWithRawResponse
6+
from ..._compat import cached_property
87
from ..._resource import SyncAPIResource, AsyncAPIResource
98
from .translations import Translations, AsyncTranslations, TranslationsWithRawResponse, AsyncTranslationsWithRawResponse
109
from .transcriptions import (
@@ -14,38 +13,43 @@
1413
AsyncTranscriptionsWithRawResponse,
1514
)
1615

17-
if TYPE_CHECKING:
18-
from ..._client import OpenAI, AsyncOpenAI
19-
2016
__all__ = ["Audio", "AsyncAudio"]
2117

2218

2319
class Audio(SyncAPIResource):
24-
transcriptions: Transcriptions
25-
translations: Translations
26-
speech: Speech
27-
with_raw_response: AudioWithRawResponse
20+
@cached_property
21+
def transcriptions(self) -> Transcriptions:
22+
return Transcriptions(self._client)
23+
24+
@cached_property
25+
def translations(self) -> Translations:
26+
return Translations(self._client)
27+
28+
@cached_property
29+
def speech(self) -> Speech:
30+
return Speech(self._client)
2831

29-
def __init__(self, client: OpenAI) -> None:
30-
super().__init__(client)
31-
self.transcriptions = Transcriptions(client)
32-
self.translations = Translations(client)
33-
self.speech = Speech(client)
34-
self.with_raw_response = AudioWithRawResponse(self)
32+
@cached_property
33+
def with_raw_response(self) -> AudioWithRawResponse:
34+
return AudioWithRawResponse(self)
3535

3636

3737
class AsyncAudio(AsyncAPIResource):
38-
transcriptions: AsyncTranscriptions
39-
translations: AsyncTranslations
40-
speech: AsyncSpeech
41-
with_raw_response: AsyncAudioWithRawResponse
42-
43-
def __init__(self, client: AsyncOpenAI) -> None:
44-
super().__init__(client)
45-
self.transcriptions = AsyncTranscriptions(client)
46-
self.translations = AsyncTranslations(client)
47-
self.speech = AsyncSpeech(client)
48-
self.with_raw_response = AsyncAudioWithRawResponse(self)
38+
@cached_property
39+
def transcriptions(self) -> AsyncTranscriptions:
40+
return AsyncTranscriptions(self._client)
41+
42+
@cached_property
43+
def translations(self) -> AsyncTranslations:
44+
return AsyncTranslations(self._client)
45+
46+
@cached_property
47+
def speech(self) -> AsyncSpeech:
48+
return AsyncSpeech(self._client)
49+
50+
@cached_property
51+
def with_raw_response(self) -> AsyncAudioWithRawResponse:
52+
return AsyncAudioWithRawResponse(self)
4953

5054

5155
class AudioWithRawResponse:

src/openai/resources/audio/speech.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Union
5+
from typing import Union
66
from typing_extensions import Literal
77

88
import httpx
@@ -15,6 +15,7 @@
1515
NotGiven,
1616
)
1717
from ..._utils import maybe_transform
18+
from ..._compat import cached_property
1819
from ..._resource import SyncAPIResource, AsyncAPIResource
1920
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2021
from ...types.audio import speech_create_params
@@ -23,18 +24,13 @@
2324
make_request_options,
2425
)
2526

26-
if TYPE_CHECKING:
27-
from ..._client import OpenAI, AsyncOpenAI
28-
2927
__all__ = ["Speech", "AsyncSpeech"]
3028

3129

3230
class Speech(SyncAPIResource):
33-
with_raw_response: SpeechWithRawResponse
34-
35-
def __init__(self, client: OpenAI) -> None:
36-
super().__init__(client)
37-
self.with_raw_response = SpeechWithRawResponse(self)
31+
@cached_property
32+
def with_raw_response(self) -> SpeechWithRawResponse:
33+
return SpeechWithRawResponse(self)
3834

3935
def create(
4036
self,
@@ -99,11 +95,9 @@ def create(
9995

10096

10197
class AsyncSpeech(AsyncAPIResource):
102-
with_raw_response: AsyncSpeechWithRawResponse
103-
104-
def __init__(self, client: AsyncOpenAI) -> None:
105-
super().__init__(client)
106-
self.with_raw_response = AsyncSpeechWithRawResponse(self)
98+
@cached_property
99+
def with_raw_response(self) -> AsyncSpeechWithRawResponse:
100+
return AsyncSpeechWithRawResponse(self)
107101

108102
async def create(
109103
self,

src/openai/resources/audio/transcriptions.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Union, Mapping, cast
5+
from typing import Union, Mapping, cast
66
from typing_extensions import Literal
77

88
import httpx
@@ -16,25 +16,21 @@
1616
FileTypes,
1717
)
1818
from ..._utils import extract_files, maybe_transform, deepcopy_minimal
19+
from ..._compat import cached_property
1920
from ..._resource import SyncAPIResource, AsyncAPIResource
2021
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2122
from ...types.audio import Transcription, transcription_create_params
2223
from ..._base_client import (
2324
make_request_options,
2425
)
2526

26-
if TYPE_CHECKING:
27-
from ..._client import OpenAI, AsyncOpenAI
28-
2927
__all__ = ["Transcriptions", "AsyncTranscriptions"]
3028

3129

3230
class Transcriptions(SyncAPIResource):
33-
with_raw_response: TranscriptionsWithRawResponse
34-
35-
def __init__(self, client: OpenAI) -> None:
36-
super().__init__(client)
37-
self.with_raw_response = TranscriptionsWithRawResponse(self)
31+
@cached_property
32+
def with_raw_response(self) -> TranscriptionsWithRawResponse:
33+
return TranscriptionsWithRawResponse(self)
3834

3935
def create(
4036
self,
@@ -117,11 +113,9 @@ def create(
117113

118114

119115
class AsyncTranscriptions(AsyncAPIResource):
120-
with_raw_response: AsyncTranscriptionsWithRawResponse
121-
122-
def __init__(self, client: AsyncOpenAI) -> None:
123-
super().__init__(client)
124-
self.with_raw_response = AsyncTranscriptionsWithRawResponse(self)
116+
@cached_property
117+
def with_raw_response(self) -> AsyncTranscriptionsWithRawResponse:
118+
return AsyncTranscriptionsWithRawResponse(self)
125119

126120
async def create(
127121
self,

src/openai/resources/audio/translations.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Union, Mapping, cast
5+
from typing import Union, Mapping, cast
66
from typing_extensions import Literal
77

88
import httpx
@@ -16,25 +16,21 @@
1616
FileTypes,
1717
)
1818
from ..._utils import extract_files, maybe_transform, deepcopy_minimal
19+
from ..._compat import cached_property
1920
from ..._resource import SyncAPIResource, AsyncAPIResource
2021
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2122
from ...types.audio import Translation, translation_create_params
2223
from ..._base_client import (
2324
make_request_options,
2425
)
2526

26-
if TYPE_CHECKING:
27-
from ..._client import OpenAI, AsyncOpenAI
28-
2927
__all__ = ["Translations", "AsyncTranslations"]
3028

3129

3230
class Translations(SyncAPIResource):
33-
with_raw_response: TranslationsWithRawResponse
34-
35-
def __init__(self, client: OpenAI) -> None:
36-
super().__init__(client)
37-
self.with_raw_response = TranslationsWithRawResponse(self)
31+
@cached_property
32+
def with_raw_response(self) -> TranslationsWithRawResponse:
33+
return TranslationsWithRawResponse(self)
3834

3935
def create(
4036
self,
@@ -110,11 +106,9 @@ def create(
110106

111107

112108
class AsyncTranslations(AsyncAPIResource):
113-
with_raw_response: AsyncTranslationsWithRawResponse
114-
115-
def __init__(self, client: AsyncOpenAI) -> None:
116-
super().__init__(client)
117-
self.with_raw_response = AsyncTranslationsWithRawResponse(self)
109+
@cached_property
110+
def with_raw_response(self) -> AsyncTranslationsWithRawResponse:
111+
return AsyncTranslationsWithRawResponse(self)
118112

119113
async def create(
120114
self,

src/openai/resources/beta/assistants/assistants.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, List, Optional
5+
from typing import List, Optional
66
from typing_extensions import Literal
77

88
import httpx
@@ -16,6 +16,7 @@
1616
NotGiven,
1717
)
1818
from ...._utils import maybe_transform
19+
from ...._compat import cached_property
1920
from ...._resource import SyncAPIResource, AsyncAPIResource
2021
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2122
from ....pagination import SyncCursorPage, AsyncCursorPage
@@ -31,20 +32,17 @@
3132
make_request_options,
3233
)
3334

34-
if TYPE_CHECKING:
35-
from ...._client import OpenAI, AsyncOpenAI
36-
3735
__all__ = ["Assistants", "AsyncAssistants"]
3836

3937

4038
class Assistants(SyncAPIResource):
41-
files: Files
42-
with_raw_response: AssistantsWithRawResponse
39+
@cached_property
40+
def files(self) -> Files:
41+
return Files(self._client)
4342

44-
def __init__(self, client: OpenAI) -> None:
45-
super().__init__(client)
46-
self.files = Files(client)
47-
self.with_raw_response = AssistantsWithRawResponse(self)
43+
@cached_property
44+
def with_raw_response(self) -> AssistantsWithRawResponse:
45+
return AssistantsWithRawResponse(self)
4846

4947
def create(
5048
self,
@@ -331,13 +329,13 @@ def delete(
331329

332330

333331
class AsyncAssistants(AsyncAPIResource):
334-
files: AsyncFiles
335-
with_raw_response: AsyncAssistantsWithRawResponse
332+
@cached_property
333+
def files(self) -> AsyncFiles:
334+
return AsyncFiles(self._client)
336335

337-
def __init__(self, client: AsyncOpenAI) -> None:
338-
super().__init__(client)
339-
self.files = AsyncFiles(client)
340-
self.with_raw_response = AsyncAssistantsWithRawResponse(self)
336+
@cached_property
337+
def with_raw_response(self) -> AsyncAssistantsWithRawResponse:
338+
return AsyncAssistantsWithRawResponse(self)
341339

342340
async def create(
343341
self,

src/openai/resources/beta/assistants/files.py

+8-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
65
from typing_extensions import Literal
76

87
import httpx
@@ -15,32 +14,23 @@
1514
NotGiven,
1615
)
1716
from ...._utils import maybe_transform
17+
from ...._compat import cached_property
1818
from ...._resource import SyncAPIResource, AsyncAPIResource
1919
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2020
from ....pagination import SyncCursorPage, AsyncCursorPage
2121
from ...._base_client import (
2222
AsyncPaginator,
2323
make_request_options,
2424
)
25-
from ....types.beta.assistants import (
26-
AssistantFile,
27-
FileDeleteResponse,
28-
file_list_params,
29-
file_create_params,
30-
)
31-
32-
if TYPE_CHECKING:
33-
from ...._client import OpenAI, AsyncOpenAI
25+
from ....types.beta.assistants import AssistantFile, FileDeleteResponse, file_list_params, file_create_params
3426

3527
__all__ = ["Files", "AsyncFiles"]
3628

3729

3830
class Files(SyncAPIResource):
39-
with_raw_response: FilesWithRawResponse
40-
41-
def __init__(self, client: OpenAI) -> None:
42-
super().__init__(client)
43-
self.with_raw_response = FilesWithRawResponse(self)
31+
@cached_property
32+
def with_raw_response(self) -> FilesWithRawResponse:
33+
return FilesWithRawResponse(self)
4434

4535
def create(
4636
self,
@@ -215,11 +205,9 @@ def delete(
215205

216206

217207
class AsyncFiles(AsyncAPIResource):
218-
with_raw_response: AsyncFilesWithRawResponse
219-
220-
def __init__(self, client: AsyncOpenAI) -> None:
221-
super().__init__(client)
222-
self.with_raw_response = AsyncFilesWithRawResponse(self)
208+
@cached_property
209+
def with_raw_response(self) -> AsyncFilesWithRawResponse:
210+
return AsyncFilesWithRawResponse(self)
223211

224212
async def create(
225213
self,

0 commit comments

Comments
 (0)