From 8671297b87105635accefd574c44dbffd8a4f9e9 Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Thu, 4 Jan 2024 16:25:22 -0500 Subject: [PATCH] chore: use property declarations for resource members (#300) This will speedup client instantiation in certain cases. --- pyproject.toml | 1 + src/anthropic/_compat.py | 10 ++++++++ src/anthropic/resources/beta/beta.py | 30 ++++++++++-------------- src/anthropic/resources/beta/messages.py | 29 +++++++---------------- src/anthropic/resources/completions.py | 22 +++++++---------- 5 files changed, 41 insertions(+), 51 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a5122880..d6e3b1b9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ dependencies = [ "anyio>=3.5.0, <5", "distro>=1.7.0, <2", "sniffio", + "cached-property; python_version < '3.8'", "tokenizers >= 0.13.0" ] requires-python = ">= 3.7" diff --git a/src/anthropic/_compat.py b/src/anthropic/_compat.py index d95db8ed..3cda3990 100644 --- a/src/anthropic/_compat.py +++ b/src/anthropic/_compat.py @@ -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 diff --git a/src/anthropic/resources/beta/beta.py b/src/anthropic/resources/beta/beta.py index 823e9669..ab63ff8b 100644 --- a/src/anthropic/resources/beta/beta.py +++ b/src/anthropic/resources/beta/beta.py @@ -2,35 +2,31 @@ from __future__ import annotations -from typing import TYPE_CHECKING - from .messages import Messages, AsyncMessages, MessagesWithRawResponse, AsyncMessagesWithRawResponse +from ..._compat import cached_property from ..._resource import SyncAPIResource, AsyncAPIResource -if TYPE_CHECKING: - from ..._client import Anthropic, AsyncAnthropic - __all__ = ["Beta", "AsyncBeta"] class Beta(SyncAPIResource): - messages: Messages - with_raw_response: BetaWithRawResponse + @cached_property + def messages(self) -> Messages: + return Messages(self._client) - def __init__(self, client: Anthropic) -> None: - super().__init__(client) - self.messages = Messages(client) - self.with_raw_response = BetaWithRawResponse(self) + @cached_property + def with_raw_response(self) -> BetaWithRawResponse: + return BetaWithRawResponse(self) class AsyncBeta(AsyncAPIResource): - messages: AsyncMessages - with_raw_response: AsyncBetaWithRawResponse + @cached_property + def messages(self) -> AsyncMessages: + return AsyncMessages(self._client) - def __init__(self, client: AsyncAnthropic) -> None: - super().__init__(client) - self.messages = AsyncMessages(client) - self.with_raw_response = AsyncBetaWithRawResponse(self) + @cached_property + def with_raw_response(self) -> AsyncBetaWithRawResponse: + return AsyncBetaWithRawResponse(self) class BetaWithRawResponse: diff --git a/src/anthropic/resources/beta/messages.py b/src/anthropic/resources/beta/messages.py index 18c524c3..515e6f3e 100644 --- a/src/anthropic/resources/beta/messages.py +++ b/src/anthropic/resources/beta/messages.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, List, overload +from typing import List, overload from functools import partial from typing_extensions import Literal @@ -16,15 +16,11 @@ NotGiven, ) from ..._utils import required_args, 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 ..._streaming import Stream, AsyncStream -from ...types.beta import ( - Message, - MessageParam, - MessageStreamEvent, - message_create_params, -) +from ...types.beta import Message, MessageParam, MessageStreamEvent, message_create_params from ..._base_client import ( make_request_options, ) @@ -37,18 +33,13 @@ AsyncMessageStreamManager, ) -if TYPE_CHECKING: - from ..._client import Anthropic, AsyncAnthropic - __all__ = ["Messages", "AsyncMessages"] class Messages(SyncAPIResource): - with_raw_response: MessagesWithRawResponse - - def __init__(self, client: Anthropic) -> None: - super().__init__(client) - self.with_raw_response = MessagesWithRawResponse(self) + @cached_property + def with_raw_response(self) -> MessagesWithRawResponse: + return MessagesWithRawResponse(self) @overload def create( @@ -671,11 +662,9 @@ def stream( class AsyncMessages(AsyncAPIResource): - with_raw_response: AsyncMessagesWithRawResponse - - def __init__(self, client: AsyncAnthropic) -> None: - super().__init__(client) - self.with_raw_response = AsyncMessagesWithRawResponse(self) + @cached_property + def with_raw_response(self) -> AsyncMessagesWithRawResponse: + return AsyncMessagesWithRawResponse(self) @overload async def create( diff --git a/src/anthropic/resources/completions.py b/src/anthropic/resources/completions.py index d78ac436..f4798401 100644 --- a/src/anthropic/resources/completions.py +++ b/src/anthropic/resources/completions.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, List, Union, overload +from typing import List, Union, overload from typing_extensions import Literal import httpx @@ -16,6 +16,7 @@ NotGiven, ) from .._utils import required_args, 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 .._streaming import Stream, AsyncStream @@ -23,18 +24,13 @@ make_request_options, ) -if TYPE_CHECKING: - from .._client import Anthropic, AsyncAnthropic - __all__ = ["Completions", "AsyncCompletions"] class Completions(SyncAPIResource): - with_raw_response: CompletionsWithRawResponse - - def __init__(self, client: Anthropic) -> None: - super().__init__(client) - self.with_raw_response = CompletionsWithRawResponse(self) + @cached_property + def with_raw_response(self) -> CompletionsWithRawResponse: + return CompletionsWithRawResponse(self) @overload def create( @@ -367,11 +363,9 @@ def create( class AsyncCompletions(AsyncAPIResource): - with_raw_response: AsyncCompletionsWithRawResponse - - def __init__(self, client: AsyncAnthropic) -> None: - super().__init__(client) - self.with_raw_response = AsyncCompletionsWithRawResponse(self) + @cached_property + def with_raw_response(self) -> AsyncCompletionsWithRawResponse: + return AsyncCompletionsWithRawResponse(self) @overload async def create(