Skip to content

Commit 2927603

Browse files
chore: speedup initial import
1 parent 9bc7dc6 commit 2927603

File tree

2 files changed

+105
-43
lines changed

2 files changed

+105
-43
lines changed

src/parallel/_client.py

Lines changed: 105 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import os
6-
from typing import Any, Mapping
6+
from typing import TYPE_CHECKING, Any, Mapping
77
from typing_extensions import Self, override
88

99
import httpx
@@ -20,16 +20,20 @@
2020
not_given,
2121
)
2222
from ._utils import is_given, get_async_library
23+
from ._compat import cached_property
2324
from ._version import __version__
24-
from .resources import task_run
2525
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
2626
from ._exceptions import ParallelError, APIStatusError
2727
from ._base_client import (
2828
DEFAULT_MAX_RETRIES,
2929
SyncAPIClient,
3030
AsyncAPIClient,
3131
)
32-
from .resources.beta import beta
32+
33+
if TYPE_CHECKING:
34+
from .resources import beta, task_run
35+
from .resources.task_run import TaskRunResource, AsyncTaskRunResource
36+
from .resources.beta.beta import BetaResource, AsyncBetaResource
3337

3438
__all__ = [
3539
"Timeout",
@@ -44,11 +48,6 @@
4448

4549

4650
class Parallel(SyncAPIClient):
47-
task_run: task_run.TaskRunResource
48-
beta: beta.BetaResource
49-
with_raw_response: ParallelWithRawResponse
50-
with_streaming_response: ParallelWithStreamedResponse
51-
5251
# client options
5352
api_key: str
5453

@@ -103,10 +102,25 @@ def __init__(
103102
_strict_response_validation=_strict_response_validation,
104103
)
105104

106-
self.task_run = task_run.TaskRunResource(self)
107-
self.beta = beta.BetaResource(self)
108-
self.with_raw_response = ParallelWithRawResponse(self)
109-
self.with_streaming_response = ParallelWithStreamedResponse(self)
105+
@cached_property
106+
def task_run(self) -> TaskRunResource:
107+
from .resources.task_run import TaskRunResource
108+
109+
return TaskRunResource(self)
110+
111+
@cached_property
112+
def beta(self) -> BetaResource:
113+
from .resources.beta import BetaResource
114+
115+
return BetaResource(self)
116+
117+
@cached_property
118+
def with_raw_response(self) -> ParallelWithRawResponse:
119+
return ParallelWithRawResponse(self)
120+
121+
@cached_property
122+
def with_streaming_response(self) -> ParallelWithStreamedResponse:
123+
return ParallelWithStreamedResponse(self)
110124

111125
@property
112126
@override
@@ -214,11 +228,6 @@ def _make_status_error(
214228

215229

216230
class AsyncParallel(AsyncAPIClient):
217-
task_run: task_run.AsyncTaskRunResource
218-
beta: beta.AsyncBetaResource
219-
with_raw_response: AsyncParallelWithRawResponse
220-
with_streaming_response: AsyncParallelWithStreamedResponse
221-
222231
# client options
223232
api_key: str
224233

@@ -273,10 +282,25 @@ def __init__(
273282
_strict_response_validation=_strict_response_validation,
274283
)
275284

276-
self.task_run = task_run.AsyncTaskRunResource(self)
277-
self.beta = beta.AsyncBetaResource(self)
278-
self.with_raw_response = AsyncParallelWithRawResponse(self)
279-
self.with_streaming_response = AsyncParallelWithStreamedResponse(self)
285+
@cached_property
286+
def task_run(self) -> AsyncTaskRunResource:
287+
from .resources.task_run import AsyncTaskRunResource
288+
289+
return AsyncTaskRunResource(self)
290+
291+
@cached_property
292+
def beta(self) -> AsyncBetaResource:
293+
from .resources.beta import AsyncBetaResource
294+
295+
return AsyncBetaResource(self)
296+
297+
@cached_property
298+
def with_raw_response(self) -> AsyncParallelWithRawResponse:
299+
return AsyncParallelWithRawResponse(self)
300+
301+
@cached_property
302+
def with_streaming_response(self) -> AsyncParallelWithStreamedResponse:
303+
return AsyncParallelWithStreamedResponse(self)
280304

281305
@property
282306
@override
@@ -384,27 +408,79 @@ def _make_status_error(
384408

385409

386410
class ParallelWithRawResponse:
411+
_client: Parallel
412+
387413
def __init__(self, client: Parallel) -> None:
388-
self.task_run = task_run.TaskRunResourceWithRawResponse(client.task_run)
389-
self.beta = beta.BetaResourceWithRawResponse(client.beta)
414+
self._client = client
415+
416+
@cached_property
417+
def task_run(self) -> task_run.TaskRunResourceWithRawResponse:
418+
from .resources.task_run import TaskRunResourceWithRawResponse
419+
420+
return TaskRunResourceWithRawResponse(self._client.task_run)
421+
422+
@cached_property
423+
def beta(self) -> beta.BetaResourceWithRawResponse:
424+
from .resources.beta import BetaResourceWithRawResponse
425+
426+
return BetaResourceWithRawResponse(self._client.beta)
390427

391428

392429
class AsyncParallelWithRawResponse:
430+
_client: AsyncParallel
431+
393432
def __init__(self, client: AsyncParallel) -> None:
394-
self.task_run = task_run.AsyncTaskRunResourceWithRawResponse(client.task_run)
395-
self.beta = beta.AsyncBetaResourceWithRawResponse(client.beta)
433+
self._client = client
434+
435+
@cached_property
436+
def task_run(self) -> task_run.AsyncTaskRunResourceWithRawResponse:
437+
from .resources.task_run import AsyncTaskRunResourceWithRawResponse
438+
439+
return AsyncTaskRunResourceWithRawResponse(self._client.task_run)
440+
441+
@cached_property
442+
def beta(self) -> beta.AsyncBetaResourceWithRawResponse:
443+
from .resources.beta import AsyncBetaResourceWithRawResponse
444+
445+
return AsyncBetaResourceWithRawResponse(self._client.beta)
396446

397447

398448
class ParallelWithStreamedResponse:
449+
_client: Parallel
450+
399451
def __init__(self, client: Parallel) -> None:
400-
self.task_run = task_run.TaskRunResourceWithStreamingResponse(client.task_run)
401-
self.beta = beta.BetaResourceWithStreamingResponse(client.beta)
452+
self._client = client
453+
454+
@cached_property
455+
def task_run(self) -> task_run.TaskRunResourceWithStreamingResponse:
456+
from .resources.task_run import TaskRunResourceWithStreamingResponse
457+
458+
return TaskRunResourceWithStreamingResponse(self._client.task_run)
459+
460+
@cached_property
461+
def beta(self) -> beta.BetaResourceWithStreamingResponse:
462+
from .resources.beta import BetaResourceWithStreamingResponse
463+
464+
return BetaResourceWithStreamingResponse(self._client.beta)
402465

403466

404467
class AsyncParallelWithStreamedResponse:
468+
_client: AsyncParallel
469+
405470
def __init__(self, client: AsyncParallel) -> None:
406-
self.task_run = task_run.AsyncTaskRunResourceWithStreamingResponse(client.task_run)
407-
self.beta = beta.AsyncBetaResourceWithStreamingResponse(client.beta)
471+
self._client = client
472+
473+
@cached_property
474+
def task_run(self) -> task_run.AsyncTaskRunResourceWithStreamingResponse:
475+
from .resources.task_run import AsyncTaskRunResourceWithStreamingResponse
476+
477+
return AsyncTaskRunResourceWithStreamingResponse(self._client.task_run)
478+
479+
@cached_property
480+
def beta(self) -> beta.AsyncBetaResourceWithStreamingResponse:
481+
from .resources.beta import AsyncBetaResourceWithStreamingResponse
482+
483+
return AsyncBetaResourceWithStreamingResponse(self._client.beta)
408484

409485

410486
Client = Parallel

src/parallel/resources/__init__.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
from .beta import (
4-
BetaResource,
5-
AsyncBetaResource,
6-
BetaResourceWithRawResponse,
7-
AsyncBetaResourceWithRawResponse,
8-
BetaResourceWithStreamingResponse,
9-
AsyncBetaResourceWithStreamingResponse,
10-
)
113
from .task_run import (
124
TaskRunResource,
135
AsyncTaskRunResource,
@@ -24,10 +16,4 @@
2416
"AsyncTaskRunResourceWithRawResponse",
2517
"TaskRunResourceWithStreamingResponse",
2618
"AsyncTaskRunResourceWithStreamingResponse",
27-
"BetaResource",
28-
"AsyncBetaResource",
29-
"BetaResourceWithRawResponse",
30-
"AsyncBetaResourceWithRawResponse",
31-
"BetaResourceWithStreamingResponse",
32-
"AsyncBetaResourceWithStreamingResponse",
3319
]

0 commit comments

Comments
 (0)