Skip to content

Commit fec8c01

Browse files
chore(internal): add helpers (#100)
1 parent f8d0bbf commit fec8c01

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

src/finch/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
AsyncFinch,
1313
AsyncClient,
1414
AsyncStream,
15-
ProxiesTypes,
1615
RequestOptions,
1716
)
1817
from ._version import __title__, __version__

src/finch/_base_client.py

+15
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ class BaseClient:
304304
max_retries: int
305305
timeout: Union[float, Timeout, None]
306306
_limits: httpx.Limits
307+
_proxies: ProxiesTypes | None
308+
_transport: Transport | None
307309
_strict_response_validation: bool
308310
_idempotency_header: str | None
309311

@@ -315,13 +317,17 @@ def __init__(
315317
max_retries: int = DEFAULT_MAX_RETRIES,
316318
timeout: float | Timeout | None = DEFAULT_TIMEOUT,
317319
limits: httpx.Limits,
320+
transport: Transport | None,
321+
proxies: ProxiesTypes | None,
318322
custom_headers: Mapping[str, str] | None = None,
319323
custom_query: Mapping[str, object] | None = None,
320324
) -> None:
321325
self._version = version
322326
self.max_retries = max_retries
323327
self.timeout = timeout
324328
self._limits = limits
329+
self._proxies = proxies
330+
self._transport = transport
325331
self._custom_headers = custom_headers or {}
326332
self._custom_query = custom_query or {}
327333
self._strict_response_validation = _strict_response_validation
@@ -590,6 +596,11 @@ def user_agent(self) -> str:
590596
def base_url(self) -> URL:
591597
return self._client.base_url
592598

599+
@base_url.setter
600+
def base_url(self, url: URL | str) -> None:
601+
# mypy doesn't use the type from the setter
602+
self._client.base_url = url # type: ignore[assignment]
603+
593604
@lru_cache(maxsize=None)
594605
def platform_headers(self) -> Dict[str, str]:
595606
return {
@@ -689,6 +700,8 @@ def __init__(
689700
version=version,
690701
limits=limits,
691702
timeout=timeout,
703+
proxies=proxies,
704+
transport=transport,
692705
max_retries=max_retries,
693706
custom_query=custom_query,
694707
custom_headers=custom_headers,
@@ -1045,6 +1058,8 @@ def __init__(
10451058
version=version,
10461059
limits=limits,
10471060
timeout=timeout,
1061+
proxies=proxies,
1062+
transport=transport,
10481063
max_retries=max_retries,
10491064
custom_query=custom_query,
10501065
custom_headers=custom_headers,

src/finch/_utils/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ._proxy import LazyProxy as LazyProxy
12
from ._utils import flatten as flatten
23
from ._utils import is_dict as is_dict
34
from ._utils import is_list as is_list

src/finch/_utils/_proxy.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from __future__ import annotations
2+
3+
from abc import ABC, abstractmethod
4+
from typing import Generic, TypeVar, Iterable, cast
5+
from typing_extensions import ClassVar
6+
7+
T = TypeVar("T")
8+
9+
10+
class LazyProxy(Generic[T], ABC):
11+
"""Implements data methods to pretend that an instance is another instance.
12+
13+
This includes forwarding attribute access and othe methods.
14+
"""
15+
16+
should_cache: ClassVar[bool] = False
17+
18+
def __init__(self) -> None:
19+
self.__proxied: T | None = None
20+
21+
def __getattr__(self, attr: str) -> object:
22+
return getattr(self.__get_proxied__(), attr)
23+
24+
def __repr__(self) -> str:
25+
return repr(self.__get_proxied__())
26+
27+
def __str__(self) -> str:
28+
return str(self.__get_proxied__())
29+
30+
def __dir__(self) -> Iterable[str]:
31+
return self.__get_proxied__().__dir__()
32+
33+
@property # type: ignore
34+
def __class__(self) -> type:
35+
return self.__get_proxied__().__class__
36+
37+
def __get_proxied__(self) -> T:
38+
if not self.should_cache:
39+
return self.__load__()
40+
41+
proxied = self.__proxied
42+
if proxied is not None:
43+
return proxied
44+
45+
self.__proxied = proxied = self.__load__()
46+
return proxied
47+
48+
def __set_proxied__(self, value: T) -> None:
49+
self.__proxied = value
50+
51+
def __as_proxied__(self) -> T:
52+
"""Helper method that returns the current proxy, typed as the loaded object"""
53+
return cast(T, self)
54+
55+
@abstractmethod
56+
def __load__(self) -> T:
57+
...

0 commit comments

Comments
 (0)