Skip to content

Commit 905dffb

Browse files
chore(client): improve error message for invalid http_client argument (#311)
1 parent 70d9da9 commit 905dffb

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/finch/_base_client.py

+10
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,11 @@ def __init__(
780780
else:
781781
timeout = DEFAULT_TIMEOUT
782782

783+
if http_client is not None and not isinstance(http_client, httpx.Client): # pyright: ignore[reportUnnecessaryIsInstance]
784+
raise TypeError(
785+
f"Invalid `http_client` argument; Expected an instance of `httpx.Client` but got {type(http_client)}"
786+
)
787+
783788
super().__init__(
784789
version=version,
785790
limits=limits,
@@ -1322,6 +1327,11 @@ def __init__(
13221327
else:
13231328
timeout = DEFAULT_TIMEOUT
13241329

1330+
if http_client is not None and not isinstance(http_client, httpx.AsyncClient): # pyright: ignore[reportUnnecessaryIsInstance]
1331+
raise TypeError(
1332+
f"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got {type(http_client)}"
1333+
)
1334+
13251335
super().__init__(
13261336
version=version,
13271337
base_url=base_url,

tests/test_client.py

+24
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ def test_http_client_timeout_option(self) -> None:
327327
timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
328328
assert timeout == DEFAULT_TIMEOUT # our default
329329

330+
async def test_invalid_http_client(self) -> None:
331+
with pytest.raises(TypeError, match="Invalid `http_client` arg"):
332+
async with httpx.AsyncClient() as http_client:
333+
Finch(
334+
base_url=base_url,
335+
access_token=access_token,
336+
client_id=client_id,
337+
client_secret=client_secret,
338+
_strict_response_validation=True,
339+
http_client=cast(Any, http_client),
340+
)
341+
330342
def test_default_headers_option(self) -> None:
331343
client = Finch(
332344
base_url=base_url,
@@ -1220,6 +1232,18 @@ async def test_http_client_timeout_option(self) -> None:
12201232
timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore
12211233
assert timeout == DEFAULT_TIMEOUT # our default
12221234

1235+
def test_invalid_http_client(self) -> None:
1236+
with pytest.raises(TypeError, match="Invalid `http_client` arg"):
1237+
with httpx.Client() as http_client:
1238+
AsyncFinch(
1239+
base_url=base_url,
1240+
access_token=access_token,
1241+
client_id=client_id,
1242+
client_secret=client_secret,
1243+
_strict_response_validation=True,
1244+
http_client=cast(Any, http_client),
1245+
)
1246+
12231247
def test_default_headers_option(self) -> None:
12241248
client = AsyncFinch(
12251249
base_url=base_url,

0 commit comments

Comments
 (0)