-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vertex): add copy and with_options (#578)
* feat(vertex): add copy and with_options Closes #566 * move vertex client tests to a separate file * add missing `credentials` argument to `copy()` * minor cleanup --------- Co-authored-by: Robert Craigie <robert@craigie.dev>
- Loading branch information
1 parent
a912917
commit fcd425f
Showing
2 changed files
with
322 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
import httpx | ||
import pytest | ||
|
||
from anthropic import AnthropicVertex, AsyncAnthropicVertex | ||
|
||
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | ||
|
||
|
||
class TestAnthropicVertex: | ||
client = AnthropicVertex(region="region", project_id="project") | ||
|
||
def test_copy(self) -> None: | ||
copied = self.client.copy() | ||
assert id(copied) != id(self.client) | ||
|
||
copied = self.client.copy(region="another-region", project_id="another-project") | ||
assert copied.region == "another-region" | ||
assert self.client.region == "region" | ||
assert copied.project_id == "another-project" | ||
assert self.client.project_id == "project" | ||
|
||
def test_with_options(self) -> None: | ||
copied = self.client.with_options(region="another-region", project_id="another-project") | ||
assert copied.region == "another-region" | ||
assert self.client.region == "region" | ||
assert copied.project_id == "another-project" | ||
assert self.client.project_id == "project" | ||
|
||
def test_copy_default_options(self) -> None: | ||
# options that have a default are overridden correctly | ||
copied = self.client.copy(max_retries=7) | ||
assert copied.max_retries == 7 | ||
assert self.client.max_retries == 2 | ||
|
||
copied2 = copied.copy(max_retries=6) | ||
assert copied2.max_retries == 6 | ||
assert copied.max_retries == 7 | ||
|
||
# timeout | ||
assert isinstance(self.client.timeout, httpx.Timeout) | ||
copied = self.client.copy(timeout=None) | ||
assert copied.timeout is None | ||
assert isinstance(self.client.timeout, httpx.Timeout) | ||
|
||
def test_copy_default_headers(self) -> None: | ||
client = AnthropicVertex( | ||
base_url=base_url, | ||
region="region", | ||
project_id="project", | ||
_strict_response_validation=True, | ||
default_headers={"X-Foo": "bar"}, | ||
) | ||
assert client.default_headers["X-Foo"] == "bar" | ||
|
||
# does not override the already given value when not specified | ||
copied = client.copy() | ||
assert copied.default_headers["X-Foo"] == "bar" | ||
|
||
# merges already given headers | ||
copied = client.copy(default_headers={"X-Bar": "stainless"}) | ||
assert copied.default_headers["X-Foo"] == "bar" | ||
assert copied.default_headers["X-Bar"] == "stainless" | ||
|
||
# uses new values for any already given headers | ||
copied = client.copy(default_headers={"X-Foo": "stainless"}) | ||
assert copied.default_headers["X-Foo"] == "stainless" | ||
|
||
# set_default_headers | ||
|
||
# completely overrides already set values | ||
copied = client.copy(set_default_headers={}) | ||
assert copied.default_headers.get("X-Foo") is None | ||
|
||
copied = client.copy(set_default_headers={"X-Bar": "Robert"}) | ||
assert copied.default_headers["X-Bar"] == "Robert" | ||
|
||
with pytest.raises( | ||
ValueError, | ||
match="`default_headers` and `set_default_headers` arguments are mutually exclusive", | ||
): | ||
client.copy(set_default_headers={}, default_headers={"X-Foo": "Bar"}) | ||
|
||
|
||
class TestAsyncAnthropicVertex: | ||
client = AsyncAnthropicVertex(region="region", project_id="project") | ||
|
||
def test_copy(self) -> None: | ||
copied = self.client.copy() | ||
assert id(copied) != id(self.client) | ||
|
||
copied = self.client.copy(region="another-region", project_id="another-project") | ||
assert copied.region == "another-region" | ||
assert self.client.region == "region" | ||
assert copied.project_id == "another-project" | ||
assert self.client.project_id == "project" | ||
|
||
def test_with_options(self) -> None: | ||
copied = self.client.with_options(region="another-region", project_id="another-project") | ||
assert copied.region == "another-region" | ||
assert self.client.region == "region" | ||
assert copied.project_id == "another-project" | ||
assert self.client.project_id == "project" | ||
|
||
def test_copy_default_options(self) -> None: | ||
# options that have a default are overridden correctly | ||
copied = self.client.copy(max_retries=7) | ||
assert copied.max_retries == 7 | ||
assert self.client.max_retries == 2 | ||
|
||
copied2 = copied.copy(max_retries=6) | ||
assert copied2.max_retries == 6 | ||
assert copied.max_retries == 7 | ||
|
||
# timeout | ||
assert isinstance(self.client.timeout, httpx.Timeout) | ||
copied = self.client.copy(timeout=None) | ||
assert copied.timeout is None | ||
assert isinstance(self.client.timeout, httpx.Timeout) | ||
|
||
def test_copy_default_headers(self) -> None: | ||
client = AsyncAnthropicVertex( | ||
base_url=base_url, | ||
region="region", | ||
project_id="project", | ||
_strict_response_validation=True, | ||
default_headers={"X-Foo": "bar"}, | ||
) | ||
assert client.default_headers["X-Foo"] == "bar" | ||
|
||
# does not override the already given value when not specified | ||
copied = client.copy() | ||
assert copied.default_headers["X-Foo"] == "bar" | ||
|
||
# merges already given headers | ||
copied = client.copy(default_headers={"X-Bar": "stainless"}) | ||
assert copied.default_headers["X-Foo"] == "bar" | ||
assert copied.default_headers["X-Bar"] == "stainless" | ||
|
||
# uses new values for any already given headers | ||
copied = client.copy(default_headers={"X-Foo": "stainless"}) | ||
assert copied.default_headers["X-Foo"] == "stainless" | ||
|
||
# set_default_headers | ||
|
||
# completely overrides already set values | ||
copied = client.copy(set_default_headers={}) | ||
assert copied.default_headers.get("X-Foo") is None | ||
|
||
copied = client.copy(set_default_headers={"X-Bar": "Robert"}) | ||
assert copied.default_headers["X-Bar"] == "Robert" | ||
|
||
with pytest.raises( | ||
ValueError, | ||
match="`default_headers` and `set_default_headers` arguments are mutually exclusive", | ||
): | ||
client.copy(set_default_headers={}, default_headers={"X-Foo": "Bar"}) |