From 78dd7e4edcd514054acee7cebb18f9ae6375f455 Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Tue, 5 Mar 2024 10:35:04 +0000 Subject: [PATCH] chore(client): improve error message for invalid http_client argument --- src/finch/_base_client.py | 10 ++++++++++ tests/test_client.py | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/finch/_base_client.py b/src/finch/_base_client.py index dda280f6..f431128e 100644 --- a/src/finch/_base_client.py +++ b/src/finch/_base_client.py @@ -780,6 +780,11 @@ def __init__( else: timeout = DEFAULT_TIMEOUT + if http_client is not None and not isinstance(http_client, httpx.Client): # pyright: ignore[reportUnnecessaryIsInstance] + raise TypeError( + f"Invalid `http_client` argument; Expected an instance of `httpx.Client` but got {type(http_client)}" + ) + super().__init__( version=version, limits=limits, @@ -1322,6 +1327,11 @@ def __init__( else: timeout = DEFAULT_TIMEOUT + if http_client is not None and not isinstance(http_client, httpx.AsyncClient): # pyright: ignore[reportUnnecessaryIsInstance] + raise TypeError( + f"Invalid `http_client` argument; Expected an instance of `httpx.AsyncClient` but got {type(http_client)}" + ) + super().__init__( version=version, base_url=base_url, diff --git a/tests/test_client.py b/tests/test_client.py index bc8fdd59..dac0d6c1 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -327,6 +327,18 @@ def test_http_client_timeout_option(self) -> None: timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore assert timeout == DEFAULT_TIMEOUT # our default + async def test_invalid_http_client(self) -> None: + with pytest.raises(TypeError, match="Invalid `http_client` arg"): + async with httpx.AsyncClient() as http_client: + Finch( + base_url=base_url, + access_token=access_token, + client_id=client_id, + client_secret=client_secret, + _strict_response_validation=True, + http_client=cast(Any, http_client), + ) + def test_default_headers_option(self) -> None: client = Finch( base_url=base_url, @@ -1220,6 +1232,18 @@ async def test_http_client_timeout_option(self) -> None: timeout = httpx.Timeout(**request.extensions["timeout"]) # type: ignore assert timeout == DEFAULT_TIMEOUT # our default + def test_invalid_http_client(self) -> None: + with pytest.raises(TypeError, match="Invalid `http_client` arg"): + with httpx.Client() as http_client: + AsyncFinch( + base_url=base_url, + access_token=access_token, + client_id=client_id, + client_secret=client_secret, + _strict_response_validation=True, + http_client=cast(Any, http_client), + ) + def test_default_headers_option(self) -> None: client = AsyncFinch( base_url=base_url,