Skip to content

Commit 2e54d11

Browse files
chore: speedup initial import
1 parent a2599a9 commit 2e54d11

File tree

1 file changed

+142
-38
lines changed

1 file changed

+142
-38
lines changed

src/perplexity/_client.py

Lines changed: 142 additions & 38 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,17 +20,21 @@
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 search
2525
from ._streaming import Stream as Stream, AsyncStream as AsyncStream
2626
from ._exceptions import APIStatusError, PerplexityError
2727
from ._base_client import (
2828
DEFAULT_MAX_RETRIES,
2929
SyncAPIClient,
3030
AsyncAPIClient,
3131
)
32-
from .resources.chat import chat
33-
from .resources.async_ import async_
32+
33+
if TYPE_CHECKING:
34+
from .resources import chat, async_, search
35+
from .resources.search import SearchResource, AsyncSearchResource
36+
from .resources.chat.chat import ChatResource, AsyncChatResource
37+
from .resources.async_.async_ import AsyncResource, AsyncAsyncResource
3438

3539
__all__ = [
3640
"Timeout",
@@ -45,12 +49,6 @@
4549

4650

4751
class Perplexity(SyncAPIClient):
48-
chat: chat.ChatResource
49-
search: search.SearchResource
50-
async_: async_.AsyncResource
51-
with_raw_response: PerplexityWithRawResponse
52-
with_streaming_response: PerplexityWithStreamedResponse
53-
5452
# client options
5553
api_key: str
5654

@@ -107,11 +105,31 @@ def __init__(
107105

108106
self._default_stream_cls = Stream
109107

110-
self.chat = chat.ChatResource(self)
111-
self.search = search.SearchResource(self)
112-
self.async_ = async_.AsyncResource(self)
113-
self.with_raw_response = PerplexityWithRawResponse(self)
114-
self.with_streaming_response = PerplexityWithStreamedResponse(self)
108+
@cached_property
109+
def chat(self) -> ChatResource:
110+
from .resources.chat import ChatResource
111+
112+
return ChatResource(self)
113+
114+
@cached_property
115+
def search(self) -> SearchResource:
116+
from .resources.search import SearchResource
117+
118+
return SearchResource(self)
119+
120+
@cached_property
121+
def async_(self) -> AsyncResource:
122+
from .resources.async_ import AsyncResource
123+
124+
return AsyncResource(self)
125+
126+
@cached_property
127+
def with_raw_response(self) -> PerplexityWithRawResponse:
128+
return PerplexityWithRawResponse(self)
129+
130+
@cached_property
131+
def with_streaming_response(self) -> PerplexityWithStreamedResponse:
132+
return PerplexityWithStreamedResponse(self)
115133

116134
@property
117135
@override
@@ -219,12 +237,6 @@ def _make_status_error(
219237

220238

221239
class AsyncPerplexity(AsyncAPIClient):
222-
chat: chat.AsyncChatResource
223-
search: search.AsyncSearchResource
224-
async_: async_.AsyncAsyncResource
225-
with_raw_response: AsyncPerplexityWithRawResponse
226-
with_streaming_response: AsyncPerplexityWithStreamedResponse
227-
228240
# client options
229241
api_key: str
230242

@@ -281,11 +293,31 @@ def __init__(
281293

282294
self._default_stream_cls = AsyncStream
283295

284-
self.chat = chat.AsyncChatResource(self)
285-
self.search = search.AsyncSearchResource(self)
286-
self.async_ = async_.AsyncAsyncResource(self)
287-
self.with_raw_response = AsyncPerplexityWithRawResponse(self)
288-
self.with_streaming_response = AsyncPerplexityWithStreamedResponse(self)
296+
@cached_property
297+
def chat(self) -> AsyncChatResource:
298+
from .resources.chat import AsyncChatResource
299+
300+
return AsyncChatResource(self)
301+
302+
@cached_property
303+
def search(self) -> AsyncSearchResource:
304+
from .resources.search import AsyncSearchResource
305+
306+
return AsyncSearchResource(self)
307+
308+
@cached_property
309+
def async_(self) -> AsyncAsyncResource:
310+
from .resources.async_ import AsyncAsyncResource
311+
312+
return AsyncAsyncResource(self)
313+
314+
@cached_property
315+
def with_raw_response(self) -> AsyncPerplexityWithRawResponse:
316+
return AsyncPerplexityWithRawResponse(self)
317+
318+
@cached_property
319+
def with_streaming_response(self) -> AsyncPerplexityWithStreamedResponse:
320+
return AsyncPerplexityWithStreamedResponse(self)
289321

290322
@property
291323
@override
@@ -393,31 +425,103 @@ def _make_status_error(
393425

394426

395427
class PerplexityWithRawResponse:
428+
_client: Perplexity
429+
396430
def __init__(self, client: Perplexity) -> None:
397-
self.chat = chat.ChatResourceWithRawResponse(client.chat)
398-
self.search = search.SearchResourceWithRawResponse(client.search)
399-
self.async_ = async_.AsyncResourceWithRawResponse(client.async_)
431+
self._client = client
432+
433+
@cached_property
434+
def chat(self) -> chat.ChatResourceWithRawResponse:
435+
from .resources.chat import ChatResourceWithRawResponse
436+
437+
return ChatResourceWithRawResponse(self._client.chat)
438+
439+
@cached_property
440+
def search(self) -> search.SearchResourceWithRawResponse:
441+
from .resources.search import SearchResourceWithRawResponse
442+
443+
return SearchResourceWithRawResponse(self._client.search)
444+
445+
@cached_property
446+
def async_(self) -> async_.AsyncResourceWithRawResponse:
447+
from .resources.async_ import AsyncResourceWithRawResponse
448+
449+
return AsyncResourceWithRawResponse(self._client.async_)
400450

401451

402452
class AsyncPerplexityWithRawResponse:
453+
_client: AsyncPerplexity
454+
403455
def __init__(self, client: AsyncPerplexity) -> None:
404-
self.chat = chat.AsyncChatResourceWithRawResponse(client.chat)
405-
self.search = search.AsyncSearchResourceWithRawResponse(client.search)
406-
self.async_ = async_.AsyncAsyncResourceWithRawResponse(client.async_)
456+
self._client = client
457+
458+
@cached_property
459+
def chat(self) -> chat.AsyncChatResourceWithRawResponse:
460+
from .resources.chat import AsyncChatResourceWithRawResponse
461+
462+
return AsyncChatResourceWithRawResponse(self._client.chat)
463+
464+
@cached_property
465+
def search(self) -> search.AsyncSearchResourceWithRawResponse:
466+
from .resources.search import AsyncSearchResourceWithRawResponse
467+
468+
return AsyncSearchResourceWithRawResponse(self._client.search)
469+
470+
@cached_property
471+
def async_(self) -> async_.AsyncAsyncResourceWithRawResponse:
472+
from .resources.async_ import AsyncAsyncResourceWithRawResponse
473+
474+
return AsyncAsyncResourceWithRawResponse(self._client.async_)
407475

408476

409477
class PerplexityWithStreamedResponse:
478+
_client: Perplexity
479+
410480
def __init__(self, client: Perplexity) -> None:
411-
self.chat = chat.ChatResourceWithStreamingResponse(client.chat)
412-
self.search = search.SearchResourceWithStreamingResponse(client.search)
413-
self.async_ = async_.AsyncResourceWithStreamingResponse(client.async_)
481+
self._client = client
482+
483+
@cached_property
484+
def chat(self) -> chat.ChatResourceWithStreamingResponse:
485+
from .resources.chat import ChatResourceWithStreamingResponse
486+
487+
return ChatResourceWithStreamingResponse(self._client.chat)
488+
489+
@cached_property
490+
def search(self) -> search.SearchResourceWithStreamingResponse:
491+
from .resources.search import SearchResourceWithStreamingResponse
492+
493+
return SearchResourceWithStreamingResponse(self._client.search)
494+
495+
@cached_property
496+
def async_(self) -> async_.AsyncResourceWithStreamingResponse:
497+
from .resources.async_ import AsyncResourceWithStreamingResponse
498+
499+
return AsyncResourceWithStreamingResponse(self._client.async_)
414500

415501

416502
class AsyncPerplexityWithStreamedResponse:
503+
_client: AsyncPerplexity
504+
417505
def __init__(self, client: AsyncPerplexity) -> None:
418-
self.chat = chat.AsyncChatResourceWithStreamingResponse(client.chat)
419-
self.search = search.AsyncSearchResourceWithStreamingResponse(client.search)
420-
self.async_ = async_.AsyncAsyncResourceWithStreamingResponse(client.async_)
506+
self._client = client
507+
508+
@cached_property
509+
def chat(self) -> chat.AsyncChatResourceWithStreamingResponse:
510+
from .resources.chat import AsyncChatResourceWithStreamingResponse
511+
512+
return AsyncChatResourceWithStreamingResponse(self._client.chat)
513+
514+
@cached_property
515+
def search(self) -> search.AsyncSearchResourceWithStreamingResponse:
516+
from .resources.search import AsyncSearchResourceWithStreamingResponse
517+
518+
return AsyncSearchResourceWithStreamingResponse(self._client.search)
519+
520+
@cached_property
521+
def async_(self) -> async_.AsyncAsyncResourceWithStreamingResponse:
522+
from .resources.async_ import AsyncAsyncResourceWithStreamingResponse
523+
524+
return AsyncAsyncResourceWithStreamingResponse(self._client.async_)
421525

422526

423527
Client = Perplexity

0 commit comments

Comments
 (0)