Skip to content

Commit

Permalink
fix(models): mark unknown fields as set in pydantic v1 (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Nov 10, 2023
1 parent a60d543 commit 4ce7a1e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/anthropic/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def construct(
if PYDANTIC_V2:
_extra[key] = value
else:
_fields_set.add(key)
fields_values[key] = value

object.__setattr__(m, "__dict__", fields_values)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class TestAnthropic:

@pytest.mark.respx(base_url=base_url)
def test_raw_response(self, respx_mock: MockRouter) -> None:
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json='{"foo": "bar"}'))
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json={"foo": "bar"}))

response = self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

@pytest.mark.respx(base_url=base_url)
def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
Expand All @@ -58,7 +58,7 @@ def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
response = self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

def test_copy(self) -> None:
copied = self.client.copy()
Expand Down Expand Up @@ -669,12 +669,12 @@ class TestAsyncAnthropic:
@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
async def test_raw_response(self, respx_mock: MockRouter) -> None:
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json='{"foo": "bar"}'))
respx_mock.post("/foo").mock(return_value=httpx.Response(200, json={"foo": "bar"}))

response = await self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

@pytest.mark.respx(base_url=base_url)
@pytest.mark.asyncio
Expand All @@ -686,7 +686,7 @@ async def test_raw_response_for_binary(self, respx_mock: MockRouter) -> None:
response = await self.client.post("/foo", cast_to=httpx.Response)
assert response.status_code == 200
assert isinstance(response, httpx.Response)
assert response.json() == '{"foo": "bar"}'
assert response.json() == {"foo": "bar"}

def test_copy(self) -> None:
copied = self.client.copy()
Expand Down
11 changes: 9 additions & 2 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from anthropic._utils import PropertyInfo, transform, parse_datetime
from anthropic._compat import PYDANTIC_V2
from anthropic._models import BaseModel


Expand Down Expand Up @@ -210,14 +211,20 @@ def test_pydantic_unknown_field() -> None:

def test_pydantic_mismatched_types() -> None:
model = MyModel.construct(foo=True)
with pytest.warns(UserWarning):
if PYDANTIC_V2:
with pytest.warns(UserWarning):
params = transform(model, Any)
else:
params = transform(model, Any)
assert params == {"foo": True}


def test_pydantic_mismatched_object_type() -> None:
model = MyModel.construct(foo=MyModel.construct(hello="world"))
with pytest.warns(UserWarning):
if PYDANTIC_V2:
with pytest.warns(UserWarning):
params = transform(model, Any)
else:
params = transform(model, Any)
assert params == {"foo": {"hello": "world"}}

Expand Down

0 comments on commit 4ce7a1e

Please sign in to comment.