Skip to content

Commit

Permalink
chore(internal): add helpers (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Sep 14, 2023
1 parent 46386f8 commit 00f5a19
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/anthropic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Transport,
AsyncClient,
AsyncStream,
ProxiesTypes,
AsyncAnthropic,
RequestOptions,
)
Expand Down
15 changes: 15 additions & 0 deletions src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ class BaseClient:
max_retries: int
timeout: Union[float, Timeout, None]
_limits: httpx.Limits
_proxies: ProxiesTypes | None
_transport: Transport | None
_strict_response_validation: bool
_idempotency_header: str | None

Expand All @@ -315,13 +317,17 @@ def __init__(
max_retries: int = DEFAULT_MAX_RETRIES,
timeout: float | Timeout | None = DEFAULT_TIMEOUT,
limits: httpx.Limits,
transport: Transport | None,
proxies: ProxiesTypes | None,
custom_headers: Mapping[str, str] | None = None,
custom_query: Mapping[str, object] | None = None,
) -> None:
self._version = version
self.max_retries = max_retries
self.timeout = timeout
self._limits = limits
self._proxies = proxies
self._transport = transport
self._custom_headers = custom_headers or {}
self._custom_query = custom_query or {}
self._strict_response_validation = _strict_response_validation
Expand Down Expand Up @@ -590,6 +596,11 @@ def user_agent(self) -> str:
def base_url(self) -> URL:
return self._client.base_url

@base_url.setter
def base_url(self, url: URL | str) -> None:
# mypy doesn't use the type from the setter
self._client.base_url = url # type: ignore[assignment]

@lru_cache(maxsize=None)
def platform_headers(self) -> Dict[str, str]:
return {
Expand Down Expand Up @@ -689,6 +700,8 @@ def __init__(
version=version,
limits=limits,
timeout=timeout,
proxies=proxies,
transport=transport,
max_retries=max_retries,
custom_query=custom_query,
custom_headers=custom_headers,
Expand Down Expand Up @@ -1045,6 +1058,8 @@ def __init__(
version=version,
limits=limits,
timeout=timeout,
proxies=proxies,
transport=transport,
max_retries=max_retries,
custom_query=custom_query,
custom_headers=custom_headers,
Expand Down
1 change: 1 addition & 0 deletions src/anthropic/_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._proxy import LazyProxy as LazyProxy
from ._utils import flatten as flatten
from ._utils import is_dict as is_dict
from ._utils import is_list as is_list
Expand Down
57 changes: 57 additions & 0 deletions src/anthropic/_utils/_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Generic, TypeVar, Iterable, cast
from typing_extensions import ClassVar

T = TypeVar("T")


class LazyProxy(Generic[T], ABC):
"""Implements data methods to pretend that an instance is another instance.
This includes forwarding attribute access and othe methods.
"""

should_cache: ClassVar[bool] = False

def __init__(self) -> None:
self.__proxied: T | None = None

def __getattr__(self, attr: str) -> object:
return getattr(self.__get_proxied__(), attr)

def __repr__(self) -> str:
return repr(self.__get_proxied__())

def __str__(self) -> str:
return str(self.__get_proxied__())

def __dir__(self) -> Iterable[str]:
return self.__get_proxied__().__dir__()

@property # type: ignore
def __class__(self) -> type:
return self.__get_proxied__().__class__

def __get_proxied__(self) -> T:
if not self.should_cache:
return self.__load__()

proxied = self.__proxied
if proxied is not None:
return proxied

self.__proxied = proxied = self.__load__()
return proxied

def __set_proxied__(self, value: T) -> None:
self.__proxied = value

def __as_proxied__(self) -> T:
"""Helper method that returns the current proxy, typed as the loaded object"""
return cast(T, self)

@abstractmethod
def __load__(self) -> T:
...

0 comments on commit 00f5a19

Please sign in to comment.