Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use property declarations for resource members #300

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'",
"tokenizers >= 0.13.0"
]
requires-python = ">= 3.7"
Expand Down
10 changes: 10 additions & 0 deletions src/anthropic/_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
30 changes: 13 additions & 17 deletions src/anthropic/resources/beta/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 9 additions & 20 deletions src/anthropic/resources/beta/messages.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, overload
from typing import List, overload
from functools import partial
from typing_extensions import Literal

Expand All @@ -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,
)
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
22 changes: 8 additions & 14 deletions src/anthropic/resources/completions.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, Union, overload
from typing import List, Union, overload
from typing_extensions import Literal

import httpx
Expand All @@ -16,25 +16,21 @@
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 .._base_client import (
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(
Expand Down Expand Up @@ -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(
Expand Down