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: remove deprecated HTTP client options #777

Merged
merged 1 commit into from
Dec 9, 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
64 changes: 2 additions & 62 deletions src/anthropic/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
NotGiven,
Transport,
ProxiesTypes,
AsyncTransport,
RequestOptions,
)
from ._utils import (
Expand All @@ -30,11 +29,8 @@
from ._exceptions import APIStatusError
from ._base_client import (
DEFAULT_MAX_RETRIES,
DEFAULT_CONNECTION_LIMITS,
SyncAPIClient,
AsyncAPIClient,
SyncHttpxClientWrapper,
AsyncHttpxClientWrapper,
)

__all__ = [
Expand Down Expand Up @@ -79,12 +75,6 @@ def __init__(
# We provide a `DefaultHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`.
# See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
http_client: httpx.Client | None = None,
# See httpx documentation for [custom transports](https://www.python-httpx.org/advanced/#custom-transports)
transport: Transport | None = None,
# See httpx documentation for [proxies](https://www.python-httpx.org/advanced/#http-proxying)
proxies: ProxiesTypes | None = None,
# See httpx documentation for [limits](https://www.python-httpx.org/advanced/#pool-limit-configuration)
connection_pool_limits: httpx.Limits | None = None,
# Enable or disable schema validation for data returned by the API.
# When enabled an error APIResponseValidationError is raised
# if the API responds with invalid data for the expected schema.
Expand Down Expand Up @@ -120,9 +110,6 @@ def __init__(
max_retries=max_retries,
timeout=timeout,
http_client=http_client,
transport=transport,
proxies=proxies,
limits=connection_pool_limits,
custom_headers=default_headers,
custom_query=default_query,
_strict_response_validation=_strict_response_validation,
Expand Down Expand Up @@ -198,7 +185,6 @@ def copy(
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.Client | None = None,
connection_pool_limits: httpx.Limits | None = None,
max_retries: int | NotGiven = NOT_GIVEN,
default_headers: Mapping[str, str] | None = None,
set_default_headers: Mapping[str, str] | None = None,
Expand Down Expand Up @@ -227,31 +213,13 @@ def copy(
elif set_default_query is not None:
params = set_default_query

if connection_pool_limits is not None:
if http_client is not None:
raise ValueError("The 'http_client' argument is mutually exclusive with 'connection_pool_limits'")

if not isinstance(self._client, SyncHttpxClientWrapper):
raise ValueError(
"A custom HTTP client has been set and is mutually exclusive with the 'connection_pool_limits' argument"
)

http_client = None
else:
if self._limits is not DEFAULT_CONNECTION_LIMITS:
connection_pool_limits = self._limits
else:
connection_pool_limits = None

http_client = http_client or self._client

http_client = http_client or self._client
return self.__class__(
api_key=api_key or self.api_key,
auth_token=auth_token or self.auth_token,
base_url=base_url or self.base_url,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
connection_pool_limits=connection_pool_limits,
max_retries=max_retries if is_given(max_retries) else self.max_retries,
default_headers=headers,
default_query=params,
Expand Down Expand Up @@ -325,12 +293,6 @@ def __init__(
# We provide a `DefaultAsyncHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`.
# See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details.
http_client: httpx.AsyncClient | None = None,
# See httpx documentation for [custom transports](https://www.python-httpx.org/advanced/#custom-transports)
transport: AsyncTransport | None = None,
# See httpx documentation for [proxies](https://www.python-httpx.org/advanced/#http-proxying)
proxies: ProxiesTypes | None = None,
# See httpx documentation for [limits](https://www.python-httpx.org/advanced/#pool-limit-configuration)
connection_pool_limits: httpx.Limits | None = None,
# Enable or disable schema validation for data returned by the API.
# When enabled an error APIResponseValidationError is raised
# if the API responds with invalid data for the expected schema.
Expand Down Expand Up @@ -366,9 +328,6 @@ def __init__(
max_retries=max_retries,
timeout=timeout,
http_client=http_client,
transport=transport,
proxies=proxies,
limits=connection_pool_limits,
custom_headers=default_headers,
custom_query=default_query,
_strict_response_validation=_strict_response_validation,
Expand Down Expand Up @@ -444,7 +403,6 @@ def copy(
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.AsyncClient | None = None,
connection_pool_limits: httpx.Limits | None = None,
max_retries: int | NotGiven = NOT_GIVEN,
default_headers: Mapping[str, str] | None = None,
set_default_headers: Mapping[str, str] | None = None,
Expand Down Expand Up @@ -473,31 +431,13 @@ def copy(
elif set_default_query is not None:
params = set_default_query

if connection_pool_limits is not None:
if http_client is not None:
raise ValueError("The 'http_client' argument is mutually exclusive with 'connection_pool_limits'")

if not isinstance(self._client, AsyncHttpxClientWrapper):
raise ValueError(
"A custom HTTP client has been set and is mutually exclusive with the 'connection_pool_limits' argument"
)

http_client = None
else:
if self._limits is not DEFAULT_CONNECTION_LIMITS:
connection_pool_limits = self._limits
else:
connection_pool_limits = None

http_client = http_client or self._client

http_client = http_client or self._client
return self.__class__(
api_key=api_key or self.api_key,
auth_token=auth_token or self.auth_token,
base_url=base_url or self.base_url,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
connection_pool_limits=connection_pool_limits,
max_retries=max_retries if is_given(max_retries) else self.max_retries,
default_headers=headers,
default_query=params,
Expand Down
188 changes: 0 additions & 188 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,99 +634,6 @@ def test_absolute_request_url(self, client: Anthropic) -> None:
)
assert request.url == "https://myapi.com/foo"

def test_transport_option_is_deprecated(self) -> None:
with pytest.warns(
DeprecationWarning,
match="The `transport` argument is deprecated. The `http_client` argument should be passed instead",
):
transport = httpx.MockTransport(
lambda: None, # type: ignore
)

client = Anthropic(
base_url=base_url, api_key=api_key, _strict_response_validation=True, transport=transport
)

assert client._client._transport is transport

def test_transport_option_mutually_exclusive_with_http_client(self) -> None:
with httpx.Client() as http_client:
with pytest.raises(ValueError, match="The `http_client` argument is mutually exclusive with `transport`"):
with pytest.warns(DeprecationWarning):
Anthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
transport=httpx.MockTransport(
lambda: None, # type: ignore
),
http_client=http_client,
)

def test_connection_pool_limits_option_is_deprecated(self) -> None:
with pytest.warns(
DeprecationWarning,
match="The `connection_pool_limits` argument is deprecated. The `http_client` argument should be passed instead",
):
connection_pool_limits = httpx.Limits(
max_connections=101, max_keepalive_connections=76, keepalive_expiry=23
)

client = Anthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
connection_pool_limits=connection_pool_limits,
)

assert isinstance(client._client._transport, httpx.HTTPTransport)
assert client._client._transport._pool._max_connections == 101
assert client._client._transport._pool._max_keepalive_connections == 76
assert client._client._transport._pool._keepalive_expiry == 23

def test_connection_pool_limits_option_mutually_exclusive_with_http_client(self) -> None:
with httpx.Client() as http_client:
with pytest.raises(
ValueError, match="The `http_client` argument is mutually exclusive with `connection_pool_limits`"
):
with pytest.warns(DeprecationWarning):
Anthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
connection_pool_limits=httpx.Limits(
max_connections=101, max_keepalive_connections=76, keepalive_expiry=23
),
http_client=http_client,
)

def test_proxies_option_is_deprecated(self) -> None:
with pytest.warns(
DeprecationWarning,
match="The `proxies` argument is deprecated. The `http_client` argument should be passed instead",
):
proxies = "https://www.example.com/proxy"

client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True, proxies=proxies)

mounts = list(client._client._mounts.keys())
assert len(mounts) == 1

pattern = mounts[0].pattern
assert pattern == "all://"

def test_proxies_option_mutually_exclusive_with_http_client(self) -> None:
with httpx.Client() as http_client:
with pytest.raises(ValueError, match="The `http_client` argument is mutually exclusive with `proxies`"):
with pytest.warns(DeprecationWarning):
Anthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
proxies="https://www.example.com/proxy",
http_client=http_client,
)

def test_copied_client_does_not_close_http(self) -> None:
client = Anthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
assert not client.is_closed()
Expand Down Expand Up @@ -1600,101 +1507,6 @@ def test_absolute_request_url(self, client: AsyncAnthropic) -> None:
)
assert request.url == "https://myapi.com/foo"

def test_transport_option_is_deprecated(self) -> None:
with pytest.warns(
DeprecationWarning,
match="The `transport` argument is deprecated. The `http_client` argument should be passed instead",
):
transport = httpx.MockTransport(
lambda: None, # type: ignore
)

client = AsyncAnthropic(
base_url=base_url, api_key=api_key, _strict_response_validation=True, transport=transport
)

assert client._client._transport is transport

async def test_transport_option_mutually_exclusive_with_http_client(self) -> None:
async with httpx.AsyncClient() as http_client:
with pytest.raises(ValueError, match="The `http_client` argument is mutually exclusive with `transport`"):
with pytest.warns(DeprecationWarning):
AsyncAnthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
transport=httpx.MockTransport(
lambda: None, # type: ignore
),
http_client=http_client,
)

def test_connection_pool_limits_option_is_deprecated(self) -> None:
with pytest.warns(
DeprecationWarning,
match="The `connection_pool_limits` argument is deprecated. The `http_client` argument should be passed instead",
):
connection_pool_limits = httpx.Limits(
max_connections=101, max_keepalive_connections=76, keepalive_expiry=23
)

client = AsyncAnthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
connection_pool_limits=connection_pool_limits,
)

assert isinstance(client._client._transport, httpx.AsyncHTTPTransport)
assert client._client._transport._pool._max_connections == 101
assert client._client._transport._pool._max_keepalive_connections == 76
assert client._client._transport._pool._keepalive_expiry == 23

async def test_connection_pool_limits_option_mutually_exclusive_with_http_client(self) -> None:
async with httpx.AsyncClient() as http_client:
with pytest.raises(
ValueError, match="The `http_client` argument is mutually exclusive with `connection_pool_limits`"
):
with pytest.warns(DeprecationWarning):
AsyncAnthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
connection_pool_limits=httpx.Limits(
max_connections=101, max_keepalive_connections=76, keepalive_expiry=23
),
http_client=http_client,
)

def test_proxies_option_is_deprecated(self) -> None:
with pytest.warns(
DeprecationWarning,
match="The `proxies` argument is deprecated. The `http_client` argument should be passed instead",
):
proxies = "https://www.example.com/proxy"

client = AsyncAnthropic(
base_url=base_url, api_key=api_key, _strict_response_validation=True, proxies=proxies
)

mounts = list(client._client._mounts.keys())
assert len(mounts) == 1

pattern = mounts[0].pattern
assert pattern == "all://"

async def test_proxies_option_mutually_exclusive_with_http_client(self) -> None:
async with httpx.AsyncClient() as http_client:
with pytest.raises(ValueError, match="The `http_client` argument is mutually exclusive with `proxies`"):
with pytest.warns(DeprecationWarning):
AsyncAnthropic(
base_url=base_url,
api_key=api_key,
_strict_response_validation=True,
proxies="https://www.example.com/proxy",
http_client=http_client,
)

async def test_copied_client_does_not_close_http(self) -> None:
client = AsyncAnthropic(base_url=base_url, api_key=api_key, _strict_response_validation=True)
assert not client.is_closed()
Expand Down